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 相关文章推荐
为查询结果建立向后/向前按钮
Oct 09 PHP
php curl基本操作详解
Jul 23 PHP
从零开始学YII2框架(四)扩展插件yii2-kartikgii
Aug 20 PHP
CI框架Session.php源码分析
Nov 03 PHP
php轻松实现文件上传功能
Mar 03 PHP
php实现的SSO单点登录系统接入功能示例分析
Oct 12 PHP
php发送http请求的常用方法分析
Nov 08 PHP
使用ThinkPHP生成缩略图及显示
Apr 27 PHP
php实现与python进行socket通信的方法示例
Aug 30 PHP
PHP小程序支付功能完整版【基于thinkPHP】
Mar 26 PHP
实例说明js脚本语言和php脚本语言的区别
Apr 04 PHP
php实现的简单多进程服务器类完整示例
Feb 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中实现进程间通讯
2006/10/09 PHP
php数据库备份还原类分享
2014/03/20 PHP
基于ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据
2015/09/23 PHP
控制打印时页眉角的代码
2007/02/08 Javascript
javascritp实现input输入框相关限制用法
2007/06/29 Javascript
js的正则test,match,exec详细解析
2014/01/29 Javascript
jQuery点缩略图弹出层显示大图片
2015/02/13 Javascript
javascript倒计时效果实现
2015/11/12 Javascript
Jquery 效果使用详解
2015/11/23 Javascript
纯JavaScript代码实现文本比较工具
2016/02/17 Javascript
JavaScript生成带有缩进的表格代码
2016/06/15 Javascript
JS全局变量和局部变量最新解析
2016/06/24 Javascript
JS控制页面跳转时未请求要跳转的地址怎么回事
2016/10/14 Javascript
仿iframe效果Aajx文件上传实例
2016/11/18 Javascript
jQuery.cookie.js使用方法及相关参数解释
2017/03/06 Javascript
JS实现加载和读取XML文件的方法详解
2017/04/24 Javascript
如何在 Vue.js 中使用第三方js库
2017/04/25 Javascript
vue2.0模拟锚点的实例
2018/03/14 Javascript
Javascript中parseInt的正确使用方式
2018/10/17 Javascript
详解vue开发中调用微信jssdk的问题
2019/04/16 Javascript
深入理解JavaScript 箭头函数
2019/05/30 Javascript
用Python实现一个简单的能够发送带附件的邮件程序的教程
2015/04/08 Python
Pyqt QImage 与 np array 转换方法
2019/06/27 Python
Python range、enumerate和zip函数用法详解
2019/09/11 Python
pytorch GAN生成对抗网络实例
2020/01/10 Python
python使用信号量动态更新配置文件的操作
2020/04/01 Python
Spark处理数据排序问题如何避免OOM
2020/05/21 Python
python 实现rolling和apply函数的向下取值操作
2020/06/08 Python
解决运行出现'dict' object has no attribute 'has_key'问题
2020/07/15 Python
Python调用C/C++的方法解析
2020/08/05 Python
英国在线花园中心:You Garden
2018/06/03 全球购物
用友笔试题目
2016/10/25 面试题
茶叶店创业计划书范文
2014/01/19 职场文书
党支部工作总结2015
2015/04/01 职场文书
redis缓存存储Session原理机制
2021/11/20 Redis
vue3引入highlight.js进行代码高亮的方法实例
2022/04/08 Vue.js