亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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" >
 當(dāng)前第<?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" > 當(dāng)前第<?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