PHP分页代码


定义变量部分:

<?php
	$all_num=mysql_num_rows($res);//获取数据总数
	$num_rows=5;//每页显示记录数
	$all_page_count=ceil($all_num/$num_rows);//总页数
	$page=empty($_GET['page'])?1:$_GET['page'];//如果get到空值那么PAGE参数即为1
	$page=(int)$page;//强制转换类型
	$limit_start = ($page-1)*$rownum;//计算从第几开始
        $px = $page>=$all_page_count ? $all_page_count : $page+1 ;
        $ps = $page<=1 ? 1 : $page-1 ;
?>

输出部分:

<ul>
<?php
	$sql="select * from data limit $limit_start,$rownum";
	$res=mysql_query($sql);
	while ($row=mysql_fetch_array($res)) 
	{
	    echo "<li>{$row[0]}</li>";
	}
      	    $px = $page>=$all_page_count ? $all_page_count : $page+1 ;
	    $ps = $page<=1 ? 1 : $page-1 ;
	?>
</ul>
<a href="index.php">首页</a>
<a href='index.php?page=<?php echo $ps?>'>上一页 </a>|
<a href='index.php?page=<?php echo $px?>'>下一页 </a>|
<a href='index.php?page=<?php echo $all_page_count?>'>尾页 </a>

————转载自PHP100

9 thoughts on “PHP分页代码”

      1. $all_num无故变成了$allnum;
        $num_rows无故变成了$rownum;
        $all_page_count无故变成了$pageall;
        $limit_start无故变成了$limit_st;
        大约有这些呢

Leave a Reply

Your email address will not be published. Required fields are marked *