Powerful PHP paging function _php Tips

Source: Internet
Author: User
Tags html tags php tutorial prev

Pagination is every program needs to understand something, learned a few languages I found that the paging principle is the same, the following for the PHP beginner analysis of the PHP paging implementation and the last side of a super powerful paging function.

There are two main ways to page the content of the article:

approach One, pagination by word control

The Word page method is easy to use, but not effective.

General idea: First, set the maximum number of words that can be accommodated per page, then, calculate the total number of words in the article, and then calculate the total number of pages by the total number of words and the maximum number of single pages, so that the entire paging preparation work has been done.

Specific to the display of each page can be achieved through the content interception, such as: the page to accommodate 500 words, article content has 2200 words, then when the page passes page=2 should show the content between the No. 501 to 1000th.

This approach is simple, but the display may encounter trouble, the article is usually accompanied by HTML tags, content cutting to achieve the closure of the HTML tag has a certain degree of difficulty, if the job is not done, then the effect of paging is obviously not good.

Approach II, Paging through page breaks

Paging through page breaks is more desirable than the first approach.

General idea: When editing the content of the content to insert page breaks (such as:

This approach is more humane, after all, through the manual control of the content of the interception of the whole of our thinking, but also to some extent to avoid the HTML tag closed situation.

Pagination display: paging display is one of the important means to send large quantities of data to the client in batches, usually it is to display the result set of the database into a paragraph.

Category: PHP pagination is divided into list pagination and content pagination, whether it is a list page or content paging, the basic principle is the same, the data sent to the client in batches.

Pager.class.php: This is a simple PHP page display class, currently supports two paging mode, one is the simplest common paging mode [home] [prev] [Next page] [last] mode, another is the classic paging mode, namely : [1][2][3][4][5][6] [7] [8] [9] [10] [Next Page] [Last].

Using the method, the code is as follows:

Require_once ' pager.class.php '; 
$pager = new Pager ($totalPage, $currentPage); $pager Object 
echo $pager->showpager ()//Output Paging 
This page shows the constructor/ 
* @total_page Total number of pages in the class 
@current_num The current page @sub_pages the number 
of pages per display 
@subPage_link the link for each page 
@subPage_type paging mode 
when @subpage_type=1 for normal paging mode 
such as: Total 4,523 records, each page shows 10, the current 1th 453 page [first] [prev] [Next page] [last] 
when @subpage_type=2 for the classic page style 
such as: Current 1th/453 page [first] [prev] 1 2 3 4 5 6 7 8 9 10 [next page] [last] 

The code is as follows:

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 pagination), I believe that the list page is not unfamiliar to everyone, for content paging, the common method is in the form of page breaks (for example: ) splits the content into segments, gets the total number of pages, and takes the current page to get the paging display list, which is as follows:

