In-depth analysis of PHP article content page
There are two main approaches to article content paging:
Method one, according to the word control for paging
Page by word method is easy to use, but the effect is not good.
General idea: First, set the maximum number of words per page, then, calculate the total number of words in the article, and then the total number of words and the maximum number of pages to calculate the total number of pages. So the whole page is ready for the job.
Specific to each page of the display can be achieved through content interception. For example: page accommodates 500 words, the article content has 2200 words, then when the page passes page=2 should display the content between the No. 501 to 1000th.
This method is simple, but the display may encounter trouble, the article content is usually accompanied by HTML tags, the content is cut to achieve the closure of the HTML tag is a certain difficulty, if the work is not done, then the effect of paging is obviously not good.
Method Two, paging through the page break
Paging through page breaks is more desirable than the first approach.
General idea: Insert page breaks into content when editing the content of the article (for example:
), the content of the article is divided when the article is displayed, each part represents the content of a page, and the page is displayed by passing parameter control.
This approach is more humane, after all, through the manual control of the content of the interception of the whole of our thinking, and to a certain extent, to avoid the HTML tag does not close the situation.
Page display
Paging is one of the most important ways to send large batches of data to the client, usually by dividing the result set in a database into a segment.
Classification
PHP pagination is divided into list pagination and content paging, whether it is a list of pagination or content paging, the rationale is the same, the data sent to the client in batches.
pager.class.php
This is a simple PHP paging display class, currently supports two paging modes. One is the simplest normal paging mode [home] [prev] [next] [last] mode, the other is the classic paging mode, that is: [1][2][3][4][5][6][7][8][9][10][next] [last].
How to use
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 of the class /* @total_page Total Pages @current_num Current Page @sub_pages number of pages per display @subPage_link links for each page @subPage_type Paging mode |
When @subpage_type=1 is the normal paging mode
Such as: A total of 4,523 records, each page shows 10, the current 1th/453 page [first] [prev] [next] [last]
When @subpage_type=2 is a classic page-out style
such as: current 1th 453 page [first] [prev] 1 2 3 4 5 6 7 8 9 10 [next] [last]
*/
The code is as follows |
|
Pager ($total _page, $current _page, $sub _pages=10, $subPage _link= ", $subPage _type=2) |
The above mentioned PHP pagination of two categories (list pagination and content paging), believe that the list page is not unfamiliar to everyone, for content paging, the common method is in the form of page breaks (such as:
Divide the content into multiple segments, find the total number of pages, and use the current page numbers to get a paginated display list.
The code is as follows |
|
/** Example * * require_once ("pager.class.php"); * $subPages =new Pager ($totalPage, $currentPage); * Echo $subPages->showpager (); *?> * */ Class pager{ var $each _disnums;//The number of entries per page var $nums;//Total number of entries var $current _page;//the currently selected page var $sub _pages;//pages per display var $pageNums;//Total pages var $page _array = Array ();//the array used to construct pagination var $subPage _link;//links for each page var $subPage _type;//shows the type of paging var $_lang = Array ( ' Index_page ' = ' home ', ' Pre_page ' = ' prev ', ' Next_page ' = ' next page ', ' Last_page ' = ' last ', ' Current_page ' = ' current page: ', ' Total_page ' = ' total pages: ', ' Current_show ' = ' now shows: ', ' Total_record ' = ' Total records: ' ); /* __construct is a subpages constructor that is used to run automatically when a class is created. @total_page Total Pages @current_num the currently selected page @sub_pages number of pages per display @subPage_link links for each page @subPage_type show the type of paging When @subpage_type=1 is the normal paging mode Example: A total of 4,523 records, each page shows 10, the current 1th/453 page [first] [prev] [next] [last page] When @subpage_type=2 is a classic page-out style Example: Current 1th 453 page [first] [prev] 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. and used to determine what kind of paging to show. */ 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 that created the paging. */ function Initarray () { for ($i =0; $i < $this->sub_pages; $i +) { $this->page_array[$i]= $i; } return $this->page_array; } /* Construct_num_page the function 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]= $i +1; } }else{ $current _array= $this->initarray (); if ($this->current_page <= 3) { for ($i =0; $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 <> $current _array[$i]= ($this->pagenums)-($this->sub_pages) +1+ $i; } }else{ for ($i =0; $i <> $current _array[$i]= $this->current_page-2+ $i; } } } return $current _array; } /* Constructing the paging of normal mode Total 4,523 records, per page 10 article, current 1th/453 page [first] [prev] [Next] [end page] */ 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.= "{$this->_lang[' index_page '}"; $subPageCss 1str.= "{$this->_lang[' pre_page '}"; }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.= "{$this->_lang[' next_page '}"; $subPageCss 1str.= "{$this->_lang[' last_page '}"; }else { $subPageCss 1str.= "{$this->_lang[' next_page '}"; $subPageCss 1str.= "{$this->_lang[' last_page '}"; } return $SUBPAGECSS 1Str; } /* Www.111cn.net pagination for constructing Classic mode Current 1th/453 [first] [prev] 1 2 3 4 5 6 7 8 9 10 [next] [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.= "{$this->_lang[' index_page '}"; $subPageCss 2str.= "{$this->_lang[' pre_page '}"; }else { $subPageCss 2str.= "{$this->_lang[' index_page '}"; $subPageCss 2str.= "{$this->_lang[' pre_page '}"; } $a = $this->construct_num_page (); for ($i =0; $i <> $s = $a [$i]; if ($s = = $this->current_page) { $subPageCss 2str.= "[". $s."]"; }else{ $url = $this->subpage_link. $s; $subPageCss 2str.= "[". $s. "]"; } } if ($this->current_page < $this->pagenums) { $LASTPAGEURL = $this->subpage_link. $this->pagenums; $NEXTPAGEURL = $this->subpage_link. ($this->current_page+1); $subPageCss 2str.= "{$this->_lang[' next_page '}"; $subPageCss 2str.= "{$this->_lang[' last_page '}"; }else { $subPageCss 2str.= "{$this->_lang[' next_page '}"; $subPageCss 2str.= "{$this->_lang[' last_page '}"; } return $SUBPAGECSS 2Str; } /* The __destruct destructor is called when the class is not in use, and the function is used to free 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); } } ?> |
http://www.bkjia.com/PHPjc/834976.html www.bkjia.com true http://www.bkjia.com/PHPjc/834976.html techarticle in-depth analysis of the contents of the PHP article content pagination There are two main ways: method one, according to the word control to page by Word paging method is simple and easy to use, but the effect is not good. General Idea ...