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


Posted in PHP onSeptember 13, 2009

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

<?php 
/* 
显示样式如下: 
[1] 2 3 4 5 6 7 8 9 10 ...100 下页 尾页 
首页 上页 1..12 13 14 15 [16] 17 18 19 20 ...100 下页 尾页 
首页 上页 1..92 93 94 95 96 97 98 [99] 100 使用方法: 
$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; 
/* 
对下面的分页显示进行分割: 
首页 上页 1..12 13 14 15 [16] 17 18 19 20 ...100 下页 尾页 
$part1 : 首页 上页 
$part2 : 1.. 
$part3 : 12 13 14 15 [16] 17 18 19 20 
$part4 : ...100 
$part5 : 下页 尾页 
*/ 
public $allPage; //总页数 
public $allRocords; //总记录数 
public $perPage; //每页记录数 
public $showPagesNo; //显示分页栏的总页码数 显示样式里共有11个 
public $currentPage; //当前页 
public $urlModel; //Url链接样式 
public $startHidden; //出现 1... 时的页数 开始隐藏中间页 
public $endHidden; //出现 ...100 时的页数 结束隐藏中间页 
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++; 
} 
//没有想起ceil时的候补方案 
return $_int; 
} 
public function getPart1(){ 
$content = '<a href="'.$this->getUrl(1).'">首页</a> <a href="'.$this->getUrl('pre').'">上页</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 = ''; 
} 
$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 下页 尾页 
$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 ){ 
//首页 上页 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{ 
//首页 上页 1..12 13 14 15 [16] 17 18 19 20 ...100 下页 尾页 
$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> '; 
} 
} 
} 
$part3 = $content; 
return $part3; 
} 
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 = ''; 
} 
$part4 = $add.$content; 
return $part4; 
} 
public function getPart5(){ 
$content = '<a href="'.$this->getUrl('next').'">下页</a> <a href="'.$this->getUrl().'">尾页</a>'; 
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 .= '<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; 
} 
} 
?>
PHP 相关文章推荐
PHP+DBM的同学录程序(2)
Oct 09 PHP
php把session写入数据库示例
Feb 26 PHP
微信营销平台系统?刮刮乐的开发
Jun 10 PHP
Codeigniter注册登录代码示例
Jun 12 PHP
PHP正则替换函数preg_replace和preg_replace_callback使用总结
Sep 22 PHP
php从字符串创建函数的方法
Mar 16 PHP
php mysql like 实现多关键词搜索的方法
Oct 29 PHP
php解决DOM乱码的方法示例代码
Nov 20 PHP
PHP获取当前执行php文件名的代码
Mar 02 PHP
php安装扩展mysqli的实现步骤及报错解决办法
Sep 23 PHP
php创建类并调用的实例方法
Sep 25 PHP
如何使用php生成zip压缩包
Apr 21 PHP
frename PHP 灵活文件命名函数 frename
Sep 09 #PHP
PHPLog php 程序调试追踪工具
Sep 09 #PHP
php 从数据库提取二进制图片的处理代码
Sep 09 #PHP
封装一个PDO数据库操作类代码
Sep 09 #PHP
PHP 数组遍历顺序理解
Sep 09 #PHP
PHP 裁剪图片成固定大小代码方法
Sep 09 #PHP
PHP 获取MSN好友列表的代码(2009-05-14测试通过)
Sep 09 #PHP
You might like
PHP中10个不常见却非常有用的函数
2010/03/21 PHP
php设计模式 Builder(建造者模式)
2011/06/26 PHP
PHP在获取指定目录下的目录,在获取的目录下面再创建文件,多平台
2011/08/03 PHP
ThinkPHP CURD方法之page方法详解
2014/06/18 PHP
php使用pear_smtp发送邮件
2016/04/15 PHP
yii框架使用分页的方法分析
2019/07/25 PHP
基于jQuery实现表格数据的动态添加与统计的代码
2011/01/31 Javascript
javascript:;与javascript:void(0)使用介绍
2013/06/05 Javascript
用JQuery实现全选与取消的两种简单方法
2014/02/22 Javascript
一张表格告诉你windows.onload()与$(document).ready()的区别
2014/05/16 Javascript
JavaScript和HTML DOM的区别与联系及Javascript和DOM的关系
2015/11/15 Javascript
jQuery使用$.ajax进行异步刷新的方法(附demo下载)
2015/12/04 Javascript
jquery读写cookie操作实例分析
2015/12/24 Javascript
javascript使用闭包模拟对象的私有属性和方法
2016/10/05 Javascript
Jq通过td获取同行其它列td的方法
2016/10/05 Javascript
Koa2 之文件上传下载的示例代码
2018/03/29 Javascript
vue 指令之气泡提示效果的实现代码
2018/10/18 Javascript
详解Vue.directive 自定义指令
2019/03/27 Javascript
中高级前端必须了解的JS中的内存管理(推荐)
2019/07/04 Javascript
[53:10]2018DOTA2亚洲邀请赛 4.6 淘汰赛 VP vs VG 第一场
2018/04/11 DOTA
[29:16]完美世界DOTA2联赛决赛日 Inki vs LBZS 第三场 11.08
2020/11/10 DOTA
python中实现php的var_dump函数功能
2015/01/21 Python
python3对接mysql数据库实例详解
2019/04/30 Python
Python单元测试与测试用例简析
2019/11/09 Python
Python TCP通信客户端服务端代码实例
2019/11/21 Python
Python基于wordcloud及jieba实现中国地图词云图
2020/06/09 Python
Python改变对象的字符串显示的方法
2020/08/01 Python
Vuori官网:运动服装的终级表现
2021/01/27 全球购物
计算机毕业生自荐信范文
2014/03/23 职场文书
优秀护士演讲稿
2014/04/30 职场文书
环卫工作汇报材料
2014/10/28 职场文书
考研经验交流会策划书
2015/11/02 职场文书
Python中Numpy和Matplotlib的基本使用指南
2021/11/02 Python
基于Pygame实现简单的贪吃蛇游戏
2021/12/06 Python
全新239军机修复记
2022/04/05 无线电
python中pycryto实现数据加密
2022/04/29 Python