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数组函数序列之krsort()- 对数组的元素键名进行降序排序,保持索引关系
Nov 02 PHP
『PHP』PHP截断函数mb_substr()使用介绍
Apr 22 PHP
php中is_null,empty,isset,unset 的区别详细介绍
Apr 28 PHP
php 模拟POST提交的2种方法详解
Jun 17 PHP
php强制更新图片缓存的方法
Feb 11 PHP
PHP使用JSON和将json还原成数组
Feb 12 PHP
codeigniter实现get分页的方法
Jul 10 PHP
php微信扫码支付 php公众号支付
Mar 24 PHP
PHP正则表达式处理函数(PCRE 函数)实例小结
May 09 PHP
php函数式编程简单示例
Aug 08 PHP
PHP读取XML文件的方法实例总结【DOMDocument及simplexml方法】
Sep 10 PHP
PHP策略模式写法
Apr 01 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获取时间排除周六、周日的两个方法
2014/06/30 PHP
PHP基本语法总结
2014/09/06 PHP
PHP重定向与伪静态区别
2017/02/19 PHP
PHP实现将几张照片拼接到一起的合成图片功能【便于整体打印输出】
2017/11/14 PHP
cloudgamer出品ImageZoom 图片放大效果
2010/04/01 Javascript
分享14个很酷的jQuery导航菜单插件
2011/04/25 Javascript
JS判断表单输入是否为空(示例代码)
2013/12/23 Javascript
DIV始终居中的js代码
2014/02/17 Javascript
初始Nodejs
2014/11/08 NodeJs
AngularJS iframe跨域打开内容时报错误的解决办法
2015/01/26 Javascript
javascript鼠标滑动评分控件完整实例
2015/05/13 Javascript
jQuery xml字符串的解析、读取及查找方法
2016/03/01 Javascript
JavaScript第一篇之实现按钮全选、功能
2016/08/21 Javascript
jquery移除了live()、die(),新版事件绑定on()、off()的方法
2016/10/26 Javascript
JavaScript实现移动端轮播效果
2017/06/06 Javascript
微信小程序开发之map地图实现教程
2017/06/08 Javascript
Vue.js与 ASP.NET Core 服务端渲染功能整合
2017/11/16 Javascript
jQuery+CSS实现的标签页效果示例【测试可用】
2018/08/14 jQuery
Vue项目数据动态过滤实践及实现思路
2018/09/11 Javascript
Node批量爬取头条视频并保存方法
2018/09/20 Javascript
vuejs简单验证码功能完整示例
2019/01/08 Javascript
Python标准模块--ContextManager上下文管理器的具体用法
2017/11/27 Python
python基于ID3思想的决策树
2018/01/03 Python
Python使用pyyaml模块处理yaml数据
2020/04/14 Python
Jupyter Notebook 文件默认目录的查看以及更改步骤
2020/04/14 Python
Html5 APP中监听返回事件处理的方法示例
2018/03/15 HTML / CSS
俄罗斯儿童和青少年服装、鞋子及配件的在线商店:Orby
2020/02/20 全球购物
武汉高蓝德国际.net机试
2016/06/24 面试题
个人生活学习自我评价范文
2013/11/26 职场文书
工程管理专业个人求职信范文
2013/12/07 职场文书
会计毕业自我鉴定
2014/02/05 职场文书
优秀公益广告词大全
2014/03/19 职场文书
宪法宣传周工作方案
2014/05/26 职场文书
敬老院标语
2014/06/27 职场文书
毕业生银行实习自我鉴定
2014/10/14 职场文书
优秀党员主要事迹材料
2015/11/04 职场文书