Home > Article > Backend Development > PHP exercise to implement paging
This article mainly introduces the implementation of paging in PHP exercises. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it
Create table statement
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 data
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');
Source code
<?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>]"; } } }
Related recommendations:
PHP Practice Project Notes COOKIES
php implementation How to save base64 format images to a specified directory
The above is the detailed content of PHP exercise to implement paging. For more information, please follow other related articles on the PHP Chinese website!