PHP实现排序功能总结

 2763

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


PHP实现排序功能总结


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

一、sql:

  1. -- phpMyAdmin SQL Dump
  2. -- version 4.5.1
  3. -- http://www.phpmyadmin.net
  4. --
  5. -- Host: 127.0.0.1
  6. -- Generation Time: 2022-03-17 17:19:09
  7. -- 服务器版本: 10.1.13-MariaDB
  8. -- PHP Version: 5.6.21
  9.  
  10. SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
  11. SET time_zone = "+00:00";
  12.  
  13.  
  14. /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  15. /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  16. /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  17. /*!40101 SET NAMES utf8mb4 */;
  18.  
  19. --
  20. -- Database: `a`
  21. --
  22.  
  23. -- --------------------------------------------------------
  24.  
  25. --
  26. -- 表的结构 `search`
  27. --
  28.  
  29. CREATE TABLE `search` (
  30.   `id` int(11) NOT NULL DEFAULT '0',
  31.   `content` text COLLATE utf8_vietnamese_ci NOT NULL
  32. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_vietnamese_ci;
  33.  
  34. --
  35. -- 转存表中的数据 `search`
  36. --
  37.  
  38. INSERT INTO `search` (`id`, `content`) VALUES
  39. (666, 'cyg'),
  40. (2, 'liwen'),
  41. (555, 'liwen&cyg');
  42.  
  43. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  44. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  45. /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

二、使用步骤

核心问题:

1、怎么链接数据库呢?

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

解析:链接数据库,在自己的电脑本地地址上localhost。数据库软件用户名:root.密码"", 数据库名:a

2、怎么设置链接的数据库的字符编码呢?

  1. mysqli_set_charset($link,'utf8');

设置这种utf8编码,不至于有汉字乱码。

3、怎么运行php中的sql呢?

  1. mysqli_query($link,$sql);

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

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

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

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

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

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

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

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

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

  1. $row=mysqli_fetch_array($result)

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


1、cyg.php

代码如下(示例):

  1. <?php
  2. $link=mysqli_connect('localhost','root','','a');
  3. //然后是指定php链接数据库的字符集
  4. mysqli_set_charset($link,'utf8');
  5. $sql="select * from search";
  6. $result=mysqli_query($link,$sql);//运行sql
  7.  
  8. ?>
  9. <!--显示的效果-->
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <meta charset="UTF-8">
  14. <title>Document</title>
  15. </head>
  16. <body>
  17. <table border="1" cellpadding="5">
  18. <tr>
  19. <td>id</td>
  20. <td>标题</td>
  21. <td>内容</td>
  22.  
  23. <?php 
  24. while ($row=mysqli_fetch_array($result)) {//把对象变成数组输出,不然会报错哦
  25.  
  26. ?>
  27. <tr>
  28. <td><?=$row['id'];?></td>
  29. <td><?=$row['content'];?></td>
  30.  
  31.  
  32. </tr>
  33. <?php 
  34. }
  35. ?>
  36. <td><a href="create.php">创建才能排序哦</a></td>
  37. <td><a href="asc.php">升序</a></td><!--从小到大-->
  38. <td><a href="desc.php">降序</a></td><!--从大到小-->
  39. </tr>
  40. </table>
  41. </body>
  42. </html>


2、create.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <form action="create.php" method="POST">
  9. <input type="text" name="id">
  10. <input type="text" name="content">
  11.  
  12. <input type="submit" value="提交">
  13. </form>
  14. </body>
  15. </html>
  16. <?php
  17. if(!$_POST['content']||!$_POST['id'])
  18. {
  19. exit();
  20. }
  21. $content=$_POST['content'];
  22. $id=$_POST['id'];
  23.  
  24. $link=mysqli_connect('localhost','root','','a');
  25. //然后是指定php链接数据库的字符集
  26. mysqli_set_charset($link,'utf8');
  27. $sql = "INSERT INTO search(id,content)
  28. VALUES ('{$id}','{$content}')";
  29.   
  30. $result=mysqli_query($link,$sql);
  31. echo "<script>alert('创建成功');</script>";
  32. ?>
  33. <button><a href="cyg.php">返回</a></button>


3、asc.php

  1. <?php
  2. $link=mysqli_connect('localhost','root','','a');
  3. //然后是指定php链接数据库的字符集
  4. mysqli_set_charset($link,'utf8');
  5. $sql = "SELECT id,content FROM search ORDER BY id";
  6.   
  7. $result=mysqli_query($link,$sql);
  8.  
  9. ?>
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <meta charset="UTF-8">
  14. <title>Document</title>
  15. </head>
  16. <body>
  17. <table border="1" cellpadding="5">
  18. <tr>
  19. <td>id</td>
  20. <td>标题</td>
  21. <td>内容</td>
  22.  
  23. <?php 
  24. while ($row=mysqli_fetch_array($result)) {//把对象编程数组输出,不然会报错哦
  25.  
  26. ?>
  27. <tr>
  28. <td><?=$row['id'];?></td>
  29. <td><?=$row['content'];?></td>
  30.  
  31.  
  32. </tr>
  33. <?php 
  34. }
  35. ?>
  36. <td><a href="create.php">创建才能排序哦</a></td>
  37. <td><a href="asc.php">升序</a></td><!--从小到大-->
  38. <td><a href="desc.php">降序</a></td><!--从大到小-->
  39. </tr>
  40. </table>
  41. </body>
  42. </html>


4、desc.php

  1. <?php
  2. $link=mysqli_connect('localhost','root','','a');
  3. //然后是指定php链接数据库的字符集
  4. mysqli_set_charset($link,'utf8');
  5. $sql = "SELECT id,content FROM search ORDER BY id desc";
  6.   
  7. $result=mysqli_query($link,$sql);
  8.  
  9. ?>
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <meta charset="UTF-8">
  14. <title>Document</title>
  15. </head>
  16. <body>
  17. <table border="1" cellpadding="5">
  18. <tr>
  19. <td>id</td>
  20. <td>标题</td>
  21. <td>内容</td>
  22.  
  23. <?php 
  24. while ($row=mysqli_fetch_array($result)) {//把对象编程数组输出,不然会报错哦
  25.  
  26. ?>
  27. <tr>
  28. <td><?=$row['id'];?></td>
  29. <td><?=$row['content'];?></td>
  30.  
  31.  
  32. </tr>
  33. <?php 
  34. }
  35. ?>
  36. <td><a href="create.php">创建才能排序哦</a></td>
  37. <td><a href="asc.php">升序</a></td><!--从小到大-->
  38. <td><a href="desc.php">降序</a></td><!--从大到小-->
  39. </tr>
  40. </table>
  41. </body>
  42. </html>


TAG标签:
本文网址:https://www.zztuku.com/index.php/detail-12387.html
站长图库 - PHP实现排序功能总结
申明:本文转载于《CSDN》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