Home > Backend Development > PHP Tutorial > PHP exercise to implement paging

PHP exercise to implement paging

不言
Release: 2023-03-24 20:08:02
Original
1264 people have browsed it

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 ;
Copy after login

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');
Copy after login

Source code

<?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>]";
            }
        }
    }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template