Classic PHP Pagination Class-PHP General Page class

Source: Internet
Author: User
Tags constructor prev


<?php
Class my_lib_pages{

Private $each _disnums;//The number of entries displayed per page
private $nums;//Total number of entries
var $current _page;//the currently selected page
Private $sub _pages;//The number of pages per display
private $pageNums;//Total pages
Private $page _array = Array ();//arrays used to construct pagination
Private $subPage _link;//links to each page
Private $subPage _type;//Display the type of paging
/*
__construct is a subpages constructor that runs automatically when you create a class.
@ $each _disnums The number of entries per page
Total number of @nums entries
@current_num the currently selected page
@sub_pages number of pages per display
@subPage_link links to each page
@subPage_type display the type of pagination

When @subpage_type=1 for normal paging mode
Example: Total 4,523 records, each page shows 10, the current 1th 453 page [first] [prev] [Next page] [last page]
The classic pagination style when @subpage_type=2
Example: Current 1th 453 page [first] [prev] 1 2 3 4 5 6 7 8 9 10 [next page] [last]
*/
function __construct ($each _disnums, $nums, $current _page, $sub _pages, $subPage _link, $subPage _type) {
$this->each_disnums=intval ($each _disnums);
$nums = $nums ==0? 1: $nums;
$this->nums=intval ($nums);
$this->pagenums=ceil ($nums/$each _disnums);
$this->current_page=intval ($current _page);
$this->current_page= $this->current_page<=0? 1: $this->current_page;
$this->current_page= $this->current_page > $this->pagenums? 1: $this->current_page;
$this->sub_pages=intval ($sub _pages);
$this->subpage_link= $subPage _link;
$this->show_subpages ($subPage _type);
echo $this->pagenums. " --". $this->sub_pages;
}


/*
__destruct a destructor that is invoked when a class is not in use, which 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);
}

/*
The Show_subpages function is used inside the constructor. And it's used to determine what kind of paging is displayed.
*/
function show_subpages ($subPage _type) {
if ($subPage _type = = 1) {
$this->subpagecss1 ();
}elseif ($subPage _type = = 2) {
$this->subpagecss2 ();
}
}


/*
The function used to initialize the array of pagination.
*/
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: [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;
}

/*
To construct a common mode paging
Total 4,523 records, each page shows 10, the current 1th 453 page [first] [prev] [Next page] [last page]
*/
function SubPageCss1 () {
$subPageCss 1str= "";
$subPageCss 1str.= "altogether". $this->nums. " Record, ";
$SUBPAGECSS 1str.= "Display per page". $this->each_disnums. " Article, ";
$subPageCss 1str.= "Current section" $this->current_page. " /". $this->pagenums." Page ";
if ($this->current_page > 1) {
$FIRSTPAGEURL = $this->subpage_link. " 1 ";
$PREWPAGEURL = $this->subpage_link. ($this->current_page-1);
$subPageCss 1str.= "[<a href= ' $firstPageUrl ' > Home </a>]";
$subPageCss 1str.= "[<a href= ' $prewPageUrl ' > previous page </a>]";
}else {
$subPageCss 1str.= "[Home]";
$subPageCss 1str.= "[prev]";
}

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 ' > Next page </a>]";
$subPageCss 1str.= "[<a href= ' $lastPageUrl ' > Last </a>]";
}else {
$subPageCss 1str.= "[next page]";
$subPageCss 1str.= "[Last]";
}

return $SUBPAGECSS 1Str;

}


/*
To construct a paging of Classic mode
Current 1th 453 page [first] [prev] 1 2 3 4 5 6 7 8 9 10 [next page] [last]
*/
function SubPageCss2 () {
$subPageCss 2str= "";
$subPageCss 2str.= "Current <span style=color:red;>". $this->current_page. " </span>/". $this->pagenums." Page ";


if ($this->current_page > 1) {
$FIRSTPAGEURL = $this->subpage_link. " 1 ";
$PREWPAGEURL = $this->subpage_link. ($this->current_page-1);
$subPageCss 2str.= "<a href= ' $firstPageUrl ' > Home </a>";
$subPageCss 2str.= "<a href= ' $prewPageUrl ' ><<</a>";
}else {
$subPageCss 2str.= "<span class=disabled> home </span>";
$subPageCss 2str.= "<span class=disabled><<</span>";
}

$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.= "<span class=current><a href= ' $url ' >". $s. " </a></span> ";
}
}
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 ' >>></a>";
$subPageCss 2str.= "<a href= ' $lastPageUrl ' > Last </a>";
}else {
$subPageCss 2str.= "<span class=disabled>>></span>";
$subPageCss 2str.= "<span class=disabled> last </span>";
}
return $SUBPAGECSS 2Str;
}
}
?>

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.