標籤:
php sortable 動態排序未分頁版.php
預覽圖:
<?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
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 "<<首頁 ";
}else{
echo "<a href=".$add."page_id=1><<首頁</a> ";
}
if($up<1){
$up=1;
echo "<span style=\"color:grey;\"><上一頁</span> ";
}else {
echo "<a href=".$add."page_id=".$up."><上一頁</a> ";
}
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\"> ".$i."/".$total_page." </option>";
}else{
echo "<option value=\"".$i."\"> ".$i."/".$total_page." </option>";
}
}
echo "</select>";
if($down>$total_page){
$down=$total_page;
echo " <span style=\"color:grey;\">下一頁></span> ";
}else {
echo " <a href=".$add."page_id=".$down.">下一頁></a> ";
}
if($page_id==$total_page){
echo "尾頁>>";
}else{
echo "<a href=".$add."page_id=$total_page>尾頁>></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 @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@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);
這篇文章是受他人的啟發,自己抽空參照網友的文章寫的,大家多提意見,原文本空間也順便轉了一下,在它的基礎上改進了一些東西,希望大家喜歡,謝謝~
php sortable 動態排序