PHP 分页类(模仿google)-面试题目解答

 更新时间:2009年09月13日 22:08:26   作者:  
今天去XX公司面试了,感觉很不理想。因为这一段时间都在加深Zend Framework,都没练习常用函数和方法了。
笔试回答的不太好,特别是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>&nbsp;<a href="'.$this->getUrl('pre').'">上页</a>&nbsp;';
if($this->currentPage <= $this->startHidden){
$content = '';
}
return $content;
}

public function getPart2(){
$content = '<a href="'.$this->getUrl(1).'">1</a>&nbsp;';
$add = '';
if($this->currentPage > $this->startHidden){
$add = '...';
}
if($this->currentPage == 1){
$content = '[1]&nbsp;';
$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.']&nbsp;';
}else{
$content .= '<a href="'.$this->getUrl($j).'">'.$j.'</a>&nbsp;';
}

}

}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.']&nbsp;';
}else{
$content .= '<a href="'.$this->getUrl($j).'">'.$j.'</a>&nbsp;';
}
}
}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.']&nbsp;';
}else{
$content .= '<a href="'.$this->getUrl($j).'">'.$j.'</a>&nbsp;';
}
}
}
$part3 = $content;
return $part3;
}

public function getPart4(){
$content = '<a href="'.$this->getUrl().'">'.$this->allPage.'</a>&nbsp;';
$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>&nbsp;<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.']&nbsp;';
}else{
$content .= '<a href="'.$this->getUrl($j).'">'.$j.'</a>&nbsp;';
}

}
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中的Cannot modify header information 问题

    完美解决PHP中的Cannot modify header information 问题

    以下是对PHP中的Cannot modify header information问题的解决方法进行了详细的分析介绍,需要的朋友可以过来参考下
    2013-08-08
  • PHP中使用hidef扩展代替define提高性能

    PHP中使用hidef扩展代替define提高性能

    这篇文章主要介绍了PHP中使用hidef扩展代替define提高性能,本文着重测试hidef的性能,同时介绍了安装方法和使用示例,需要的朋友可以参考下
    2015-04-04
  • php 类中的常量、静态属性、非静态属性的区别

    php 类中的常量、静态属性、非静态属性的区别

    这篇文章主要介绍了php 类中的常量、静态属性、非静态属性的区别与介绍,需要的朋友可以参考下
    2017-04-04
  • PHP模版引擎原理、定义与用法实例

    PHP模版引擎原理、定义与用法实例

    这篇文章主要介绍了PHP模版引擎原理、定义与用法,结合实例形式分析了php模板引擎相关的模板编译、缓存处理、变量替换等操作技巧,需要的朋友可以参考下
    2019-03-03
  • 在Mac上编译安装PHP7的开发环境

    在Mac上编译安装PHP7的开发环境

    这篇文章主要介绍了在Mac上编译安装PHP7的开发环境的相关资料,需要的朋友可以参考下
    2015-07-07
  • 详解php内存管理机制与垃圾回收机制

    详解php内存管理机制与垃圾回收机制

    在PHP中,没有任何变量指向这个对象时,这个对象就成为垃圾。PHP会将其在内存中销毁;这是PHP的GC垃圾处理机制。PHP的内存管理,分为俩部分, 第一部分是PHP自身的内存管理,而第二部分是zend_alloc中描写的关于PHP自身的内存管理。本文将详细介绍这两个内容。
    2021-05-05
  • PHP使用PDO调用mssql存储过程的方法示例

    PHP使用PDO调用mssql存储过程的方法示例

    这篇文章主要介绍了PHP使用PDO调用mssql存储过程的方法,结合简单实例形式分析了php基于pdo简单实现连接及调用存储过程相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • php SQL防注入代码集合

    php SQL防注入代码集合

    php下实现sql防注入效果代码,asp的比较多,php的倒不多见,喜欢php的朋友可以参考下
    2008-04-04
  • PHP中使用协同程序实现合作多任务

    PHP中使用协同程序实现合作多任务

    这篇文章指导你通过使用协同程序来实施任务调度,通过实例实现对技术的理解。我将在前三节做一个简单的背景介绍。如果你已经有了比较好的基础,可以直接跳到“协同多任务处理”一节
    2013-07-07
  • PHP实战之投票系统的实现

    PHP实战之投票系统的实现

    这篇文章主要为大家介绍了如何利用PHP制作一个投票系统,文中的示例代码讲解详细,感兴趣的小伙伴快跟随小编一起学习一下
    2022-04-04

最新评论