關于數組的分頁函數,用數組進行分頁的好處是可以方便的進行聯合多表查詢,只需要將查詢的結果放在數組中就可以了以下是數組分頁的函數,函數page_array用于數組的分頁,函數show_array用于分頁函數的操作及顯示,需要配合使用.兩個函數通過全局變量$countpage發生聯系,此變量用于跟蹤總頁碼數.
<?php
/** * 數組分頁函數 核心函數 array_slice * 用此函數之前要先將數據庫里面的所有數據按一定的順序查詢出來存入數組中 * $count 每頁多少條數據 * $page 當前第幾頁 * $array 查詢出來的所有數組 * order 0 - 不變 1- 反序 */ function page_array($count,$page,$array,$order){ global $countpage; #定全局變量 $page=(empty($page))?'1':$page; #判斷當前頁面是否為空 如果為空就表示為第一頁面 $start=($page-1)*$count; #計算每次分頁的開始位置 if($order==1){ $array=array_reverse($array); } $totals=count($array); $countpage=ceil($totals/$count); #計算總頁面數 $pagedata=array(); $pagedata=array_slice($array,$start,$count); return $pagedata; #返回查詢數據
}
/** * 分頁及顯示函數 * $countpage 全局變量,照寫 * $url 當前url */
function show_array($countpage,$url){ $page=empty($_GET['page'])?1:$_GET['page']; if($page > 1){ $uppage=$page-1; }else{ $uppage=1; } if($page < $countpage){ $nextpage=$page+1; }else{ $nextpage=$countpage; } $str='<div style="border:1px; width:300px; height:30px; color:#9999CC">'; $str.="<span>共 {$countpage} 頁 / 第 {$page} 頁</span>"; $str.="<span><a href='$url?page=1'> 首頁 </a></span>"; $str.="<span><a href='$url?page={$uppage}'> 上一頁 </a></span>"; $str.="<span><a href='$url?page={$nextpage}'>下一頁 </a></span>"; $str.="<span><a href='$url?page={$countpage}'>尾頁 </a></span>"; $str.='</div>'; return $str;
}
?>
<?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"];
*/
?>