1、order.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<name>实现新闻的上移下移功能</name>
</head>
<body>
<style>
*{font-size:12px;}
td{height:24px; line-height:24px; text-align:center}
</style>
<?php
header("Content-type:text/html;charset=gb2312");
//include("mysql.class.php");
$conn=mysql_connect("localhost","root","12345678") or die("连接失败");
$db=mysql_select_db("fqwh");
if(!empty($_GET["weight"])){
if($_GET["move"]=="up"){
//获取上一条新闻信息
$query=mysql_query("select * from `usr` where `weight` > '$_GET[weight]' order by `weight` asc limit 1");
if(mysql_num_rows($query)>0){
$rows=mysql_fetch_array($query);
mysql_query("update `usr` set `weight`='$rows[weight]' where `weight`='$_GET[weight]'");
mysql_query("update `usr` set `weight`='$_GET[weight]' where `id`='$rows[id]'");
}else{
echo "<script>alert('已经在最顶上');</script>";
}
}else if($_GET["move"]=="down"){
//获取下一条新闻信息
$query=mysql_query("select * from `usr` where `weight` < '$_GET[weight]' order by `weight` desc limit 1");
if(mysql_num_rows($query)>0){
$rows=mysql_fetch_array($query);
mysql_query("update `usr` set `weight`='$rows[weight]' where `weight`='$_GET[weight]'");
mysql_query("update `usr` set `weight`='$_GET[weight]' where `id`='$rows[id]'");
}else{
echo "<script>alert('已经在最底下');</script>";
}
}
}
?>
<table border="1" cellpadding="0" cellspacing="0" width="300" align="center">
<caption>文章上下移动</caption>
<tr>
<td>id</td><td>标题</td><td>移动</td>
</tr>
<?php
$query=mysql_query("select * from `usr` order by `weight` desc");
if(mysql_num_rows($query)>0){
while($rows=mysql_fetch_array($query)){
$aid[].=$rows["id"];
?>
<tr>
<td><?php echo $rows["id"]?></td>
<td><?php echo $rows["name"]?></td>
<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>
</tr>
<?php
}
}
?>
</table>
</body>
</html>
2、sql脚本 fqwh.sql
/*
MySQL Data Transfer
Source Host: localhost
Source Database: fqwh
Target Host: localhost
Target Database: fqwh
Date: 2012-8-24 16:37:33
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for raty
-- ----------------------------
CREATE TABLE `raty` (
`id` varchar(255) NOT NULL,
`voter` varchar(255) DEFAULT NULL,
`total` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for usr
-- ----------------------------
CREATE TABLE `usr` (
`id` varchar(11) NOT NULL,
`name` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`chinesename` varchar(255) NOT NULL,
`email` varchar(255) DEFAULT NULL,
`qq` varchar(255) DEFAULT NULL,
`department` varchar(255) DEFAULT NULL,
`housename` varchar(255) DEFAULT NULL,
`groupid` int(11) DEFAULT NULL,
`weight` varchar(11) NOT NULL,
PRIMARY KEY (`id`,`weight`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `usr` VALUES ('1', '1', '1', '1', '1', '1', '1', '1', '1', '2');
INSERT INTO `usr` VALUES ('2', '2', '2', '2', '2', '2', '2', '2', '2', '1');
INSERT INTO `usr` VALUES ('3', '3', '3', '3', '3', '3', '3', '3', '3', '3');
INSERT INTO `usr` VALUES ('4', '123123', '1111', '4', null, null, null, null, null, '4');
INSERT INTO `usr` VALUES ('5', 'qwe', '111111', '5', null, null, null, null, null, '5');
INSERT INTO `usr` VALUES ('6', 'sdf', '123456', '6', null, null, null, null, null, '6');