• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP 分页类(模仿google)-面试题目解答_php技巧

php 搞代码 3年前 (2022-01-26) 17次浏览 已收录 0个评论

笔试回答的不太好,特别是JS部分,也是许久都没复习的原因。
上机题目是要写一个仿google分页的类,当要取类似9/2的最大整数,却怎么也想不起函数ceil的名字,晕了半天。
最后测试程序没错误,但是就是不能正常显示,后来(回家后)一查才知道是语句:for($i=0;$i++;$i<9)写错了,于是下决心重新写一遍,于是就有了下面的代码了:

 <BR><?php <BR>/* <BR>显示样式如下: <BR>[1] 2 3 4 5 6 7 8 9 10 ...100 下页 尾页 <BR>首页 上页 1..12 13 14 15 [16] 17 18 19 20 ...100 下页 尾页 <BR>首页 上页 1..92 93 94 95 96 97 98 [99] 100 <br><br>使用方法: <BR>$currentPage = $_GET['page']?$_GET['page']:1; <BR>$pagediv = new pagediv(500, 10, 11, $currentPage, 'test.php?page='); <BR>$pagediv->show(); <br><br>*/ <BR>class pagediv <BR>{ <BR>public $part1; <BR>public $part2; <BR>public $part3; <BR>public $part4; <BR>public $part5; <br><br>/* <BR>对下面的分页显示进行分割: <BR>首页 上页 1..12 13 14 15 [16] 17 18 19 20 ...100 下页 尾页 <BR>$part1 : 首页 上页 <BR>$part2 : 1.. <BR>$part3 : 12 13 14 15 [16] 17 18 19 20 <BR>$part4 : ...100 <BR>$part5 : 下页 尾页 <BR>*/ <br><br>public $allPage; //总页数 <BR>public $allRocords; //总记录数 <BR>public $perPage; //每页记录数 <BR>public $showPagesNo; //显示分页栏的总页码数 显示样式里共有11个 <BR>public $currentPage; //当前页 <BR>public $urlModel; //Url链接样式 <br><br>public $startHidden; //出现 1... 时的页数 开始隐藏中间页 <BR>public $endHidden; //出现 ...100 时的页数 结束隐藏中间页 <br><br>public function __construct($allRocords, $perPage, $showPagesNo, $currentPage, $urlModel){ <BR>$this->allRocords = $allRocords; <BR>$this->perPage = $perPage; <BR>$this->showPagesNo = $showPagesNo; <BR>$this->currentPage = $currentPage; <BR>$this->urlModel = $urlModel; <BR>$this->allPage = $this->getAllPage(); <br><br>$this->startHidden = $this->getInt(($this->showPagesNo)/2); //6 <BR>$this->endHidden = $this->allPage - $this->startHidden; //94 <BR>} <br><br>public function getUrl($_index = ''){ <BR>$_current = $_index; <BR>if($_index == 'pre') $_current = $this->currentPage -1; <BR>if($_index == 'next') $_current = $this->currentPage+1; <BR>if($_index == '') $_current = $this->allPage; <BR>return $this->urlModel.$_current; <BR>} <br><br>public function getAllPage(){ <BR>return $this->getInt($this->allRocords/$this->perPage); <BR>} <br><br>public function getInt($_float){ <BR>$_int = $_float; <BR>if( $_index = strpos($_float,'.') == true ){ <BR>$_int = substr($_float,0,$_index); <BR>$_int++; <BR>} <BR>//没有想起ceil时的候补方案 <BR>return $_int; <BR>} <br><br>public function getPart1(){ <BR>$content = 'getUrl(1).'">首页 getUrl('pre').'">上页 '; <BR>if($this->currentPage startHidden){ <BR>$content = ''; <BR>} <BR>return $content; <BR>} <br><br>public function getPart2(){ <BR>$content = 'getUrl(1).'">1 '; <BR>$add = ''; <BR>if($this->currentPage > $this->startHidden){ <BR>$add = '...'; <BR>} <BR>if($this->currentPage == 1){ <BR>$content = '[1] '; <BR>$add = ''; <BR>} <BR>$part2 = $content.$add; <BR>return $part2; <BR>} <br><br>public function getPart3(){ <BR>$content = ''; <BR>if($this->currentPage startHidden){ <BR>//[1] 2 3 4 5 6 7 8 9 10 ...100 下页 尾页 <BR>$long = $this->showPagesNo - 2; <BR>for($i=0;$i<$long;$i++){ <BR>$j = $i+2; <BR>if($j == $this->currentPage){ <BR>$content .= '['.$this->currentPage.'] '; <BR>}else{ <BR>$content .= 'getUrl($j).'">'.$j.' '; <BR>} <br><br>} <br><br>}elseif( $this->currentPage >= $this->endHidden ){ <BR>//首页 上页 1..92 93 94 95 96 97 98 [99] 100 <BR>$long = $this->showPagesNo - 2; <BR>$_start = $this->allPage - $long; <BR>for($i=0;$i<$long;$i++){ <BR>$j = $_start + $i; <BR>if($j == $this->currentPage){ <BR>$content .= '['.$this->currentPage.'] '; <BR>}else{ <BR>$content .= 'getUrl($j).'">'.$j.' '; <BR>} <BR>} <BR>}else{ <BR>//首页 上页 1..12 13 14 15 [16] 17 18 19 20 ...100 下页 尾页 <BR>$long = $this->showPagesNo - 2; <BR>$offset = $this->getInt($long/2) - 1; <BR>$_start = $this->currentPage - $offset; <BR>for($i=0;$i<$long;$i++){ <BR>$j = $_start + $i; <BR>if($j == $this->currentPage){ <BR>$content .= '['.$this->currentPage.'] '; <BR>}else{ <BR>$content .= 'getUrl($j).'">'.$j.' '; <BR>} <BR>} <BR>} <BR>$part3 = $content; <BR>return $part3; <BR>} <br><br>public function getPart4(){ <BR>$content = 'getUrl().'">'.$this->allPage.' '; <BR>$add = ''; <BR>if($this->currentPage endHidden){ <BR>$add = '...'; <BR>} <BR>if($this->currentPage == $this->allPage){ <BR>$content =<strong>+本文来源gao@daima#com搞(%代@#码网</strong><pre>搞代gaodaima码

‘[‘.$this->allPage.’]’;
$add = ”;
}
$part4 = $add.$content;
return $part4;

}

public function getPart5(){
$content = ‘getUrl(‘next’).'”>下页 getUrl().'”>尾页’;
if($this->currentPage >= $this->endHidden){
$content = ”;
}
return $content;
}

public function show(){
//判断非法
if(!is_numeric($this->currentPage) || $this->currentPage currentPage > $this->allPage){
print ‘error:pageNo is flase’;
return;
}
//总页数没有达到显示分页栏的总页码数,则全部显示
if($this->allPage showPagesNo){
$long = $this->allPage;
for($i=0;$i<$long;$i++){
$j = $i+1;
if($j == $this->currentPage){
$content .= ‘[‘.$this->currentPage.’] ‘;
}else{
$content .= ‘getUrl($j).'”>’.$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;
}
}
?>


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP 分页类(模仿google)-面试题目解答_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址