萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP實現對數組分頁處理實例詳解

PHP實現對數組分頁處理實例詳解

小編推薦的這篇文章介紹了PHP實現對數組分頁處理實例詳解,非常實用,有興趣的同學快來看看吧  代碼如下復制代碼

<?php

classPaginationArray{

 public$pageArray=array();//數組

 public$pageSize=10;//每頁顯示記錄數

 public$current= 1;//當前頁

 private$total=0;//總頁數

 private$prev=0;//上一頁

 private$next=0;//下一頁

 public$argumetsOther='';//設置參數

 function__construct($array=array(),$pageSize=10,$current=1){

 $this->pageArray=$array;

 $this->pageSize=$pageSize;

 $this->current=$current;

 }

 /*通過數組進行初始化

 *

 * 數組為關聯數組,參數索引為pageArray,pageSize,current

 *

 */

 functionsetArguments($arr){

 if(is_array($arr)){

  $this->pageArray=$arr['pageArray'];

  $this->pageSize=$arr['pageSize'];

  $this->current=$arr['current'];

 }else{

  return;

 }

 }

 //返回鏈接

 functionpage(){

 $_return=array();

 /*calculator*/

 $this->total=ceil(Count($this->pageArray)/$this->pageSize);

 $this->prev=(($this->current-1)<= 0="" this-="">current-1));

 $this->next=(($this->current+1)>=$this->total ?$this->total:$this->current+1);

 $current=($this->current>($this->total)?($this->total):$this->current);

 $start=($this->current-1)*$this->pageSize;

 $arrleng=count($this->pageArray);

 for($i=$start;$i<($start+$this->pageSize);$i++){

  if($i>=$arrleng)break;

  array_push($_return,$this->pageArray[$i]);

 }

 $pagearray["source"]=$_return;

 $pagearray["links"]=$this->linkStyle(2);

 return$pagearray;

 }

 //鏈接的樣式

 privatefunctionlinkStyle($number=1){

 $linkStyle='';

 switch($number){

  case1:

  $linkStyle="first prev next end";

  break;

  case2:

  $linkStyle="首頁 上一頁  下一頁  尾頁";

  break;

 }

 return$linkStyle;

 }

}

//調用的實例

/*

header('Content-Type: text/html;charset=utf-8');

$array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");

$page= isset($_GET['page'])? $_GET['page'] : 1 ;

$arrayPage = new PaginationArray($array,"5",$page);

$r = $arrayPage->page();

foreach($r["source"] as $s){

 echo $s.'
';

}

echo $r["links"];

*/

?>

copyright © 萬盛學電腦網 all rights reserved