您当前的位置: 首页 >  php

暂无认证

  • 1浏览

    0关注

    95081博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

php实现排序功能(提供全部)

发布时间:2022-03-18 00:24:55 ,浏览量:1

作者:陈业贵 华为云享专家 51cto(专家博主 明日之星 TOP红人)

文章目录
  • 前言
  • 一、sql:
  • 二、使用步骤
    • 1.cyg.php
    • 2.create.php
    • 2.asc.php
    • 2.desc.php
  • 总结
前言

和大家一起完成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"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- 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'); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 
二、使用步骤

核心问题: 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 lang="en"> <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 lang="en"> <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 ""; ?> <button><a href="cyg.php">返回</a></button> 
2.asc.php
//把对象编程数组输出,不然会报错哦 ?> <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
//把对象编程数组输出,不然会报错哦 ?> <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> 
总结

讲解完了,谢谢大家的访问。

关注
打赏
1655516835
查看更多评论
立即登录/注册

微信扫码登录

0.0530s