时间:2021-07-01 10:21:17 帮助过:6人阅读
PHP分页代码实例教程
12345678910111213141516171819202122 | //数据库连接@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?'',$gblist['nickname'],'?';????echo?'发表于:',date("Y-m-d?H:i",?$gblist[createtime]),' ';????echo?'内容:',$gblist['content'],' ';}确定当前页参数?$p?中,使用了一个三元运算符:$p?=?$_GET['p']?$_GET['p']:1; |
1234567891011121314151617 | //分页代码//计算留言总数$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?'?',$i,'';????????}????}} |