PHP develops a simple guestbook with paging display message function

To display all user messages on the page and display them through paging.

The general idea of ​​paging is: first determine the number of messages that need to be displayed on each page, divide the total number of messages by the number of messages displayed on each page, and get the total number of pages. If it cannot be divided evenly, the remainder becomes a separate page. , the total number of pages +1.

6.png

Continue to use the LyDB class of the database set in the previous section.

Here is set to display 3 pieces of data per page

<?php
$page_size=3;
?>

Use a public function to put the number page_size displayed on each page, the number of messages ly_count, and the total number of pages page_count into an array array

<?php
class LyDb{
//....省略
public function select_page_info(){
   $sql = "select count(*) as pagecount from ly";
   $query = mysqli_query($this->link,$sql);
   $row = mysqli_fetch_assoc($query);
   $ly_count=$row["pagecount"];
   $page_count=($ly_count%$this->page_size==0)?($ly_count/$this->page_size):($ly_count/$this->page_size+1);
   $page_info=array("ly_count"=> $ly_count,"page_count"=>(int)$page_count,"page_size"=>$this->page_size);
   return $page_info;
  }
//......省略  
}
?>

Set the current page through a public function $page_no

When it is judged that the current page is less than 1, continue to display the first page. When the current page is greater than the total number of pages obtained, the last page will be displayed. Page

Determine the first and last pages

<?php
lass LyDb{
//....省略
public function select_page_result(&$page_no){
   $page_info=$this->select_page_info();
   if(!isset($page_no))$page_no=1;
   else if($page_no<1)$page_no=1;
   else if($page_no>$page_info["page_count"])$page_no=$page_info["page_count"];
   
   $first=($page_no-1)*$this->page_size;
   $sql="select * from ly order by id desc limit $first,$this->page_size ";
   $query=mysqli_query($this->link,$sql);
   $page_result=array("page_data"=>$query,"page_info"=>$page_info,"page_no"=>$page_no);
   return $page_result;   
}
//......省略  
}
?>

Add paging function to the front-end page

<div class="pagination" >
 当前第<?php echo $page_result["page_no"];?>页/
 共<?php echo $page_result["page_info"]["page_count"];?>页/
 每页显示<?php echo $page_result["page_info"]["page_size"]; ?>条/
 共<?php echo $page_result["page_info"]["ly_count"]; ?>条留言
<a href="index.php?pn=<?php echo ($page_result['page_no']-1);?>">上一页</a>
<a href="index.php?pn=<?php echo ($page_result['page_no']+1);?>">下一页</a>
<a href="index.php?pn=1">首页</a>
<a href="index.php?pn=<?php echo ($page_result['page_info']['page_count']);?>">尾页</a>
</div>
Continuing Learning
||
​<div class="pagination" > 当前第<?php echo $page_result["page_no"];?>页/ 共<?php echo $page_result["page_info"]["page_count"];?>页/ 每页显示<?php echo $page_result["page_info"]["page_size"]; ?>条/ 共<?php echo $page_result["page_info"]["ly_count"]; ?>条留言 <a href="index.php?pn=<?php echo ($page_result['page_no']-1);?>">上一页</a> <a href="index.php?pn=<?php echo ($page_result['page_no']+1);?>">下一页</a> <a href="index.php?pn=1">首页</a> <a href="index.php?pn=<?php echo ($page_result['page_info']['page_count']);?>">尾页</a> </div>
submitReset Code