筆試回答的不太好,特別是JS部分,也是許久都沒複習的原因。
上機題目是要寫一個仿google分頁的類,當要取類似9/2的最大整數,卻怎麼也想不起函數ceil的名字,暈了半天。
最後測試程式沒錯,但就是無法正常顯示,後來(回家後)一查才知道是語句:for($i=0;$i++;$i
複製程式碼 程式碼如下:
/*
顯示樣式如下:
[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 $part1;
public $part2;
public $part3; $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->getAllPage();
$this->startHidden = $this->getAllPage();
$this->startHidden = $this->getAllPage();
$this->startHidden = $this->getAllPage();
$this->startHidden = $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 = ' 首頁 上頁 ';
if($this->currentPage startHidden){
$content = '';
}
return $content;
}
public function getPart2(){
$content = '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 . = ''.$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 .= ''.$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 .= ''.$j.' ';
}
}
}
$part3 = $content;
return $part3;
} public function getPart4(){ $content = ''.$this->allPage.' '; $add = ''; if($this->currentPage endHidden){ $add = '...'; }
if($this->currentPage == $this->allPage){
$content = '['.$this->allPage.']';
$add = '';
}
$part4 = $add.$content;
回傳$第4部分;
}
public function getPart5(){
$content = '下頁> ; 尾頁';
if ($this->currentPage >= $this->endHidden){
$content = '';
}
回傳$內容;
}
public function show(){
//判斷非法
if(!is_numeric($this->currentPage) || $this->currentPage currentPage>$this->allPage){
print '錯誤: pageNo 為flase';
回傳;
}
//總頁碼沒有達到顯示分頁欄的總頁碼數,則全部顯示
if($this->allPage showPagesNo ){
$ 長 = $this->allPage;
for($i=0;$i$j = $i+1;
if( $j == $this->currentPage){
$content .= '['.$this->currentPage.'] ';
}else{
$content .= ''.$j.' ';
}
}
列印$內容;
回傳;
}
$this->part1 = $this->getPart1();
$this->part2 = $this->getPart2();
$this->part3 = $this->getPart3 ();
$this->part4 = $this->getPart4();
$this->part5 = $this->getPart5();
印$這個->第1部分.$這個->第2部分.$這個->第3部分.$這個->第4部分.$這個->第5部分;
}
}
? >
以上主題就介紹了 PHP 分頁類原型 google-面試解答,包括了方面的內容,希望對 PHP 教程有興趣的朋友得到幫助。