<body>
<table class="" width="" cellspacing="" >
<tr>
<td>排序</td>
</tr>
<tr align="center" id='{$i["id"]}'> <!---这里的 id='{$i["id"]}' 因为我是用了smarty模板所以这么写的 '{$i["id"]}' 取得是数据库的id值 如果用了别的字段或者模板的话,请更正。-->
<td><a class="up" style="cursor:pointer">上移</a></td>
<td><a class="down" style="cursor:pointer">下移</a></td>
<td><a class="top" style="cursor:pointer">置顶</a></td>
</tr>
</table>
</body>
{literal}
<script language="JavaScript" type="text/javascript">
$(function(){
var $up = $(".up")
$up.click(function() {
var $tr = $(this).parents("tr");
if ($tr.index() != 0) {
$tr.fadeOut().fadeIn();
$tr_pre = $tr.prev();
$tr_pre.before($tr);
id_from = $tr.attr("id");
id_to = $tr_pre.attr("id");
if(id_from && id_to){
$.post("server.php", {"from":id_from, "to":id_to}, function(data){
alert(data);
});
}
}
});
var $down = $(".down");
var len = $down.length;
$down.click(function() {
var $tr = $(this).parents("tr");
if ($tr.index() != len - 1) {
$tr.fadeOut().fadeIn();
$tr_next = $tr.next();
$tr_next.after($tr);
id_from = $tr.attr("id");
id_to = $tr_next.attr("id");
if(id_from && id_to){
$.post("server.php", {"from":id_from, "to":id_to}, function(data){
alert(data);
});
}
}
});
var $top = $(".top");
$top.click(function(){
var $tr = $(this).parents("tr");
$tr.fadeOut().fadeIn();
$(".table").prepend($tr);
$tr.css("color","#f60");
});
});
</script>
{/literal}
<!--------------------------------------------------------万恶的分界线-------------------------------!>
//server.php
//ajax后台提交页面
<?php
include "connection.php";//连接数据库页面
$data = array(
"from" => $_POST["from"],
"to" => $_POST["to"],
);
print_r($data);
$sql = "update `数据表` set id=0 where id=".$_POST["to"];//将移动的id改为0 id=0在数据库是没有的
mysql_query($sql,$con);
$sql = "update `数据表` set id=".$_POST["to"]." where id=".$_POST["from"];//将被移动的id 改为移动的id
mysql_query($sql,$con);
$sql = "update `数据表` set id=".$_POST["from"]." where id=0";//将id=0 的改为被移动的id
mysql_query($sql,$con);
//就是这样了,置顶我没有做 置顶就是id改为1, 如果用别的专门的字段排序 改id为那个字段就可以了
?>