PHP實現對數組分頁處理的方法

來源:互聯網
上載者:User
這篇文章主要介紹了PHP實現對數組分頁處理,結合執行個體形式分析了php封裝的數組分頁類定義與提示,需要的朋友可以參考下

具體如下:

最近用到了用數組資料分頁,這裡整理了一下,具體代碼如下:

<?phpclass PaginationArray{ 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 *  */ function setArguments($arr){ if (is_array($arr)){  $this->pageArray=$arr['pageArray'];  $this->pageSize=$arr['pageSize'];  $this->current=$arr['current']; }else{  return ; } } //返回連結 function page(){ $_return=array(); /*calculator*/ $this->total=ceil(Count($this->pageArray)/$this->pageSize); $this->prev=(($this->current-1)<= 0 ? "1":($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; } //連結的樣式 private function linkStyle($number=1){ $linkStyle=''; switch ($number){  case 1:  $linkStyle="<a href=\"?page=1\">first</a> <a href=\"?page={$this->prev}\">prev</a> <a href=\"?page={$this->next}\">next</a> <a href=\"?page={$this->total}\">end</a>";  break;  case 2:  $linkStyle="<a href=\"?page=1&{$this->argumetsOther}\">首頁</a> <a href=\"?page={$this->prev}&{$this->argumetsOther}\">上一頁</a> <a href=\"?page={$this->next}&{$this->argumetsOther}\">下一頁</a> <a href=\"?page={$this->total}&{$this->argumetsOther}\">尾頁</a>";  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.'<br />';}echo $r["links"];*/?>

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.