PHP分页代码实例教程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //数据库连接 @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"; $result?=?mysql_query($query_sql); //循环输出 while($gblist?=?mysql_fetch_array($result)){ ????echo?'<a?href="',$gblist['nickname'],'">',$gblist['nickname'],'?'; ????echo?'发表于:',date("Y-m-d?H:i",?$gblist[createtime]),'<br?/>'; ????echo?'内容:',$gblist['content'],'<br?/><hr?/>'; } 确定当前页参数?$p?中,使用了一个三元运算符: $p?=?$_GET['p']?$_GET['p']:1; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //分页代码 //计算留言总数 $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,''; ????????} ????} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?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'],''; ????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,''; ????????} ????} } ?> |