PHP pagination class (imitate Google)-interview topics Answer _php tips

Source: Internet
Author: User
Tags prev
The written answer is not very good, especially the JS part, but also for a long time did not review the reason.
On the machine is to write a copy of Google pagination class, when to take a similar 9/2 of the largest integer, but how can not think of functions Ceil name, dizzy for half a day.
The final test program is not wrong, but is not normal display, later (after going home) a check to know is the statement: for ($i =0; $i + +; $i <9) wrote wrong, so determined to rewrite it, so there is the following code:
Copy Code code as follows:

<?php
/*
The display style is as follows:
[1] 2 3 4 5 6 7 8 9 ... 100 Next Last Page
First prev 1..12 13 14 15 [16] 17 18 19 20 ... 100 Next Last Page
First Prev 1..92 93 94 95 96 97 98 [99] 100

How to use:
$currentPage = $_get[' page ']?$_get[' page ']:1;
$pagediv = new Pagediv (A, $currentPage, ' test.php?page= ');
$pagediv->show ();

*/
Class Pagediv
{
Public $part 1;
Public $part 2;
Public $part 3;
Public $part 4;
Public $part 5;

/*
Split the following paging display:
First prev 1..12 13 14 15 [16] 17 18 19 20 ... 100 Next Last Page
$part 1: Home prev
$part 2:1.
$part 3:12 13 14 15 [16] 17 18 19 20
$part 4:. 100
$part 5: Next Last
*/

Public $allPage; Total pages
Public $allRocords; Total number of records
Public $perPage; Number of records per page
Public $showPagesNo; Displays the total number of pages in the page bar there are 11 display styles
Public $currentPage; Current page
Public $urlModel; URL link Style

Public $startHidden; Appeared 1 ... When the number of pages begins to hide the middle page
Public $endHidden; Appear... Number of pages at 100 End Hide Middle page

Public function __construct ($allRocords, $perPage, $showPagesNo, $currentPage, $urlModel) {
$this->allrocords = $allRocords;
$this->perpage = $perPage;
$this->showpagesno = $showPagesNo;
$this->currentpage = $currentPage;
$this->urlmodel = $urlModel;
$this->allpage = $this->getallpage ();

$this->starthidden = $this->getint ($this->showpagesno)/2); 6
$this->endhidden = $this->allpage-$this->starthidden; 94
}

Public Function getUrl ($_index = ') {
$_current = $_index;
if ($_index = = ' pre ') $_current = $this->currentpage-1;
if ($_index = = ' Next ') $_current = $this->currentpage+1;
if ($_index = = ") $_current = $this->allpage;
return $this->urlmodel.$_current;
}

Public Function Getallpage () {
return $this->getint ($this->allrocords/$this->perpage);
}

Public Function GetInt ($_float) {
$_int = $_float;
if ($_index = Strpos ($_float, '. ') = = True) {
$_int = substr ($_float,0,$_index);
$_int++;
}
I didn't think of Ceil when I was waiting.
return $_int;
}

Public Function GetPart1 () {
$content = ' <a href= '. $this->geturl (1). ' " > Home </a> <a href= "'. $this->geturl (' pre ')." > Prev </a> ';
if ($this->currentpage <= $this->starthidden) {
$content = ';
}
return $content;
}

Public Function GetPart2 () {
$content = ' <a href= '. $this->geturl (1). ' " >1</a> ';
$add = ';
if ($this->currentpage > $this->starthidden) {
$add = ' ... ';
}
if ($this->currentpage = = 1) {
$content = ' [1] ';
$add = ';
}
$part 2 = $content. $add;
return $part 2;
}

Public Function GetPart3 () {
$content = ';
if ($this->currentpage <= $this->starthidden) {
[1] 2 3 4 5 6 7 8 9 ... 100 Next Last Page
$long = $this->showpagesno-2;
for ($i =0; $i < $long; $i + +) {
$j = $i +2;
if ($j = = $this->currentpage) {
$content. = ' ['. $this->currentpage. '] ';
}else{
$content. = ' <a href= '. $this->geturl ($j). ' " > '. $j. ' </a> ';
}

}

}elseif ($this->currentpage >= $this->endhidden) {
First Prev 1..92 93 94 95 96 97 98 [99] 100
$long = $this->showpagesno-2;
$_start = $this->allpage-$long;
for ($i =0; $i < $long; $i + +) {
$j = $_start + $i;
if ($j = = $this->currentpage) {
$content. = ' ['. $this->currentpage. '] ';
}else{
$content. = ' <a href= '. $this->geturl ($j). ' " > '. $j. ' </a> ';
}
}
}else{
First prev 1..12 13 14 15 [16] 17 18 19 20 ... 100 Next Last Page
$long = $this->showpagesno-2;
$offset = $this->getint ($long/2)-1;
$_start = $this->currentpage-$offset;
for ($i =0; $i < $long; $i + +) {
$j = $_start + $i;
if ($j = = $this->currentpage) {
$content. = ' ['. $this->currentpage. '] ';
}else{
$content. = ' <a href= '. $this->geturl ($j). ' " > '. $j. ' </a> ';
}
}
}
$part 3 = $content;
return $part 3;
}

Public Function GetPart4 () {
$content = ' <a href= '. $this->geturl (). ' " > '. $this->allpage. ' </a> ';
$add = ';
if ($this->currentpage < $this->endhidden) {
$add = ' ... ';
}
if ($this->currentpage = = $this->allpage) {
$content = ' [; $this->allpage. '] ';
$add = ';
}
$part 4 = $add. $content;
return $part 4;

}

Public Function GetPart5 () {
$content = ' <a href= '. $this->geturl (' Next '). " > next page </a> <a href= "'. $this->geturl (). '" > Last </a> ';
if ($this->currentpage >= $this->endhidden) {
$content = ';
}
return $content;
}

Public function Show () {
Judged illegal
if (!is_numeric ($this->currentpage) | | $this->currentpage < 0 | | $this->currentpage > $this->allpage) {
print ' Error:pageno is flase ';
Return
}
The total number of pages does not reach the total number of pages that display the page bar, all display
if ($this->allpage < $this->showpagesno) {
$long = $this->allpage;
for ($i =0; $i < $long; $i + +) {
$j = $i +1;
if ($j = = $this->currentpage) {
$content. = ' ['. $this->currentpage. '] ';
}else{
$content. = ' <a href= '. $this->geturl ($j). ' " > '. $j. ' </a> ';
}

}
Print $content;
Return
}
$this->part1 = $this->getpart1 ();
$this->part2 = $this->getpart2 ();
$this->part3 = $this->getpart3 ();
$this->part4 = $this->getpart4 ();
$this->part5 = $this->getpart5 ();

Print $this->part1. $this->part2. $this->part3. $this->part4. $this->part5;
}
}
?>

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.