完整代码如下:
<?php //数据库连接 @mysql_connect("localhost","root","root123") or die("连接数据库失败:".mysql_error()); mysql_select_db("test"); mysql_query("set character set 'gbk'"); //每页显示的留言数 $pagesize = 4; //确定页数 p 参数 $p = $_GET['p']?$_GET['p']:1; //数据指针 $offset = ($p-1)*$pagesize; //查询本页显示的数据 $query_sql = "SELECT * FROM guestbook ORDER BY id DESC LIMIT $offset , $pagesize"; //echo $query_sql; $result = mysql_query($query_sql); //循环输出 while($gblist = mysql_fetch_array($result)){ echo '<a href="',$gblist['nickname'],'">',$gblist['nickname'],'</a>�'; echo '发表于:',date("Y-m-d H:i", $gblist[createtime]),'<br />'; echo '内容:',$gblist['content'],'<br /><hr />'; } //分页代码 //计算留言总数 $count_result = mysql_query("SELECT count(*) as count FROM guestbook"); $count_array = mysql_fetch_array($count_result); //计算总的页数 $pagenum=ceil($count_array['count']/$pagesize); echo '共 ',$count_array['count'],' 条留言'; //循环输出各页数目及连接 if ($pagenum > 1) { for($i=1;$i<=$pagenum;$i++) { if($i==$p) { echo ' [',$i,']'; } else { echo ' <a href="index.php?p=',$i,'">',$i,'</a>'; } } } ?>
运行显示效果:
Tom 发表于:2010-09-01 18:16 内容:Hello -------------------------------------------------------------------------------- 小丽 发表于:2010-09-01 01:42 内容:hahaha -------------------------------------------------------------------------------- 小张 发表于:2010-01-22 22:05 内容:来看看 -------------------------------------------------------------------------------- 小明 发表于:2010-01-22 22:01 内容:做得好,继续努力 -------------------------------------------------------------------------------- 共 10 条留言 1 [2] 3