首页  >  文章  >  后端开发  >  PHP练习之实现分页

PHP练习之实现分页

不言
不言原创
2018-04-26 10:01:381235浏览

这篇文章主要介绍了关于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 = &#39;localhost&#39;;    
    $username = &#39;root&#39;;    
    $password = &#39;&#39;;    
    $dbname = &#39;test&#39;;    
    $port = &#39;&#39;;    
    $link = new mysqli($host,$username,$password,$dbname);    
    if($link->connect_error){        
    die(&#39;Connect Error(&#39;.$linlk->connect_erron.&#39;)&#39;.$link->connect_error);
    }    
    $link->set_charset("utf8");    //每页显示的留言数
    $pageSize = 4;    //确定当前页数$p 参数
    $p = isset($_GET[&#39;p&#39;]) ? $_GET[&#39;p&#39;] : 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 &#39;<a href="&#39;,$gblist[&#39;nickname&#39;],&#39;">&#39;,$gblist[&#39;nickname&#39;],&#39;</a> &#39;;            
    echo &#39;发表于:&#39;,date("Y-m-d H:i", $gblist[&#39;createtime&#39;]),&#39;<br />&#39;;            
    echo &#39;内容:&#39;,$gblist[&#39;content&#39;],&#39;<br /><hr />&#39;;
          }
    }    //分页格式
    $count_sql = "select count(*) as count from guestbook";    
    $count_result = $link->query($count_sql);    
    $count_array = $count_result->fetch_assoc();    
    $count = $count_array[&#39;count&#39;];    //计算总的页数
    $pagenum = ceil($count/$pageSize);    
    echo &#39;共&#39;,$count,&#39;条留言&#39;;    //循环输出各页数目及连接
    if($pagenum > 1){        
    for($i=1;$i<=$pagenum;$i++){            
    if($i == $p){                
    echo "[$i]";
            }else{                
            echo "[<a href=&#39;oPagenate.php?p=$i"."&#39;>$i</a>]";
            }
        }
    }

相关推荐:

PHP练习项目笔记之COOKIES

php实现将base64格式图片保存到指定目录的方法

               

以上是PHP练习之实现分页的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn