複製代碼 代碼如下:js 端 及判斷
<script language='javascript' >
$(function(){
//var url=$(this)[0].location.href;
//alert(url);
goPageFirst();
$('#selpage').bind('change',goPage);
});
//返回當前頁數
function getNowPage()
{
return parseInt($('#nowPage').html());
}
//返回每頁顯示的條數
function getPer()
{
return $('#perPage').val();
}
//返回總記錄數
function getTotal()
{
return parseInt($('#total').html());
}
//返回總頁數
function getAllPage()
{
return parseInt($('#allpage').html());
}
//組合字元串參數
function getData(act)
{
var target=1;
var now=getNowPage()+1;
var per=getPer();
var sum=getTotal();
var allpage=getAllPage();
switch(act)
{
case 'first':
target=1;
break;
case 'prev':
target=now>1?now-1:1;
break;
case 'next':
target=now+1>allpage?now:1;
break;
case 'last':
target=allpage>0?allpage:1;
break;
case 'constans':
target=$('#selpage').val()>0?$('#selpage').val():1;
default :
target=1;
}
var data="check="+act+"&nowpage="+now+"&perpage="+per+'&target='+target;
alert(data);
return data;
}
//執行第一頁
function goPageFirst()
{
var param=getData('first');
http_post(param);
}
//執行上一頁
function goPagePrev()
{
var param=getData('prev');
http_post(param);
}
//執行下一頁
function goPageNext()
{
var param=getData('next');
http_post(param);
}
//執行最後頁
function goPageLast()
{
var param=getData('last');
http_post(param);
}
//選中到指定頁面
function goPage()
{
var param=getData('constans');
http_post(param);
}
//post 請求資料載入
function http_post(db)
{
$.post('./mysql.php',db,function(data){
responseAjax(data);
},'text');
}
//返回資料的調處理
function responseAjax(msg)
{
//alert(msg);
//alert(msg.content);
//$('#listDiv').html(msg.content);
$('#listDiv').html(msg);
}
server端 判斷及實現
//@autor 499188204@qq.com
mb_internal_encoding('gb2312');
$con=mysql_connect("127.0.0.1","root","12345678") or die("串連失敗");
$m=mysql_select_db("ec",$con);
$sql = 'SELECT `attr_id`,`cat_id`,`attr_name`
FROM `ecs_attribute` ';
$rs = mysql_query($sql );
$count = mysql_num_rows($rs); //返回記錄總數
//echo $count; //總記錄數
//每頁的記錄數 10
//求總頁數 ceil($count/10)
//echo '<hr >';
if(!isset($_POST['check']))
{
$set['id']=0;
die('no access please!');
}
if(isset($_POST['perpage']) && $_POST['perpage']>0 )
{
$per=$_POST['perpage'];
}
else
{
$per=15;
}
$page = ceil($count/$per);
//echo $page;
if(!isset($_POST['nowpage']))
{
$nowPage=1;
}
else
{
$nowPage = $_POST['nowpage'];
}
$start = ($nowPage-1)*$per+1;
$sql = 'select `attr_id`,`cat_id`,`attr_name` from `ecs_attribute` limit '.$start.','. $per;
//$sql = 'select `attr_id`,`cat_id`,`attr_name` from `ecs_attribute` limit 10 offset 20';
$rs = mysql_query($sql );
$str = "<table id='content'>
<tr><th>屬性編號</th><th>類型編號</th><th>屬性名稱字</th></tr>";
while($row = mysql_fetch_assoc($rs)){
$str .='<tr><td>'.$row['attr_id'].'</td><td>'.$row['cat_id'].'</td><td>'.$row['attr_name'].'</td><tr>';
}
$str.="</table>
<table id='page_list'>
<tr><td><div><span>總計<span id='total'>".$count."</span>個記錄分為<span id='allpage'>".$page."</span>頁當前第<span id='nowPage'>".$nowPage."</span>頁,每頁<input type='text' id='perPage' value='".$per."'/> |
<a href='javascript:void(0)' onclick='goPageFirst()'>第一頁</a>
<a href='javascript:void(0)' onclick='goPagePrev()'>上一頁</a>
<a href='javascript:void(0)' onclick='goPageFirst()'>下一頁</a>
<a href='javascript:void(0)' onclick='goPageFirst()'>最末頁 </a>
<select id='selpage' >";
for($i=1;$i <=$page;$i++)
{
if($nowPage==$i)
{
$str .= "<option selected >".$i."</option>";
}
else
{
$str .= "<option >".$i."</option>";
}
}
$str .="</select>
</span></div></td></tr>
</table>";
echo mb_convert_encoding($str,'UTF-8','gb2312');
//$set['content']=$str;
//echo json_encode($set);
?>