php 分頁

cnnbull發表於2021-09-09



classPage{
private$each_disNums;//每頁顯示的條目數
private$nums;//總條目數
private$current_page;//當前被選中的頁
private$sub_pages;//每次顯示的頁數
private$pageNums;//總頁數
private$page_array=array();//用來構造分頁的陣列
private$subPage_link;//每個分頁的連結

functionPage($each_disNums,$nums,$current_page,$sub_pages,$subPage_link){
$this->each_disNums=intval($each_disNums);
$this->nums=intval($nums);
if(!$current_page){
$this->current_page=1;
}else{
$this->current_page=intval($current_page);
}
$this->sub_pages=intval($sub_pages);
$this->pageNums=ceil($nums/$each_disNums);
$this->subPage_link=$subPage_link;
}

function__construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_linke){
$this->Page($each_disNums,$nums,$current_page,$sub_pages,$subPage_link);
}


function__destruct(){
unset($each_disNums);
unset($nums);
unset($current_page);
unset($sub_pages);
unset($pageNums);
unset($page_array);
unset($subPage_link);
}


functioninitArray(){
for($i=0;$isub_pages;$i++){
$this->page_array[$i]=$i;
}
return$this->page_array;
}


functionconstruct_num_Page(){
if($this->pageNumssub_pages){
$current_array=array();
for($i=0;$ipageNums;$i++){
$current_array[$i]=$i+1;
}
}else{
$current_array=$this->initArray();
if($this->current_pagefor($i=0;$i$current_array[$i]=$i+1;
}
}
elseif($this->current_pagepageNums&&$this->current_page>$this->pageNums-$this->sub_pages+1){
for($i=0;$i$current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
}
}else{
for($i=0;$i$current_array[$i]=$this->current_page-2+$i;
}
}
}

return$current_array;
}


functionsubPageCss1(){
$subPageCss1Str="";
$subPageCss1Str.="共".$this->nums."條記錄,";
$subPageCss1Str.="每頁顯示".$this->each_disNums."條,";
$subPageCss1Str.="當前第".$this->current_page."/".$this->pageNums."頁";
if($this->current_page>1){
$firstPageUrl=$this->subPage_link."1";
$prewPageUrl=$this->subPage_link.($this->current_page-1);
$subPageCss1Str.="[首頁]";
$subPageCss1Str.="[上一頁]";
}else{
$subPageCss1Str.="[首頁]";
$subPageCss1Str.="[上一頁]";
}

if($this->current_pagepageNums){
$lastPageUrl=$this->subPage_link.$this->pageNums;
$nextPageUrl=$this->subPage_link.($this->current_page+1);
$subPageCss1Str.="[下一頁]";
$subPageCss1Str.="[尾頁]";
}else{
$subPageCss1Str.="[下一頁]";
$subPageCss1Str.="[尾頁]";
}

return$subPageCss1Str;

}


functionsubPageCss2(){
$subPageCss2Str="";
$subPageCss2Str.="當前第".$this->current_page."/".$this->pageNums."頁";

if($this->current_page>1){
$firstPageUrl=$this->subPage_link."1";
$prewPageUrl=$this->subPage_link.($this->current_page-1);
$subPageCss2Str.="[首頁]";
$subPageCss2Str.="[上一頁]";
}else{
$subPageCss2Str.="[首頁]";
$subPageCss2Str.="[上一頁]";
}

$a=$this->construct_num_Page();
for($i=0;$i$s=$a[$i];
if($s==$this->current_page){
$subPageCss2Str.="[".$s."]";
}else{
$url=$this->subPage_link.$s;
$subPageCss2Str.="[".$s."]";
}
}

if($this->current_pagepageNums){
$lastPageUrl=$this->subPage_link.$this->pageNums;
$nextPageUrl=$this->subPage_link.($this->current_page+1);
$subPageCss2Str.="[下一頁]";
$subPageCss2Str.="[尾頁]";
}else{
$subPageCss2Str.="[下一頁]";
$subPageCss2Str.="[尾頁]";
}
return$subPageCss2Str;
}
}


//測試一下,看看兩種不同效果
$t=newPage(10,100,$_GET['p'],5,'cls_page.php?p=');
echo$t->subPageCss2();
echo"
";
echo$t->subPageCss1();
?>


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2524/viewspace-2802754/,如需轉載,請註明出處,否則將追究法律責任。

相關文章