PHP Page-Class programmer

Source: Internet
Author: User

Class Page {
Public param
Public $pageName = ' page '; Default page label, which is the pagename in filename.php?pagename=4
Public $prevPage = ' < '; Previous page
Public $nextPage = ' > '; Next page
Public $prevBar = ' << '; Previous Page Bar
Public $nextBar = ' >> '; Next Page Bar
Private param
Private $_totalnums = 1; Total number of records
Private $_barnum = 10; Pagination Display number of pages
Private $_totalpage = 0; Total pages
Private $_nowpage = 1; Current page
Private $_perpage = 10; Show number of records per page
Private $_preurl = '; URL Prefix
Private $_prenow = 0; The number of pages to display before the current page (the range should be 0 to $_barnum-1)
Private $_frompage = 0; Starting point of page-bar
Private $_topage = 0; Page Bar End
Private $_offset = 0; SQL query Record offset
Public functions-----------------------------------------------------------------
Constructor initializes a paging variable
Total records of @param $nums int
@param $per int Displays the number of records per page
@param $now int Current page
@param $url string URL prefix is null by default
Public function __construct ($nums, $per = ', $preNow = ', $barNum = ', $now = ', $url = ') {
Class
$this->_set ($nums, $per, $preNow, $barNum, $now, $url);
$this->_totalpage = ceil ($this->_totalnums/$this->_perpage);
$this->_offset = ($this->_nowpage-1) * $this->_perpage;
}
Previous page
Public Function Getprevpage () {
The current page is not the first page
if ($this->_nowpage > 1) {
return $this->_setlink ($this->_seturl ($this->_nowpage-1), $this->prevpage, ' prevpage ');
}
}
Next page
Public Function Getnextpage () {
Current page is less than total pages
if ($this->_nowpage < $this->_totalpage) {
return $this->_setlink ($this->_seturl ($this->_nowpage + 1), $this->nextpage, ' nextPage ');
}
}
First page
Public Function Getfirstpage () {
Starting point is not the first page
if ($this->_nowpage-$this->_prenow) > 1) {
return $this->_setlink ($this->_seturl (1), ' 1 ... ', ' firstpage ');
}
}
Last page
Public Function Getlastpage () {
End not greater than total number of pages
if (($this->_nowpage-$this->_prenow + $this->_barnum) <= $this->_totalpage) {
return $this->_setlink ($this->_seturl ($this->_totalpage), ' ... '. $this->_totalpage, ' lastpage ');
}
}
Previous Page Bar
Public Function Getprevbar () {
The starting point is greater than the number of page-bars
if ($this->_nowpage-$this->_prenow) > $this->_barnum) {
return $this->_setlink ($this->_seturl ($this->_nowpage-$this->_prenow)-$this->_barnum), $this- >prevbar, ' Prevbar ');
}
}
Next Page Bar
Public Function Getnextbar () {
End not greater than total number of pages
if (($this->_nowpage-$this->_prenow + $this->_barnum) <= $this->_totalpage) {
return $this->_setlink ($this->_seturl ($this->_frompage + $this->_barnum), $this->nextbar, ' Nextbar ') ;
}
}
Page-Bar
Public Function Pagebar () {
Initialize the beginning end point of the page bar
$this->_topage = $this->_nowpage + ($this->_barnum-$this->_prenow-1);
if ($this->_topage > $this->_totalpage) {
$this->_prenow = $this->_barnum-($this->_totalpage-$this->_nowpage + 1);
$this->_topage = $this->_totalpage;
}
if ($this->_topage < $this->_barnum) {
$this->_topage = $this->_barnum;
}
$this->_frompage = $this->_nowpage-$this->_prenow;
if ($this->_frompage < 1) {
$this->_frompage = 1;
}
Initializing a page-bar
$return = ';
for ($i = $this->_frompage; $i <= $this->_topage; $i + +) {
if ($i!= $this->_nowpage) {
$return. = $this->_setlink ($this->_seturl ($i), $i, ' page ');
} else {
$return. = ' <span class= ' nowpage ' > '. $i. ' </span> ';
}
}
return $return;
}
return offset for SQL query
Public Function GetOffset () {
return $this->_offset;
}
Returns the total number of records
Public Function gettotalnums () {
Return ' <span class= ' totalnums ' > '. $this->_totalnums. ' </span> ';
}
Show page pagination
Public Function ShowPage () {
return $this->gettotalnums (). $this->getfirstpage (). $this->getprevbar (). $this->getprevpage (). $this->pagebar (). $this->getnextpage (). $this->getnextbar (). $this->getlastpage ();
}
Private Functions----------------------------------------------------------------
//
Private Function _set ($nums, $per, $preNow, $barNum, $now, $url) {
Set the total number of records
if ($nums > 0) {
$this->_totalnums = $nums;
}
Set the number of records to display per page
if ($per > 0) {
$this->_perpage = $per;
}
Set the number of pages to display before the current page
if ($preNow > 0) {
$this->_prenow = $preNow;
}
Set the number of page-bar links
if ($barNum > 0) {
$this->_barnum = $barNum;
}
Set Current page
if (empty ($now)) {
Automatically get
if (Isset ($_get[$this->pagename])) {
$this->_nowpage = intval ($_get[$this->pagename]);
}
} else {
$now has been manually processed
$this->_nowpage = intval ($now); //
}
Set URL prefix
if (!empty ($url)) {
$url has been manually processed
$this->_preurl = $url. (Stristr ($url, '? ') ' & ': '? ' . $this->pagename. '=';
} else {
Automatically get
if (Empty ($_server[' query_string ')) {
Query not present in URL
$this->_preurl = $_server[' Request_uri ']. '?' . $this->pagename. '=';
} else {
if (Stristr ($_server[' query_string '), $this->pagename. '=')) {
There are page=n (2,3 ...) in the query.
$this->_preurl = Str_replace ($this->pagename. '=' . $this->_nowpage, ', $_server[' Request_uri ']);
$lastCharacter = $this->_preurl[strlen ($this->_preurl)-1];
if ($lastCharacter = = '? ' | | $lastCharacter = = ' & ') {
Page=n (2,3 ...) at the end of the original URL
$this->_preurl. = $this->pagename. '=';
} else {
Page=n (2,3 ...) is not at the end of the original URL
$this->_preurl. = ' & '. $this->pagename. '=';
}
} else {
There is no page=n (2,3 ...) in the query.
$this->preurl = $_server[' Request_uri ']. ' & '. $this->pagename. '=';
}
}
}
}
Set up a link address
Private Function _seturl ($pageNo) {
Return $this->_preurl. $pageNo;
}
Set up a link
Private Function _setlink ($url, $link, $style = ') {
$style = ' class= '. $style. '"';
Return ' <a href= '. $url. '" ' . $style. ' > '. $link. ' </a> ';
}
}
//

How to use

$total = 1245;
//class  page ($nums, $per = ', $preNow = ', $barNum = ', $now = ', $url = ')
$page = new page ($total); br> $pager = $page->showpage ();
$smarty->assign (' pager ', $pager);
$smarty->display (' Index.tpl ');

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.