PHP paging class imitates google-interview question answers

WBOY
Release: 2016-07-29 08:40:47
Original
1087 people have browsed it

I didn’t answer the written test very well, especially the JS part, which is why I haven’t reviewed it for a long time.
The computer question is to write a class that imitates Google's paging. When I want to get the largest integer like 9/2, I can't remember the name of the function ceil, and I was confused for a long time.
In the end, there was no error in the test program, but it just couldn’t be displayed normally. After checking it later (after returning home), I found out that the statement: for($i=0;$i++;$i<9) was wrong, so I decided to rewrite it. Once, we have the following code:

Copy the code The code is as follows:


/*
The display style is as follows:
[1] 2 3 4 5 6 7 8 9 10 ...100 Next page Last page
Home page Previous page 1..12 13 14 15 [16] 17 18 19 20 ...100 Next page Last page
Home page Previous page 1..92 93 94 95 96 97 98 [99] 100
Usage:
$currentPage = $_GET['page']?$_GET['page ']:1;
$pagediv = new pagediv(500, 10, 11, $currentPage, 'test.php?page=');
$pagediv->show();
*/
class pagediv
{
public $part1;
public $part2;
public $part3;
public $part4;
public $part5;
/*
Split the following paging display:
Home Page Previous Page 1..12 13 14 15 [16] 17 18 19 20 ...100 Next page Last page
$part1 : Home page Previous page
$part2 : 1..
$part3 : 12 13 14 15 [16] 17 18 19 20
$part4 : ...100
$part5: Next page and last page
*/
public $allPage; //Total number of pages
public $allRocords; //Total number of records
public $perPage; //Number of records per page
public $showPagesNo; //Show paging There are 11 total page numbers in the column display style
public $currentPage; //Current page
public $urlModel; //Url link style
public $startHidden; //The number of pages when 1... starts to hide the middle page
public $endHidden; //The page number ends when...100 appears to hide the 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++;
}
//Alternative solution when ceil is not remembered
return $_int;
}
public function getPart1(){
$content = 'Homepage Previous page ';
if($this->currentPage <= $this->startHidden){
$content = '';
}
return $content;
}
public function getPart2(){
$content = '1 ';
$add = '';
if($this->currentPage > $this->startHidden ){
$add = '...';
}
if($this->currentPage == 1){
$content = '[1] ';
$add = '';
}
$part2 = $content.$add;
return $part2;
}
public function getPart3(){
$content = '';
if($this->currentPage <= $this->startHidden){
/ /[1] 2 3 4 5 6 7 8 9 10 ...100 Next page Last page
$long = $this->showPagesNo - 2;
for($i=0;$i<$long;$i++ ){
$j = $i+2;
if($j == $this->currentPage){
$content .= '['.$this->currentPage.'] ';
}else{
$content .= ''.$j.' ';
}
}
}elseif( $this->currentPage >= $this->endHidden ){
//Home page previous page 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 .= ''.$j.' ';
}
}
}else{
//Home Page Previous Page 1..12 13 14 15 [16] 17 18 19 20 ...100 Next page 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 .= ' ';
}
}
}
$part3 = $content;
return $part3;
}
public function getPart4(){
$content = '
'.$this->allPage.' ';
$add = '';
if($this->currentPage < $this->endHidden){
$add = '...';
}
if($this->currentPage == $this->allPage){
$content = '['.$this->allPage.']';
$add = '';
}
$part4 = $add.$content;
return $part4;
}
public function getPart5(){
$content = '下页 尾页';
if($this->currentPage >= $this->endHidden){
$content = '';
}
return $content;
}
public function show(){
//判断非法
if(!is_numeric($this->currentPage) || $this->currentPage < 0 || $this->currentPage > $this->allPage){
print 'error:pageNo is flase';
return;
}
//总页数没有达到显示分页栏的总页码数,则全部显示
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 .= ''.$j.' ';
}
}
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;
}
}
?>

以上就介绍了 PHP 分页类模仿google-面试题目解答,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!