php sortable 动态排序

php sortable 动态排序未分页版.php

预览图:

php sortable 动态排序

<?php

mysql_connect("localhost","root","root");

mysql_select_db("test");

mysql_query("SET CHARACTER SET GB2312");

if($_POST["reorder"]){

$i=1;

foreach ($_POST["fruit"] as $fruit_id){

$query="update `fruit` set `order`='$i' where `id`='$fruit_id'";

mysql_query($query);

$i++;  

}

}

?>

<!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" />

<title>宋正河作品--php sortable 动态排序未分页版</title>

<style>

ul li{

font-size:14px;

list-style-type:none;

background:#33CCFF;

margin:5px;

padding:5px;

border:1px solid #666666;

width:300px;

cursor:pointer;

}

</style>

<script language="JavaScript" type="text/javascript" src="js/jquery-1.4.2.min.js"></script>

<script language="JavaScript" type="text/javascript" src="js/jquery-ui.js"></script>

<script language="JavaScript" type="text/javascript" src="js/ui.core.js"></script>

<script language="JavaScript" type="text/javascript" src="js/ui.sortable.js"></script>

<script type="text/javascript">

$(document).ready(function(){

$('#fruit_list').sortable();

});

</script>

</head>

<body>

<div align="center">

<form name="form1" method="post" action=""> 

  <ul id="fruit_list">  

  <?php

    $query="select * from `fruit` order by `order` ASC"; 

 $result=mysql_query($query);

 while($row=mysql_fetch_assoc($result)){    

 echo '<li><input type="hidden" name="fruit[]" value="'.$row["id"].'" />'.'ID:'.$row["id"].' | '.'名称:'.$row["name"].' | '.'序号:'.$row["order"].'</li>';

    }  

  ?>

  </ul> 

  <input type="submit" name="reorder" value="重新排序" /> 

</form>

拖动相应的项目进行排序,序号越小越靠前

</div>

</body>

</html>

php sortable 动态排序分页版.php

预览图:

php sortable 动态排序

<?php

mysql_connect("localhost","root","root");

mysql_select_db("test");

mysql_query("SET CHARACTER SET GB2312");

//分页函数 调用 $common_func->pages();$total_num为总记录数,$page_id为当前页码,$add为链接地址(可选),$pagesize为新闻条数

