Home>

Paging is something that every program needs to understand,I have found that the principle of paging is the same in several languages ​​I have studied.The following is a php beginner's analysis of php paging implementation and added a super powerful paging function at the end.

There are two main methods for pagination of article content:

Method one,Pagination by word count control

Paging by words is simple and easy to use,But the effect is not good.

The general idea:First, set the maximum number of words that each page can hold.Then, calculate the total number of words in the article content,Then calculate the total number of pages from the total number of words and the maximum number of words on a single page.In this way, the preparation for the entire paging is completed.

The display content specific to each page can be achieved by content interception.For example:the page holds 500 words, and the content of the article has 2200 words.When the page passes page=2, the content between 501 and 1000 should be displayed.

This approach is simple,But you may have trouble displaying it,Article content is usually accompanied by html tags.It is difficult to close the html tags when cutting content.If this job is not done well,Then the effect after pagination is obviously not good.

Method two,Pagination by page break

Pagination by page breaks is better than the first method.

The general idea:When editing the content of the article, insert page breaks into the content (such as:


), and divide the content of the article when the article is displayed.Each section represents the content of a page,Pass the parameters to control which page is displayed.

This approach is more humane,After all, the content intercepted by manual control is more in line with our thinking.And can avoid the situation that html tags are not closed to some extent.

Pagination display:Paging display is one of the important means of distributing large quantities of data to clients.The result set in the database is usually artificially divided into sections to display.

Category:php pagination is divided intoList paginationandContent paginationWhether it ’s a list or content tab,The basic principle is the same,Distribute data to clients.

