笔试回答的不太好,特别是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;
}
}
?>
上机题目是要写一个仿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,分页类
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“PHP 分页类(模仿google)-面试题目解答”评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。