ホームページ  >  記事  >  バックエンド開発  >  PHP ページング クラスは Google インタビューの質問の回答を模倣します

PHP ページング クラスは Google インタビューの質問の回答を模倣します

WBOY
WBOYオリジナル
2016-07-29 08:40:471069ブラウズ

筆記試験、特に JS の部分にあまりうまく答えられなかったので、長い間見直していませんでした。
コンピュータの問題は、Google のページングを模倣するクラスを書くことです。9/2 のような最大の整数を取得したいとき、関数 ceil の名前を思い出せず、長い間めまいを感じます。
結局、テストプログラムにはエラーはなかったのですが、ただ正常に表示できなかっただけで、後で(帰宅後)確認したところ、 for($i=0;$i++ という文があったことが分かりました。 ;$i<9) の記述が間違っていたため、次のコードを書き直すことにしました:

コードをコピーします コードは次のとおりです:


/*
表示スタイルは次のとおりです:
[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 = 新しい pagediv(500, 10, 11, $currentPage, 'test.php?page=');
*/
class pagediv
{
public $part1;
public $part3;
public $part5;
ホームページ 前のページ 1..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 // ページごとのレコード数
; $showPagesNo; //ページングを表示 バー表示には合計 11 ページあります style
public $urlModel; // URL リンク style
public $startHidden; ... 中間ページの非表示を開始します
public $endHidden; //...100 が表示されたときのページ数が中間ページの非表示を終了します
public function __construct($allRocords, $perPage, $showPagesNo, $currentPage, $ urlModel){
$this->allRocords = $allRocords;
$this->showPagesNo = $showPagesNo; ->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 == '前') $_current = $this->currentPage -1;
if($_index == 'next') $_current = $this->currentPage+1; $_index == '') $_current = $this ->allPage;
return $this->urlModel.$_current
}
return $this->getInt; >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 <= $this->startHidden){
$content = '';
return $content; ){
$content = '
1$add = ''; ;currentPage > $this->startHidden ){
$add = '...'
}
if($this->currentPage == 1){
$content = '[1] '; add = '';
$part2 = $content.$add;
}
パブリック関数 getPart3(){
$content = ''; $this->startHidden){
/ /[1] 2 3 4 5 6 7 8 9 10 ...100 次のページ 最後のページ
$long = $this->showPagesNo - 2; 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 .= '< a href="'.$this-> getUrl($j).'">'.$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-> $offset;
for($i=0;$i<$long;$i++){
$j = $_start +
if; == $this->currentPage){
$ content .= '['.$this->currentPage.'] ';
}else{
$content .= ' ';
}
}
}
$part3 = $content;
return $part3;
}
public function getPart4 (){
$content = '
'.$this->allPage.'
$add = '';
if($this->currentPage < $this->endHidden){
$add = '...';
if($this->currentPage == $this->allPage){
$content = '['.$this->allPage.']';
$add = '';
}
$part4 = $add.$content;
$part4 を返します;
}
public function getPart5(){
$content = '下页 尾页';
if($this->currentPage>= $this->endHidden){
$content = '';
}
$content を返します。
}
public function show(){
//判断非法
if(!is_numeric($this->currentPage) || $this->currentPage < 0 || $this->currentPage > $this ->allPage){
print 'error:pageNo は flase';
戻る;
}
//总页数は表示分数に達していません、すべて表示
if($this->allPage < $this->showPagesNo){
$long = $this->allPage ;
for($i=0;$i$j = $i+1;
if($j == $this->currentPage){
$content .= '['.$this->currentPage.'] ';
}else{
$content .= ''.$j.' ';
}
}
print $content;
戻る;
}
$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 面での PHP 分別モジュールの解答を介して、側面の内容が含まれており、PHP 教程に関心のある友人の助けになることを望みます。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。