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 相关文章推荐
将RTF格式的文件转成HTML并在网页中显示的代码
Oct 09 PHP
ob_start(),ob_start('ob_gzhandler')使用
Dec 25 PHP
How do I change MySQL timezone?
Mar 26 PHP
JS 网站性能优化笔记
May 24 PHP
判断php数组是否为索引数组的实现方法
Jun 13 PHP
php中让上传的文件大小在上传前就受限制的两种解决方法
Jun 24 PHP
CI框架开发新浪微博登录接口源码完整版
May 28 PHP
PHP中使用正则表达式提取中文实现笔记
Jan 20 PHP
php利用smtp类实现电子邮件发送
Oct 30 PHP
PHP实现将MySQL重复ID二维数组重组为三维数组的方法
Aug 01 PHP
PHP实现mysqli批量执行多条语句的方法示例
Jul 22 PHP
PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】
Nov 14 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代码包装修正版
2008/03/15 PHP
PHP数据类型之布尔型的介绍
2013/04/28 PHP
php对数组排序代码分享
2014/02/24 PHP
PHP生成不重复标识符的方法
2014/11/21 PHP
php随机获取金山词霸每日一句的方法
2015/07/09 PHP
php中array_column函数简单实现方法
2016/07/11 PHP
javascript中数组中求最大值示例代码
2013/12/18 Javascript
js中判断用户输入的值是否为空的简单实例
2013/12/23 Javascript
将数字转换成大写的人民币表达式的js函数
2014/09/21 Javascript
jQuery中empty()方法用法实例
2015/01/16 Javascript
使用javascript实现判断当前浏览器
2015/04/14 Javascript
TypeOf这些知识点你了解吗
2016/02/21 Javascript
javaScript如何跳出多重循环break、continue
2016/09/01 Javascript
Express框架之connect-flash详解
2017/05/31 Javascript
微信小程序之swiper滑动面板用法示例
2018/12/04 Javascript
详解Nodejs get获取远程服务器接口数据
2019/03/26 NodeJs
详解mpvue实现对苹果X安全区域的适配
2019/07/31 Javascript
小程序实现上下移动切换位置
2019/09/23 Javascript
通过实例了解Nodejs模块系统及require机制
2020/07/16 NodeJs
vue实现移动端项目多行文本溢出省略
2020/07/29 Javascript
Python实现字典依据value排序
2016/02/24 Python
总结python实现父类调用两种方法的不同
2017/01/15 Python
python rsa 加密解密
2017/03/20 Python
Python爬虫工程师面试问题总结
2018/03/22 Python
Python从list类型、range()序列简单认识类(class)【可迭代】
2019/05/31 Python
Python Pandas实现数据分组求平均值并填充nan的示例
2019/07/04 Python
Python编写通讯录通过数据库存储实现模糊查询功能
2019/07/18 Python
python 计算两个列表的相关系数的实现
2019/08/29 Python
docker-py 用Python调用Docker接口的方法
2019/08/30 Python
python构造IP报文实例
2020/05/05 Python
Anaconda详细安装步骤图文教程
2020/11/12 Python
美国玛丽莎收藏奢华时尚商店:Marissa Collections
2016/11/21 全球购物
幼儿园国庆节活动方案
2014/02/01 职场文书
cf战队收人口号
2014/06/21 职场文书
护士求职简历自我评价
2015/03/10 职场文书
互联网的下一个风口:新的独角兽将诞生
2019/08/02 职场文书