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 相关文章推荐
CentOS 6.2使用yum安装LAMP以及phpMyadmin详解
Jun 17 PHP
解析使用ThinkPHP应该掌握的调试手段
Jun 20 PHP
浅析php面向对象public private protected 访问修饰符
Jun 30 PHP
PHP-FPM实现性能优化
Mar 31 PHP
php+mysql查询实现无限下级分类树输出示例
Oct 03 PHP
PHP实现添加购物车功能
Mar 06 PHP
PHP实现文件上传功能实例代码
May 18 PHP
php如何修改SESSION的生存存储时间的实例代码
Jul 05 PHP
PHP实现生成推广海报的方法详解
Mar 14 PHP
PHP实现搜索时记住状态的方法示例
May 11 PHP
laravel 实现划分admin和home 模块分组
Oct 15 PHP
thinkphp框架无限级栏目的排序功能实现方法示例
Mar 29 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 字符转义 注意事项
2009/05/27 PHP
浅析php-fpm静态和动态执行方式的比较
2016/11/09 PHP
php mysql_real_escape_string addslashes及mysql绑定参数防SQL注入攻击
2016/12/23 PHP
PHP用户注册邮件激活账户的实现代码
2017/05/31 PHP
Laravel框架Eloquent ORM删除数据操作示例
2019/12/03 PHP
js判断浏览器的比较全的代码
2007/02/13 Javascript
如何在Web页面上直接打开、编辑、创建Office文档
2007/03/12 Javascript
js 面向对象的技术创建高级 Web 应用程序
2010/02/25 Javascript
jquery中通过父级查找进行定位示例
2013/06/28 Javascript
使用jQuery解决IE与FireFox下createElement方法的差异
2013/11/14 Javascript
JS中JSON对象和String之间的互转及处理技巧
2016/04/06 Javascript
jQueryUI中的datepicker使用方法详解
2016/05/25 Javascript
canvas快速绘制圆形、三角形、矩形、多边形方法介绍
2016/12/29 Javascript
js实现4个方向滚动的球
2017/03/06 Javascript
详解Vue双向数据绑定原理解析
2017/09/11 Javascript
ReactNative实现Toast的示例
2017/12/31 Javascript
从parcel.js打包出错到选择nvm的全部过程
2018/01/23 Javascript
vue数据控制视图源码解析
2018/03/28 Javascript
JS实现全屏预览F11功能的示例代码
2018/07/23 Javascript
JS实现点击生成UUID的方法完整实例【基于jQuery】
2019/06/12 jQuery
vue中上传视频或图片或图片和文字一起到后端的解决方法
2019/12/01 Javascript
js实现点击生成随机div
2020/01/16 Javascript
js实现超级玛丽小游戏
2020/03/18 Javascript
vue中watch的用法汇总
2020/12/28 Vue.js
Cpy和Python的效率对比
2015/03/20 Python
用Python编写简单的微博爬虫
2016/03/04 Python
Python多进程方式抓取基金网站内容的方法分析
2019/06/03 Python
pandas实现将dataframe满足某一条件的值选出
2019/06/12 Python
python多进程并发demo实例解析
2019/12/13 Python
详解python中的异常捕获
2020/12/15 Python
Mountain Warehouse波兰官方网站:英国户外品牌
2019/08/29 全球购物
介绍一下.net和Java的特点和区别
2012/09/26 面试题
个人考核材料
2014/05/15 职场文书
官僚主义现象查摆问题整改措施
2014/10/04 职场文书
Go 通过结构struct实现接口interface的问题
2021/10/05 Golang
MySQL 数据库 增删查改、克隆、外键 等操作
2022/05/11 MySQL