Home  >  Article  >  Backend Development  >  PHP paging class (imitating Google)-Interview question answers_PHP tutorial

PHP paging class (imitating Google)-Interview question answers_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:44:321010browse

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 dizzy for a long time.
In the end, there was no error in the test program, but it just couldn’t be displayed normally. Later (after returning home), I checked and found out that the statement: for($i=0;$i++;$i<9) was written incorrectly, so I made up my mind. Write it again, so you 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 Page 1..12 13 14 15 [16] 17 18 19 20 ...100 Next page Last page
First page Previous page 1..92 93 94 95 96 97 98 [99] 100

Use Method:
$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 last page
*/

public $allPage; //Total number of pages
public $allRocords; //Total Number of records
public $perPage; //Number of records per page
public $showPagesNo; //Display the total number of pages in the paging bar. There are 11 in the display style.
public $currentPage; //Current page
public $urlModel; //Url link style

public $startHidden; //When 1... appears, the page number starts to hide the middle page
public $endHidden; //When...100 appears The number of pages ends and the middle page is hidden

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 plan 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 .= '< a href="'.$this->getUrl($j).'">'.$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 .= ''.$j.' ' ;
}
}
}
$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;
}
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/320481.htmlTechArticle笔试回答的不太好,特别是JS部分,也是许久都没复习的原因。 上机题目是要写一个仿google分页的类,当要取类似9/2的最大整数,却怎么也...
Statement:
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