<?php/** * Example: * <?php * require_once ("pager.class.php"); 
 * $subPages =new Pager ($totalPage, $currentPage); 
 * Echo $subPages->showpager (); *?> */class pager{var $each _disnums;//The number of entries displayed per page var $nums;//Total number of entries Var $current _page;//the currently selected page Var $sub _pa ges;//the number of pages per page var $pageNums//Total pages var $page _array = Array ();//the array var $subPage to construct the pagination _link;//the link var $subPage for each page _t ype;//display paging type var $_lang = array (' index_page ' => ' home ', ' pre_page ' => ' prev ', ' next_page ' => ' next page ', ' Las T_page ' => ' End ', ' current_page ' => ' current page: ', ' total_page ' => ' total pages: ', ' current_show ' => ' is currently displayed: ', ' Total_r 
 Ecord ' => ' total record number: '); 
 /* __construct is a subpages constructor that is used to run automatically when you create a class. @total_page total number of pages @current_num the currently selected page @sub_pages the number of pages displayed each time @subPage_link each page link @subPage_type display the type of pagination when @subpage_ 
 Type=1 when the www.phpfensi.com for ordinary paging mode example: Total 4,523 records, each page shows 10, the current 1th 453 page [first] [prev] [Next page] [last] when @subpage_type=2 for classic pagination style Example: Current 1th 453 page [first] [prev]1 2 3 4 5 6 7 8 9 10 [next page] [last] * * function __construct ($total _page, $current _page, $sub _pages=10, $subPage _link= ", $subPa 
 ge_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) {$t 
 his->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) {$sub Page_link. = (Strpos ($subPage _link, '? ') = = False? '? ': ' & '). 
 ' Page= '; }} $this->subpage_link= $subPage _link? $subPage _link: $_server[' php_self ']. 
 ' Page= '; 
 $this->subpage_type = $subPage _type; }/* Show_subpages function is used in constructors. And to determine what the display of the paging/function Showpager () {if ($this->subpage_type = = 1) {return $this->pagelist1 (); 
 }elseif ($this->subpage_type = = 2) {return $this->pagelist2 (); 
 }/* The function used to initialize the array of pagination. 
 */function Initarray () {for ($i =0; $i < $this->sub_pages; $i + +) {$this]= $i->page_array[$i; 
 return $this->page_array; 
 }/* Construct_num_page The function is used to construct the displayed entry even if: [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]= +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; 
 }/* Constructs common Mode page Total 4,523 records, each page shows 10, the current 1th 453 page [first] [prev] [Next page] [last]/function Pagelist1 () {$subPageCss 1str= ""; $subPageCss 1str.= $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); 
 $subPageCss 1str.= "<a href= ' $firstPageUrl ' >{$this->_lang[' index_page ']}</a> '; 
 $subPageCss 1str.= "<a href= ' $prewPageUrl ' >{$this->_lang[' pre_page ']}</a> '; 
 }else {$subPageCss 1str.= "{$this->_lang[' index_page ']}"; 
 $subPageCss 1str.= "{$this->_lang[' pre_page ']}"; 
 } if ($this->current_page < $this->pagenums) {$lastPageUrl = $this->subpage_link. $this->pagenums; $NEXTPAGEURL = $this->subpage_link. 
 ($this->current_page+1); $subPageCss 1str.= "<a HRef= ' $nextPageUrl ' >{$this->_lang[' next_page ']}</a> '; 
 $subPageCss 1str.= "<a href= ' $lastPageUrl ' >{$this->_lang[' last_page ']}</a> '; 
 }else {$subPageCss 1str.= "{$this->_lang[' next_page ']}"; 
 $subPageCss 1str.= "{$this->_lang[' last_page ']}"; 
 return $SUBPAGECSS 1Str; }/* Construction Classic mode pagination current 1th 453 page [first] [prev] 1 2 3 4 5 6 7 8 9 10 [next page] [last] * function Pagelist2 () {$subPageCss 2str= 
 ""; $subPageCss 2str.= $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); 
 $subPageCss 2str.= "<a href= ' $firstPageUrl ' >{$this->_lang[' index_page ']}</a> '; 
 $subPageCss 2str.= "<a href= ' $prewPageUrl ' >{$this->_lang[' pre_page ']}</a> '; 
 }else {$subPageCss 2str.= "{$this->_lang[' index_page ']}"; 
 $subPageCss 2str.= "{$this->_lang[' pre_page ']}"; }
 
 $a = $this->construct_num_page (); 
 For ($i =0 $i <count ($a); $i + +) {$s = $a [$i]; if ($s = = $this->current_page) {$subPageCss 2str.= "[<span style= ' color:red;font-weight:bold;] > ". $s." 
 </span>] "; 
 }else{$url = $this->subpage_link. $s; $subPageCss 2str.= "[<a href= ' $url ' >". $s. " 
 </a>] "; 
 } if ($this->current_page < $this->pagenums) {$lastPageUrl = $this->subpage_link. $this->pagenums; $NEXTPAGEURL = $this->subpage_link. 
 ($this->current_page+1); 
 $subPageCss 2str.= "<a href= ' $nextPageUrl ' >{$this->_lang[' next_page ']}</a> '; 
 $subPageCss 2str.= "<a href= ' $lastPageUrl ' >{$this->_lang[' last_page ']}</a> '; 
 }else {$subPageCss 2str.= "{$this->_lang[' next_page ']}"; 
 $subPageCss 2str.= "{$this->_lang[' last_page ']}"; 
 return $SUBPAGECSS 2Str; 
 }/* __destruct destructor, which is used to release resources when the class is not in use. 
 * * Function __destruct () {unset ($each _disnums); 
 Unset ($nums); 
 unset ($current _page); Unset ($sUb_pages); 
 Unset ($pageNums); 
 unset ($page _array); 
 unset ($subPage _link); 
 unset ($subPage _type); 
 }}?>

All right, all we have to do is save it in a file, just call it when you want to use it somewhere else, it's very handy. A content or record paging class function Oh, this is an entry-level PHP tutorial.

Wonderful topic Sharing: PHP paging function

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.