function pages($total_num,$page_id,$add,$pagesize){ 

$total_page=ceil($total_num/$pagesize);

$up=$page_id-1;

$down=$page_id+1;

if($page_id==1){

echo "&lt;&lt;首页&nbsp;";

}else{

echo "<a href=".$add."page_id=1>&lt;&lt;首页</a>&nbsp;";

}

if($up<1){

$up=1;

echo "<span style=\"color:grey;\">&lt;上一页</span>&nbsp;&nbsp;";

}else {

echo "<a href=".$add."page_id=".$up.">&lt;上一页</a>&nbsp;&nbsp;";

}

echo "<select onchange=\"window.location='".$add."page_id='+this.value;\">";

for($i=1;$i<=$total_page;$i++){

if($i==$page_id){

echo "<option value=\"".$i."\" selected=\"selected\">&nbsp;&nbsp;".$i."/".$total_page."&nbsp;&nbsp;</option>";

}else{

echo "<option value=\"".$i."\">&nbsp;&nbsp;".$i."/".$total_page."&nbsp;&nbsp;</option>";

}

}

echo "</select>";

if($down>$total_page){

$down=$total_page;

echo "&nbsp;&nbsp;<span style=\"color:grey;\">下一页&gt;</span>&nbsp;";

}else {

echo "&nbsp;&nbsp;<a href=".$add."page_id=".$down.">下一页&gt;</a>&nbsp;";

}

if($page_id==$total_page){

echo "尾页&gt;&gt;";

}else{

echo "<a href=".$add."page_id=$total_page>尾页&gt;&gt;</a>";

}

echo " 共".$total_num."条记录 每页显示".$pagesize."条";

//分页函数

$page_id=intval(trim($_GET["page_id"]));

$page_id=($page_id=="")?"1":$page_id;

$page_id=($page_id>0)?$page_id:"1";

 

$pagesize=intval(trim($_GET["pagesize"]));

$pagesize=($pagesize=="")?"3":$pagesize;

$pagesize=($pagesize>0)?$pagesize:"3";

 

$begin=($page_id-1)*$pagesize;

if($_POST["reorder"]){

$i=$begin+1;

foreach ($_POST["fruit"] as $fruit_id){

$query="update `fruit` set `order`='$i' where `id`='$fruit_id'";

mysql_query($query);

$i++;  

}

}

?>

<!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" />

<title>宋正河作品--php sortable 动态排序分页版</title>

<style>

ul li{

font-size:14px;

list-style-type:none;

background:#33CCFF;

margin:5px;

padding:5px;

border:1px solid #666666;

width:300px;

cursor:pointer;

}

</style>

<script language="JavaScript" type="text/javascript" src="js/jquery-1.4.2.min.js"></script>

<script language="JavaScript" type="text/javascript" src="js/jquery-ui.js"></script>

<script language="JavaScript" type="text/javascript" src="js/ui.core.js"></script>

<script language="JavaScript" type="text/javascript" src="js/ui.sortable.js"></script>

<script type="text/javascript">

$(document).ready(function(){

$('#fruit_list').sortable();

});

</script>

</head>

<body>

<div align="center">

<form name="form1" method="post" action=""> 

  <ul id="fruit_list">  

  <?php

    $query="select * from `fruit` order by `order` ASC";

 $result=mysql_query($query);

    $total_num=mysql_num_rows($result);

 $add="?";

 $query=$query." limit $begin,$pagesize";

 pages($total_num,$page_id,$add,$pagesize);

 $result=mysql_query($query);

 while($row=mysql_fetch_assoc($result)){    

 echo '<li><input type="hidden" name="fruit[]" value="'.$row["id"].'" />'.'ID:'.$row["id"].' | '.'名称:'.$row["name"].' | '.'序号:'.$row["order"].'</li>';

    }  

  ?>

  </ul> 

  <input type="submit" name="reorder" value="重新排序" /> 

</form>

拖动相应的项目进行排序,序号越小越靠前<br />

<a href="?pagesize=<?php echo $total_num;?>">显示全部</a> | <a href="?pagesize=3">分页显示</a>

</div>

</body>

</html>

 

test.sql

-- phpMyAdmin SQL Dump

-- version 3.1.5-rc1

-- http://www.phpmyadmin.net

--

-- 主机: localhost

-- 生成日期: 2011 年 01 月 08 日 06:22

-- 服务器版本: 5.0.18

-- PHP 版本: 5.2.8

 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

 

 

/*!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 utf8 */;

 

--

-- 数据库: `test`

--

 

-- --------------------------------------------------------

 

--

-- 表的结构 `fruit`

--

 

CREATE TABLE IF NOT EXISTS `fruit` (

  `id` int(11) unsigned NOT NULL auto_increment,

  `name` varchar(256) NOT NULL,

  `order` int(4) NOT NULL,

  PRIMARY KEY  (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=gb2312 AUTO_INCREMENT=7 ;

 

--

-- 导出表中的数据 `fruit`

--

 

INSERT INTO `fruit` (`id`, `name`, `order`) VALUES

(1, '苹果', 5),

(2, '西瓜', 2),

(3, '菠萝', 1),

(4, '桃子', 4),

(5, '樱桃', 3),

(6, '桔子', 6);


这篇文章是受他人的启发,自己抽空参照网友的文章写的,大家多提意见,原文本空间也顺便转了一下,在它的基础上改进了一些东西,希望大家喜欢,谢谢~

你可能感兴趣的:(table)