pager.class.php:This is a simple php pagination display class,Currently supports two paging modes,One is the simplest common paging mode [First] [Previous] [Next] [Last], and the other is the classic paging mode.Ie:[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [Next] [Last].

Instructions,code show as below:

require_once "pager.class.php";
$pager=new pager ($totalpage, $currentpage);//$pager object
echo $pager->showpager ();//output paging
This pagination shows the constructor of the class
/*
@total_page Total pages
@current_num current page
@sub_pages pages displayed at a time
@subpage_link link for each page
@subpage_type pagination mode
[email protected]_type=1, it is ordinary paging mode
Such as:A total of 4523 records, each page shows 10 records, the current 1/453 page [First] [Previous] [Next] [Last]
[email protected]_type=2 is the classic pagination style
Ex:Current page 1/453 [First] [Previous] 1 2 3 4 5 6 7 8 9 10 [Next] [Last]
* /

code show as below:

pager ($total_page, $current_page, $sub_pages=10, $subpage_link="", $subpage_type=2)

The two categories of php paging mentioned above (list paging and content paging), I believe that list paging is no stranger to everyone,For content paging,A common method is in the form of page breaks (for example:<hr>) split the content into multiple paragraphs,Find the total number of pages,Use the current page number to get the pagination display list,code show as below:

<?php
/**
 * Example:
 *<?php
 * require_once ("pager.class.php");
 * $subpages=new pager ($totalpage, $currentpage);
 * echo $subpages->showpager ();
 *?>
 * * /
class pager {
 var $each_disnums;//Number of entries displayed per page
 var $nums;//Total number of entries
 var $current_page;//The currently selected page
 var $sub_pages;//Number of pages displayed each time
 var $pagenums;//Total number of pages
 var $page_array=array ();//used to construct a paging array
 var $subpage_link;//The link of each page
 var $subpage_type;//Display the type of pagination
 var $_lang=array (
 "index_page" =>"Home", "pre_page" =>"Previous Page", "next_page" =>"Next Page", "last_page" =>"Last Page", "current_page" =>"Current page:", "total_page" =>"Total pages:", "current_show" =>"Currently show:", "total_record" =>"Total Records:"
 );
 /*
 __construct is the constructor for subpages,Used to run automatically when a class is created.
 @total_page Total pages
 @current_num currently selected page
 @sub_pages pages displayed at a time
 @subpage_link link for each page
 @subpage_type shows the type of pagination
 [email protected]_type=1, www.phpfensi.com is ordinary paging mode
 example : A total of 4523 records, each page shows 10 records, the current 1/453 page [First] [Previous] [Next] [Last]
 [email protected]_type=2 is the classic pagination style
 example : Current page 1/453 [First] [Previous] 1 2 3 4 5 6 7 8 9 10 [Next] [Last]
 * /
 function __construct ($total_page, $current_page, $sub_pages=10, $subpage_link="", $subpage_type=2) {
 $this->pager ($total_page, $current_page, $sub_pages, $subpage_link, $subpage_type);
 }
 function pager ($total_page, $current_page, $sub_pages=10, $subpage_link="", $subpage_type=2) {
 if (! $current_page) {
 $this->current_page=1;
 } else {
 $this->current_page=intval ($current_page);
 }
 $this->sub_pages=intval ($sub_pages);
 $this->pagenums=ceil ($total_page);
 if ($subpage_link) {
 if (strpos ($subpage_link, "?page =") === false and strpos ($subpage_link, "&page =") === false) {
 $subpage_link.=(strpos ($subpage_link, "?") === false?"?":"&"). "page =";
 }
 }
 $this->subpage_link=$subpage_link?$subpage_link:$_server ["php_self"]. "?page =";
 $this->subpage_type=$subpage_type;
 }
 /*
 The show_subpages function is used inside the constructor.
Also used to determine what tabs are displayed
 * /
 function showpager () {
 if ($this->subpage_type == 1) {
 return $this->pagelist1 ();
 } elseif ($this->subpage_type == 2) {
 return $this->pagelist2 ();
 }
 }
 /*
 A function used to initialize a paged array.
 * /
 function initarray () {
 for ($i=0;$i<$this->sub_pages;$i ++) {
 $this->page_array [$i]=$i;
 }
 return $this->page_array;
 }
 /*
 construct_num_page This function is used to construct the displayed entries
 Even:[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
 * /
 function construct_num_page () {
 if ($this->pagenums<$this->sub_pages) {
 $current_array=array ();
 for ($i=0;$i<$this->pagenums;$i ++) {
 $current_array [$i]=$i + 1;
 }
 } else {
 $current_array=$this->initarray ();
 if ($this->current_page<= 3) {
 for ($i=0;$i<count ($current_array);$i ++) {
 $current_array [$i]=$i + 1;
 }
 } elseif ($this->current_page<= $this->pagenums&&$this->current_page>$this->pagenums-$this->sub_pages + 1
 for ($i=0;$i<count ($current_array);$i ++) {
 $current_array [$i]=($this->pagenums)-($this->sub_pages) + 1 + $i;
 }
 } else {
 for ($i=0;$i<count ($current_array);$i ++) {
 $current_array [$i]=$this->current_page-2 + $i;
 }
 }
 }
 return $current_array;
 }
 /*
 Construct paging in normal mode
 A total of 4523 records, each page shows 10 records, the current 1/453 page [First] [Previous] [Next] [Last]
 * /
 function pagelist1 () {
 $subpagecss1str="";
 $subpagecss1str.=$this->_lang ["current_page"]. $this->current_page. "/". $this->pagenums. "";
 if ($this->current_page>1) {
 $firstpageurl=$this->subpage_link. "1";
 $prewpageurl=$this->subpage_link. ($this->current_page-1);
 $subpagecss1str.="<a href =" $firstpageurl ">{$this->_lang [" index_page "]}</a>";
 $subpagecss1str.="<a href =" $prewpageurl ">{$this->_lang [" pre_page "]}</a>";
 } else {
 $subpagecss1str.="{$this->_lang [" index_page "]}";
 $subpagecss1str.="{$this->_lang [" pre_page "]}";
 }
 if ($this->current_page<$this->pagenums) {
 $lastpageurl=$this->subpage_link. $this->pagenums;
 $nextpageurl=$this->subpage_link. ($this->current_page + 1);
 $subpagecss1str.="<a href =" $nextpageurl ">{$this->_lang [" next_page "]}</a>";
 $subpagecss1str.="<a href =" $lastpageurl ">{$this->_lang [" last_page "]}</a>";
 } else {
 $subpagecss1str.="{$this->_lang [" next_page "]}";
 $subpagecss1str.="{$this->_lang [" last_page "]}";
 }
 return $subpagecss1str;
 }
 /*
 Constructing Classic Mode Paging
 Current Page1/453 [First] [Previous] 1 2 3 4 5 6 7 8 9 10 [Next] [Last]
 * /
 function pagelist2 () {
 $subpagecss2str="";
 $subpagecss2str.=$this->_lang ["current_page"]. $this->current_page. "/". $this->pagenums. "";
 if ($this->current_page>1) {
 $firstpageurl=$this->subpage_link. "1";
 $prewpageurl=$this->subpage_link. ($this->current_page-1);
 $subpagecss2str.="<a href =" $firstpageurl ">{$this->_lang [" index_page "]}</a>";
 $subpagecss2str.="<a href =" $prewpageurl ">{$this->_lang [" pre_page "]}</a>";
 } else {
 $subpagecss2str.="{$this->_lang [" index_page "]}";
 $subpagecss2str.="{$this->_lang [" pre_page "]}";
 }
 $a=$this->construct_num_page ();
 for ($i=0;$i<count ($a);$i ++) {
 $s=$a [$i];
 if ($s == $this->current_page) {
 $subpagecss2str.="[<span>". $s. "</span>]";
 } else {
 $url=$this->subpage_link. $s;
 $subpagecss2str.="[<a href =" $url ">". $s. "<lt
 }
 }
 if ($this->current_page<$this->pagenums) {
 $lastpageurl=$this->subpage_link. $this->pagenums;
 $nextpageurl=$this->subpage_link. ($this->current_page + 1);
 $subpagecss2str.="<a href =" $nextpageurl ">{$this->_lang [" next_page "]}</a>";
 $subpagecss2str.="<a href =" $lastpageurl ">{$this->_lang [" last_page "]}</a>";
 } else {
 $subpagecss2str.="{$this->_lang [" next_page "]}";
 $subpagecss2str.="{$this->_lang [" last_page "]}";
 }
 return $subpagecss2str;
 }
 /*
 __destruct destructor,Called when the class is not in use,This function is used to release resources.
 * /
 function __destruct () {
 unset ($each_disnums);
 unset ($nums);
 unset ($current_page);
 unset ($sub_pages);
 unset ($pagenums);
 unset ($page_array);
 unset ($subpage_link);
 unset ($subpage_type);
 }
}
?>

OK, we just save it in a file,It can be called directly when it is used elsewhere,A very useful content or record paging class function,This is also an entry level PHP tutorial.

php
  • Previous PHP disable function settings and viewing methods
  • Next In-depth analysis of the wording of the search box