您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在PHP中實現一個數組分頁處理功能,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
具體代碼如下:
<?php class 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"]; */ ?>
關于怎么在PHP中實現一個數組分頁處理功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。