直接上代码,注释很详细了。
<?php
/**
* php分页技术详解
* @author jahng
*/header("Content-type: text/html; charset=UTF-8");echo'<link rel="stylesheet" type="text/css" href="css.css" media="all" />'; require_once('page.php');$db = @mysql_connect("localhost","root","haojiang");if($db==false){exit("连接数据库失败!");}if(!mysql_select_db("gbook", $db)){exit("gbook数据库不存在!");}//从数据库获取将要分页的数据的总记录数$result=mysql_query("SELECT COUNT(*) AS c FROM message");$allRecord=mysql_fetch_assoc($result);$count=$allRecord['c'];//获取当前页$currentPage=isset($_GET['page'])?intval($_GET['page']):1;//每页显示五条记录$limit=5;//计算当前页的第一条数据在数据库中的id$start=($currentPage-1)*$limit; //从数据库获取当前页将要显示的数据$sql="SELECT * FROM message LIMIT $start,$limit";$result=mysql_query($sql);$message=array();if($result && mysql_num_rows($result)!=0){while($r=mysql_fetch_assoc($result)){$message[]=$r;}}//简单的留言显示foreach($message as $item){echo "<dt>".date('Y-m-d h:i:s',$item['time'])." ".$item['name']."</dt>";echo "<dd>".$item['content']."</dd>";}//分页链接echo page($currentPage,$count,$limit,5,$class='sabrosus');
?>
效果图:
demo可以在 PHP分页demo 下载。