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 adodb介绍
Mar 19 PHP
php下获取Discuz论坛登录用户名、用户组、用户ID等信息的实现代码
Dec 29 PHP
PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)
Oct 05 PHP
PHP输出时间差函数代码
Jan 28 PHP
PHP Session机制简介及用法
Aug 19 PHP
php中实现记住密码下次自动登录的例子
Nov 06 PHP
php获取url参数方法总结
Nov 13 PHP
php获取数组元素中头一个数组元素值的实现方法
Dec 20 PHP
PHP弹出对话框技巧详细解读
Sep 26 PHP
thinkPHP中钩子的两种配置调用方法详解
Nov 11 PHP
Thinkphp整合微信支付功能
Dec 14 PHP
PHP实现一维数组与二维数组去重功能示例
May 24 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文件上传主要代码讲解
2013/09/30 PHP
ThinkPHP的L方法使用简介
2014/06/18 PHP
PHP实现的大文件切割与合并功能示例
2018/04/10 PHP
Gambit vs CL BO3 第一场 2.13
2021/03/10 DOTA
可以用鼠标拖动的DIV实现思路及代码
2013/10/21 Javascript
nodejs中使用多线程编程的方法实例
2015/03/24 NodeJs
jQuery实现点击小图片淡入淡出显示大图片特效
2015/09/09 Javascript
JavaScript+html5 canvas制作的圆中圆效果实例
2016/01/27 Javascript
基于jQuery实现表格内容的筛选功能
2016/08/21 Javascript
Vue.js第三天学习笔记(计算属性computed)
2016/12/01 Javascript
懒加载实现的分页&amp;&amp;网站footer自适应
2016/12/21 Javascript
Vue.js鼠标悬浮更换图片功能
2017/05/17 Javascript
详解利用 Vue.js 实现前后端分离的RBAC角色权限管理
2017/09/15 Javascript
webpack4.x打包过程详解
2018/07/18 Javascript
微信小程序实现卡片左右滑动效果的示例代码
2019/05/01 Javascript
JavaScript HTML DOM元素 节点操作汇总
2019/07/29 Javascript
jQuery 图片查看器插件 Viewer.js用法简单示例
2020/04/04 jQuery
[01:09:40]Newbee vs Pain 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python判断端口是否打开的实现代码
2013/02/10 Python
Python实现的一个简单LRU cache
2014/09/26 Python
python根据出生日期获得年龄的方法
2015/03/31 Python
在Django框架中伪造捕捉到的URLconf值的方法
2015/07/18 Python
Python中如何获取类属性的列表
2016/12/26 Python
Python排序算法实例代码
2017/08/10 Python
Tensorflow中的placeholder和feed_dict的使用
2018/07/09 Python
opencv3/Python 稠密光流calcOpticalFlowFarneback详解
2019/12/11 Python
Pytorch自定义Dataset和DataLoader去除不存在和空数据的操作
2021/03/03 Python
Whittard官方海外旗舰店:英国百年茶叶品牌
2018/02/22 全球购物
迪卡侬中国官网:Decathlon中国
2020/08/10 全球购物
物理研修随笔感言
2014/02/14 职场文书
保洁公司服务承诺书
2014/05/28 职场文书
2014年党风建设工作总结
2014/11/19 职场文书
2014年德育工作总结
2014/11/20 职场文书
2016年“六一儿童节”校园广播稿
2015/12/17 职场文书
创业不要错过,这4种餐饮新模式
2019/07/18 职场文书
springboot创建的web项目整合Quartz框架的项目实践
2022/06/21 Java/Android