PHP练习之实现分页
程序员文章站
2022-04-11 20:45:55
...
这篇文章主要介绍了关于PHP练习之实现分页,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
创建表语句
CREATE TABLE `guestbook` ( `id` mediumint(8) unsigned NOT NULL auto_increment, `nickname` char(15) NOT NULL default '', `email` varchar(100) NOT NULL default '', `content` text NOT NULL, `createtime` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
插入数据
insert into guestbook (`id`,`nickname`,`email`,`content`,`createtime`) values('1', 'admin', 'admin@5idev.com', '留言测试', '1264167501'); insert into guestbook (`id`,`nickname`,`email`,`content`,`createtime`) values('2', 'user', 'user@163.com', '大家好', '1264168127'); insert into guestbook (`id`,`nickname`,`email`,`content`,`createtime`) values('3', '小明', 'xiaoming@163.com', '做得好,继续努力。。', '1264168865'); insert into guestbook (`id`,`nickname`,`email`,`content`,`createtime`) values('4', '小张', 'xiaozhang@163.com', '来看看', '1264169118'); insert into guestbook (`id`,`nickname`,`email`,`content`,`createtime`) values('5', '小丽', 'xiaoli@tom.com', 'haha', '1283276566'); insert into guestbook (`id`,`nickname`,`email`,`content`,`createtime`) values('6', 'Tom', 'tom@gmail.com', 'Hello', '1283336218'); insert into guestbook (`id`,`nickname`,`email`,`content`,`createtime`) values('7', 'Jack', 'jack@hotmail.com', 'okok', '1283336315'); insert into guestbook (`id`,`nickname`,`email`,`content`,`createtime`) values('8', 'admin', 'admin@5idev.com', '嗯嗯', '1283336315'); insert into guestbook (`id`,`nickname`,`email`,`content`,`createtime`) values('9', '阿里巴巴', 'alibaba@5idev.com', '来看看', '1283337158'); insert into guestbook (`id`,`nickname`,`email`,`content`,`createtime`) values('10', '路人甲', 'haha@163.com', '哈哈哈', '1283338228');
源代码
<?php $host = 'localhost'; $username = 'root'; $password = ''; $dbname = 'test'; $port = ''; $link = new mysqli($host,$username,$password,$dbname); if($link->connect_error){ die('Connect Error('.$linlk->connect_erron.')'.$link->connect_error); } $link->set_charset("utf8"); //每页显示的留言数 $pageSize = 4; //确定当前页数$p 参数 $p = isset($_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 = $link->query($query_sql); if($result){ while($gblist = $result->fetch_array(MYSQLI_ASSOC)){ echo '<a href="',$gblist['nickname'],'">',$gblist['nickname'],'</a> '; echo '发表于:',date("Y-m-d H:i", $gblist['createtime']),'<br />'; echo '内容:',$gblist['content'],'<br /><hr />'; } } //分页格式 $count_sql = "select count(*) as count from guestbook"; $count_result = $link->query($count_sql); $count_array = $count_result->fetch_assoc(); $count = $count_array['count']; //计算总的页数 $pagenum = ceil($count/$pageSize); echo '共',$count,'条留言'; //循环输出各页数目及连接 if($pagenum > 1){ for($i=1;$i<=$pagenum;$i++){ if($i == $p){ echo "[$i]"; }else{ echo "[<a href='oPagenate.php?p=$i"."'>$i</a>]"; } } }
相关推荐:
以上就是PHP练习之实现分页的详细内容,更多请关注其它相关文章!
上一篇: 文件系统的基础知识点讲解
下一篇: Linux显示中文乱码解决方法