<p class="intro">php ajax 静态分页过程形式,扩展性不是很好 ,但比较适合新手学习。</p> <p id="con_all"> </p> <p id="con_ad1"></p> <p id="con_ad8"></p> 代码如下: <br>js 端 及判断 <br><script language="javascript"> <BR>$(function(){ <BR>//var url=$(this)[0].location.href; <BR>//alert(url); <BR>goPageFirst(); <BR>$('#selpage').bind('change',goPage); <BR>}); <BR>//返回当前页数 <BR>function getNowPage() <BR>{ <BR>return parseInt($('#nowPage').html()); <BR>} <BR>//返回每页显示的条数 <BR>function getPer() <BR>{ <BR>return $('#perPage').val(); <BR>} <BR>//返回总记录数 <BR>function getTotal() <BR>{ <BR>return parseInt($('#total').html()); <BR>} <BR>//返回总页数 <BR>function getAllPage() <BR>{ <BR>return parseInt($('#allpage').html()); <BR>} <BR>//组合字符串参数 <BR>function getData(act) <BR>{ <BR>var target=1; <BR>var now=getNowPage()+1; <BR>var per=getPer(); <BR>var sum=getTotal(); <BR>var allpage=getAllPage(); <BR>switch(act) <BR>{ <BR>case 'first': <BR>target=1; <BR>break; <BR>case 'prev': <BR>target=now>1?now-1:1; <BR>break; <BR>case 'next': <BR>target=now+1>allpage?now:1; <BR>break; <BR>case 'last': <BR>target=allpage>0?allpage:1; <BR>break; <BR>case 'constans': <BR>target=$('#selpage').val()>0?$('#selpage').val():1; <BR>default : <BR>target=1; <BR>} <BR>var data="check="+act+"&nowpage="+now+"&perpage="+per+'&target='+target; <BR>alert(data); <BR>return data; <BR>} <BR>//执行第一页 <BR>function goPageFirst() <BR>{ <BR>var param=getData('first'); <BR>http_post(param); <BR>} <BR>//执行上一页 <BR>function goPagePrev() <BR>{ <BR>var param=getData('prev'); <BR>http_post(param); <BR>} <BR>//执行下一页 <BR>function goPageNext() <BR>{ <BR>var param=getData('next'); <BR>http_post(param); <BR>} <BR>//执行最后页 <BR>function goPageLast() <BR>{ <BR>var param=getData('last'); <BR>http_post(param); <BR>} <BR>//选中到指定页面 <BR>function goPage() <BR>{ <BR>var param=getData('constans'); <BR>http_post(param); <BR>} <BR>//post 请求数据加载 <BR>function http_post(db) <BR>{ <BR>$.post('./mysql.php',db,function(data){ <BR>responseAjax(data); <BR>},'text'); <BR>} <BR>//返回数据的调处理 <BR>function responseAjax(msg) <BR>{ <BR>//alert(msg); <BR>//alert(msg.content); <BR>//$('#listDiv').html(msg.content); <BR>$('#listDiv').html(msg); <BR>} <BR>server端 判断及实现 <BR>//@autor 499188204@qq.com <BR>mb_internal_encoding('gb2312'); <BR>$con=mysql_connect("127.0.0.1","root","12345678") or die("连接失败"); <BR>$m=mysql_select_db("ec",$con); <BR>$sql = 'SELECT `attr_id`,`cat_id`,`attr_name` <BR>FROM `ecs_attribute` '; <BR>$rs = mysql_query($sql ); <BR>$count = mysql_num_rows($rs); //返回记录总数 <BR>//echo $count; //总记录数 <BR>//每页的记录数 10 <BR>//求总页数 ceil($count/10) <BR>//echo '<hr >'; <BR>if(!isset($_POST['check'])) <BR>{ <BR>$set['id']=0; <BR>die('no access please!'); <BR>} <BR>if(isset($_POST['perpage']) && $_POST['perpage']>0 ) <BR>{ <BR>$per=$_POST['perpage']; <BR>} <BR>else <BR>{ <BR>$per=15; <BR>} <BR>$page = ceil($count/$per); <BR>//echo $page; <BR>if(!isset($_POST['nowpage'])) <BR>{ <BR>$nowPage=1; <BR>} <BR>else <BR>{ <BR>$nowPage = $_POST['nowpage']; <BR>} <BR>$start = ($nowPage-1)*$per+1; <BR>$sql = 'select `attr_id`,`cat_id`,`attr_name` from `ecs_attribute` limit '.$start.','. $per; <BR>//$sql = 'select `attr_id`,`cat_id`,`attr_name` from `ecs_attribute` limit 10 offset 20'; <BR>$rs = mysql_query($sql ); <br><br>$str = "<table id='content'> <BR><tr><th>属性编号<th>类型编号<th>属性名字"; <BR>while($row = mysql_fetch_assoc($rs)){ <BR>$str .='<tr><td>'.$row['attr_id'].'<td>'.$row['cat_id'].'<td>'.$row['attr_name'].'<tr>'; <BR>} <BR>$str.=" <BR><table id='page_list'> <BR><tr><td><div><span>总计<span id='total'>".$count."个记录分为<span id='allpage'>".$page."页当前第<span id='nowPage'>".$nowPage."页,每页<input type='text' id='perPage' value='".$per."'/> | <BR><a href='javascript:void(0)' onclick='goPageFirst()'>第一页 <BR><a href='javascript:void(0)' onclick='goPagePrev()'>上一页 <BR><a href='javascript:void(0)' onclick='goPageFirst()'>下一页 <BR><a href='javascript:void(0)' onclick='goPageFirst()'>最末页 <BR><select id='selpage' >"; <BR>for($i=1;$i <=$page;$i++) <BR>{ <BR>if($nowPage==$i) <BR>{ <BR>$str .= "<option selected >".$i.""; <BR>} <BR>else <BR>{ <BR>$str .= "<option >".$i.""; <BR>} <BR>} <BR>$str .=" <BR> <BR>"; <BR>echo mb_convert_encoding($str,'UTF-8','gb2312'); <BR>//$set['content']=$str; <BR>//echo json_encode($set); <BR>?> <BR> </script>