php sortable 動態排序

來源:互聯網
上載者:User

標籤:

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 "&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 @[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 動態排序

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.