WD1X.COM - 问答一下,轻松解决,电脑应用解决专家
主板显卡CPU内存显示器
硬盘维修显卡维修显示器维修
注册表系统命令DOS命令Win8
存储光存储鼠标键盘
内存维修打印机维修
WinXPWin7Win11Linux
硬件综合机箱电源散热器手机数码
主板维修CPU维修键盘鼠标维修
Word教程Excel教程PowerPointWPS
网络工具系统工具图像工具
数据库javascript服务器
PHP教程CSS教程XML教程

PHP实现文章排序的上移下移功能

更新时间:2012-03-27 13:22 作者:佚名点击:

今天做系统要用到“上移”或“下移”功能,网上找到下面的方法,虽然繁琐,但是实现起来还算简单。

1、我们数据库表中需要一个排列字段作为移动排序的参考,表中添加字段weight;

2、我们一开始先让weight与ID对应起来,也就是每条记录的ID值:

weight字段设置的和自增字段相同。插入记录后$weight=mysql_insert_id(),得到最近插入记录ID的值,然后更新表:

$sql="update table set weight={$weight} where ID={$ID}";
mysql_query($sql);


移或下移时:取欲移动的新闻的上一新闻或是下一新闻的weight值,然后将自己的weight值改为刚才取出的weight,刚才取出的改为自己的。(交换一下weight值)SQL排序规则:在原有规则(order by)前加入weight desc

 


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>实现新闻的上移下移功能</title> 
  6. </head> 
  7.   
  8. <body> 
  9. <style> 
  10. *{font-size:12px;} 
  11. td{height:24px; line-height:24px; text-align:center} 
  12. </style> 
  13.   
  14. <?php 
  15. header("Content-type:text/html;charset=gb2312"); 
  16. include("mysql.class.php"); 
  17. $conn=new mysql("localhost","root","root","test"," ","gbk"); 
  18. if(!emptyempty($_GET["weight"])){ 
  19.     if($_GET["move"]=="up"){ 
  20.         //获取上一条新闻信息 
  21.         $query=$conn->query("select * from `article` where `weight` > '$_GET[weight]' order by `weight` asc limit 1"); 
  22.         if($conn->db_num_rows(query)>0){ 
  23.             $rows=$conn->fetch_array($query); 
  24.             $conn->query("update `article` set `weight`='$rows[weight]' where `weight`='$_GET[weight]'"); 
  25.             $conn->query("update `article` set `weight`='$_GET[weight]' where `ID`='$rows[ID]'"); 
  26.         }else
  27.             echo "<script>alert('已经在最顶上');</script>"
  28.         } 
  29.     }else if($_GET["move"]=="down"){ 
  30.         //获取下一条新闻信息 
  31.         $query=$conn->query("select * from `article` where `weight` < '$_GET[weight]' order by `weight` desc limit 1"); 
  32.         if($conn->db_num_rows(query)>0){ 
  33.             $rows=$conn->fetch_array($query); 
  34.             $conn->query("update `article` set `weight`='$rows[weight]' where `weight`='$_GET[weight]'"); 
  35.             $conn->query("update `article` set `weight`='$_GET[weight]' where `ID`='$rows[ID]'"); 
  36.         }else
  37.             echo "<script>alert('已经在最底下');</script>"
  38.         } 
  39.     } 
  40. ?> 
  41. <table border="1" cellpadding="0" cellspacing="0" width="300" align="center"
  42.         <caption>文章上下移动</caption> 
  43.         <tr> 
  44.             <td>ID</td><td>标题</td><td>移动</td> 
  45.         </tr> 
  46. <?php 
  47. $query=$conn->query("select * from `article` order by `weight` desc"); 
  48. if($conn->db_num_rows($query)>0){ 
  49. while($rows=$conn->fetch_array($query)){ 
  50.     $aid[].=$rows["ID"]; 
  51. ?> 
  52.         <tr> 
  53.             <td><?php echo $rows["ID"]?></td> 
  54.             <td><?php echo $rows["title"]?></td> 
  55.             <td><a href="<?php $PHP_SELF?>?weight=<?php echo $rows[weight]?>&move=up">上移</a>/<a href="<?php $PHP_SELF?>?weight=<?php echo $rows[weight]?>&move=down">下移</a></td> 
  56.         </tr> 
  57.   
  58. <?php 
  59.     } 
  60. ?> 
  61.     </table> 
  62. </body> 
  63. </html> 
顶一下
(4)
100%
踩一下
(0)
0%
------分隔线----------------------------
你可能感兴趣的内容