PHP原生分页的编写
1 <?php 2 $link=mysql_connect("localhost","root","admin")or die("connect error!"); 3 mysql_select_db("news")or die("select db error!"); 4 5 $sql="select * from admin_login "; 6 $result=mysql_query($sql); 7 $cols=mysql_num_fields($result);//总列数 8 $total=mysql_num_rows($result);//总条数 9 $num=10;//每页显示条数 10 $url="mysql.php";//请求的URL 11 $cpage=isset($_GET["page"])?$_GET["page"]:1;//当前页 12 $pagenum=ceil($total/$num);//总页数 13 $offset=($cpage-1)*$num;//开始去数据的位置 14 $sql="select * from admin_login limit {$offset},{$num}"; 15 $result=mysql_query($sql); 16 $start=$offset+1;//开始记录页 17 $end=($cpage==$pagenum)?$total : ($cpage*$num);//结束记录页 18 $next=($cpage==$pagenum)? 0:($cpage+1);//下一页 19 $prev=($cpage==1)? 0:($cpage-1);//前一页 20 21 echo '<table align="center" width="800" border="1">'; 22 while($row=mysql_fetch_row($result)){ 23 echo '<tr>'; 24 foreach($row as $col){ 25 echo '<td>'.$col.'</td>'; 26 } 27 echo '</tr>'; 28 } 29 echo '<tr><td colspan="'.$cols.'" align="right">'; 30 echo "共<b>$total</b>条记录,本页显示<b>{$start}-{$end}</b> {$cpage}/{$pagenum}"; 31 if($cpage==1) 32 echo " 首页 "; 33 else 34 echo " <a href='{$url}?page=1'>首页</a> "; 35 if($prev) 36 echo " <a href='{$url}?page={$prev}'>上一页</a> "; 37 else 38 echo " 上一页 "; 39 if($next) 40 echo " <a href='{$url}?page={$next}'>下一页</a> "; 41 else 42 echo " 下一页 "; 43 if($cpage==$pagenum) 44 echo " 尾页 "; 45 else 46 echo " <a href='{$url}?page={$pagenum}'>尾页</a> "; 47 echo '</td></tr>'; 48 echo '</table>'; 49 mysql_close();
效果: