广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >PHP实现排序功能总结
  • 239
分享到

PHP实现排序功能总结

php 2022-06-20 13:06:07 239人浏览 安东尼
摘要

本篇文章给大家带来了关于PHP的相关知识,其中主要介绍了关于排序功能的相关问题,通过实例完成php+Mysqli排序功能的实现,下面一起来看一下,希望对大家有帮助。和大家一起完成php+mysqli排序功能的实现.一、sql:-- phpM

本篇文章给大家带来了关于PHP的相关知识,其中主要介绍了关于排序功能的相关问题,通过实例完成php+Mysqli排序功能的实现,下面一起来看一下,希望对大家有帮助。

和大家一起完成php+mysqli排序功能的实现.

一、sql:

-- phpMyAdmin SQL Dump
-- version 4.5.1
-- Http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: 2022-03-17 17:19:09
-- 服务器版本: 10.1.13-MariaDB
-- PHP Version: 5.6.21

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


;
;
;
;

--
-- Database: `a`
--

-- --------------------------------------------------------

--
-- 表的结构 `search`
--

CREATE TABLE `search` (
  `id` int(11) NOT NULL DEFAULT '0',
  `content` text COLLATE utf8_vietnamese_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_vietnamese_ci;

--
-- 转存表中的数据 `search`
--

INSERT INTO `search` (`id`, `content`) VALUES
(666, 'cyg'),
(2, 'liwen'),
(555, 'liwen&cyg');

;
;
;

二、使用步骤

核心问题:
1.怎么链接数据库呢?

$link=mysqli_connect('localhost','root','','a');

解析:链接数据库,在自己的电脑本地地址上localhost。数据库软件用户名:root.密码"", 数据库名:a
2.怎么设置链接的数据库的字符编码呢?

mysqli_set_charset($link,'utf8');

设置这种utf8编码,不至于有汉字乱码。
3.怎么运行php中的sql呢?

mysqli_query($link,$sql);

解析:第一个参数是数据库链接赋值的变量。第二个参数是sql语句变量

4.怎么在插入语句中写变量呢?

$sql = "INSERT INTO search(id,content)
VALUES ('{$id}','{$content}')";

解析:按照这种格式来就行了

5.排序的sql语句,升序怎么写?从小到大的是升序。越来越大

$sql = "SELECT id,content FROM search ORDER BY id";

6.从大到小的降序sql怎么写?越来越小

$sql = "SELECT id,content FROM search ORDER BY id desc";

7.mysqli_query遍历出来的数据要转化为数组才能运行.

$row=mysqli_fetch_array($result)

解析:因为foreach不支持mysqli_query数据直接输出

1.cyg.php

代码如下(示例):

<?php
$link=mysqli_connect('localhost','root','','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql="select * from search";
$result=mysqli_query($link,$sql);//运行sql

?>
<!--显示的效果-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table border="1" cellpadding="5">
<tr>
<td>id</td>
<td>标题</td>
<td>内容</td>

<?php 
while ($row=mysqli_fetch_array($result)) {//把对象变成数组输出,不然会报错哦

?>
<tr>
<td><?=$row['id'];?></td>
<td><?=$row['content'];?></td>


</tr>
<?php 
}
?>
<td><a href="create.php">创建才能排序哦</a></td>
<td><a href="asc.php">升序</a></td><!--从小到大-->
<td><a href="desc.php">降序</a></td><!--从大到小-->
</tr>
</table>
</body>
</html>

2.create.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<fORM action="create.php" method="POST">
<input type="text" name="id">
<input type="text" name="content">

<input type="submit" value="提交">
</form>
</body>
</html>
<?php
if(!$_POST['content']||!$_POST['id'])
{
exit();
}
$content=$_POST['content'];
$id=$_POST['id'];

$link=mysqli_connect('localhost','root','','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql = "INSERT INTO search(id,content)
VALUES ('{$id}','{$content}')";
 
$result=mysqli_query($link,$sql);
echo "<script>alert('创建成功');</script>";
?>
<button><a href="cyg.php">返回</a></button>

2.asc.php

<?php
$link=mysqli_connect('localhost','root','','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql = "SELECT id,content FROM search ORDER BY id";
 
$result=mysqli_query($link,$sql);

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table border="1" cellpadding="5">
<tr>
<td>id</td>
<td>标题</td>
<td>内容</td>

<?php 
while ($row=mysqli_fetch_array($result)) {//把对象编程数组输出,不然会报错哦

?>
<tr>
<td><?=$row['id'];?></td>
<td><?=$row['content'];?></td>


</tr>
<?php 
}
?>
<td><a href="create.php">创建才能排序哦</a></td>
<td><a href="asc.php">升序</a></td><!--从小到大-->
<td><a href="desc.php">降序</a></td><!--从大到小-->
</tr>
</table>
</body>
</html>

2.desc.php

<?php
$link=mysqli_connect('localhost','root','','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql = "SELECT id,content FROM search ORDER BY id desc";
 
$result=mysqli_query($link,$sql);

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table border="1" cellpadding="5">
<tr>
<td>id</td>
<td>标题</td>
<td>内容</td>

<?php 
while ($row=mysqli_fetch_array($result)) {//把对象编程数组输出,不然会报错哦

?>
<tr>
<td><?=$row['id'];?></td>
<td><?=$row['content'];?></td>


</tr>
<?php 
}
?>
<td><a href="create.php">创建才能排序哦</a></td>
<td><a href="asc.php">升序</a></td><!--从小到大-->
<td><a href="desc.php">降序</a></td><!--从大到小-->
</tr>
</table>
</body>
</html>
以上就是PHP实现排序功能总结的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: PHP实现排序功能总结

本文链接: https://www.lsjlt.com/news/32993.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作