笔试回答的不太好,特别是js部分,也是许久都没复习的原因。
上机题目是要写一个仿google分页的类,当要取类似9/2的最大整数,却怎么也想不起函数ceil的名字,晕了半天。
最后测试程序没错误,但是就是不能正常显示,后来(回家后)一查才知道是语句:for($i=0;$i++;$i
复制代码 代码如下:
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 = 'geturl(1).'>首页 geturl('pre').'>上页 ';
if($this->currentpage starthidden){
$content = '';
}
return $content;
}
public function getpart2(){
$content = 'geturl(1).'>1 ';
$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 starthidden){
//[1] 2 3 4 5 6 7 8 9 10 ...100 下页 尾页
$long = $this->showpagesno - 2;
for($i=0;$i$j = $i+2;
if($j == $this->currentpage){
$content .= '['.$this->currentpage.'] ';
}else{
$content .= 'geturl(%24j).'>'.$j.' ';
}
}
}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$j = $_start + $i;
if($j == $this->currentpage){
$content .= '['.$this->currentpage.'] ';
}else{
$content .= 'geturl(%24j).'>'.$j.' ';
}
}
}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$j = $_start + $i;
if($j == $this->currentpage){
$content .= '['.$this->currentpage.'] ';
}else{
$content .= 'geturl(%24j).'>'.$j.' ';
}
}
}
$part3 = $content;
return $part3;
}
public function getpart4(){
$content = 'geturl().'>'.$this->allpage.' ';
$add = '';
if($this->currentpage endhidden){
$add = '...';
}
if($this->currentpage == $this->allpage){
$content = '['.$this->allpage.']';
$add = '';
}
$part4 = $add.$content;
return $part4;
}
public function getpart5(){
$content = 'geturl('next').'>下页 geturl().'>尾页';
if($this->currentpage >= $this->endhidden){
$content = '';
}
return $content;
}
public function show(){
//判断非法
if(!is_numeric($this->currentpage) || $this->currentpage currentpage > $this->allpage){
print 'error:pageno is flase';
return;
}
//总页数没有达到显示分页栏的总页码数,则全部显示
if($this->allpage showpagesno){
$long = $this->allpage;
for($i=0;$i$j = $i+1;
if($j == $this->currentpage){
$content .= '['.$this->currentpage.'] ';
}else{
$content .= 'geturl(%24j).'>'.$j.' ';
}
}
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 分页类模仿google-面试题目解答,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。