class PageLink {
- /* 分頁顯示參數設定 */
- private $db_table = "";
- private $db_table_field = "";//要顯示的資料庫中的欄位
- private $condition = ""; //查詢條件
- private $sort = ""; //排序條件
- private $page_size = 0; //每頁顯示的記錄數目
- private $link_num = 0; //顯示頁碼連結的數目
- private $page = 1; //當前頁碼
- private $records = 0; //表中記錄總數
- private $page_count = 0; //總頁數
- private $pagestring = ""; //前後分頁連結字串
- private $linkUrl = ""; //當前頁面路徑
- private $urlPara = ""; //當前頁面url參數
- private $linkUrlNum = 1;
- /*
- * 自訂分頁
- */
- private $linkFormat = "";
/* 擷取的資料 */
- private $page_data = "";//從資料庫中擷取的資料,
//$page_data為二維數組接收
private $dbHelper;
- /* 變數定義部分 end */
/* 函數定義(類方法) begin */
function __construct() {include_once 'DBHelper/DBHelper.php';//這是資料庫操作類
- $this->dbHelper = new DBHelper();
- }
/*
- * 設定分頁資訊 begin
- * @param $db_table 表
- * @param $db_table_field 欄位
- * @param $condition 條件
- * @param $sort 排序
- * @param $page_size 顯示條數
- * @param $link_num 數字鏈接數
- * @param $url 頁面路徑
- * @param $para url參數
- */
- public function set($db_table, $db_table_field, $condition, $sort, $page_size, $link_num, $url, $para) {
- $this->db_table = $db_table;//表名
- $this->db_table_field = $db_table_field; //欄位數組,
- $this->condition = $condition; //排序條件
- $this->sort = $sort; //排序條件
- //將要顯示的欄位名稱
- //寫入該數組
- /* db參數設定 end */
/* 分頁參數設定 begin */
- $this->page_size = $page_size;//每頁顯示記錄的數目
- $this->link_num = $link_num;//顯示翻頁連結的數目
- $this->linkUrl = $url;
- $this->urlPara = $para;
- /* 分頁參數設定 end */
- }
/* 設定分頁資訊 end */
/* 擷取分頁連結資料 begin */
public function get() {
- $page_data[0] = $this->pagestring;
- $page_data[1] = $this->page_data;
- return $page_data;
- }
/* 擷取分頁連結資料 end */
/* 頁碼處理 begin */
private function set_page() {
- if (isset($_REQUEST["page"])) {
- $this->page = intval($_REQUEST["page"]);
- } else {
- $this->page = 1;
- }
- }
/* 頁碼處理 end */
/* 擷取db中記錄的數目 begin */
private function get_records() {
- $this->records = $this->dbHelper->counts($this->db_table, $this->condition);
- }
/* 擷取db中記錄的數目 end */
/* 建立翻頁連結字串 begin */
private function page_link() {
- $checkPage = intval($this->page / $this->link_num);
- $startPage = 1;
- $stopPage = 1;
- if($checkPage == 0 && $this->page < $this->link_num){
- $startPage = 1;
- }
- $linkPage = ($this->link_num / 2);
- if($this->page > $linkPage){
- $startPage = $this->page - $linkPage;
- }
- if(($startPage+$this->link_num)>$this->page_count){
- $startPage = $this->page_count - $this->link_num + 1;
- }
- if($startPage<1){
- $startPage = 1;
- }
- $stopPage = $startPage+($this->link_num - 1);
- if($stopPage> $this->page_count){
- $stopPage = $this->page_count;
- }
- $countStr = "共".$this->records."條記錄";
- $currStr = "".$this->page."/".$this->page_count."頁 ";
- $beginLink = "linkUrl."?page=1".$this->urlPara."'>首頁";
- $preLink = "linkUrl."?page=".($this->page - 1).$this->urlPara."'>上一頁";
- $nextLink = "linkUrl."?page=".($this->page + 1).$this->urlPara."'>下一頁";
- $noPreLink = "上一頁";
- $noNextLink = "下一頁";
- $endLink = "linkUrl."?page=".$this->page_count.$this->urlPara."'>尾頁";
- if($this->page > ($linkPage + 1) && $this->page_count > $this->link_num){
- $currPage .= "linkUrl."?page=1".$this->urlPara."'>1";
- }
- for($i=$startPage;$i<=$stopPage;$i++){
- if($i == $this->page){
- $currPage .= "".$i."";
- }
- else{
- $currPage .= "linkUrl."?page=".$i.$this->urlPara."'>".$i."";
- }
- }
- if(($this->page_count - $this->page) > $linkPage && $this->page_count > $this->link_num){
- $currPage .= "....$this->linkUrl."?page=".$this->page_count.$this->urlPara."'>".$this->page_count."";
- }
- $jumpPage .= " ";
- $jumpPage .= "linkUrl
- ."?page='+document.getElementById('page_text_".$this->linkUrlNum."').value+'".$this->urlPara
- ."'\" name='page_submit' value='GO' />";
- $this->linkUrlNum++;
- if(!empty($this->linkFormat)){
- $tempLinkFormat = $this->linkFormat;
- $tempLinkFormat = str_replace("總記錄", $countStr, $tempLinkFormat);
- $tempLinkFormat = str_replace("頁次", $currStr, $tempLinkFormat);
- if($this->page_count > 1){
- $tempLinkFormat = str_replace("首頁", $beginLink, $tempLinkFormat);
- if($this->page > 1){
- $tempLinkFormat = str_replace("上一頁", $preLink, $tempLinkFormat);
- }
- else{
- $tempLinkFormat = str_replace("上一頁", $noPreLink, $tempLinkFormat);
- }
- if($this->page < $this->page_count){
- $tempLinkFormat = str_replace("下一頁", $nextLink, $tempLinkFormat);
- }
- else{
- $tempLinkFormat = str_replace("下一頁", $noNextLink, $tempLinkFormat);
- }
- $tempLinkFormat = str_replace("尾頁", $endLink, $tempLinkFormat);
- $tempLinkFormat = str_replace("分頁", $currPage, $tempLinkFormat);
- $tempLinkFormat = str_replace("跳轉", $jumpPage, $tempLinkFormat);
- }
- else{
- $tempLinkFormat = str_replace("首頁", "", $tempLinkFormat);
- $tempLinkFormat = str_replace("上一頁", "", $tempLinkFormat);
- $tempLinkFormat = str_replace("下一頁", "", $tempLinkFormat);
- $tempLinkFormat = str_replace("尾頁", "", $tempLinkFormat);
- $tempLinkFormat = str_replace("分頁", "", $tempLinkFormat);
- $tempLinkFormat = str_replace("跳轉", "", $tempLinkFormat);
- }
}
- $this->pagestring.=$countStr." ".$currStr;
- if($this->page_count > 1){
- $this->pagestring.=$beginLink;
- if($this->page > 1){
- $this->pagestring.=$preLink;
- }
- $this->pagestring.=$currPage;
- if($this->page < $this->page_count){
- $this->pagestring.=$nextLink;
- }
- $this->pagestring.=$endLink.$jumpPage;
- }
- }
/* 建立翻頁連結字串 end */
/* 擷取資料 begin */
private function fetch_data() {
- if ($this->records) {
- $limit = ($this->page - 1) * $this->page_size . ",$this->page_size";
- $this->page_data = $this->dbHelper->fetch($this->db_table, $this->condition, $this->sort, $limit);
- }
- }
/* 擷取資料 end */
/* 建立分頁 begin */
public function create_page() {
- $this->set_page();
- $this->get_records();
- $this->page_count = ceil($this->records / $this->page_size);
- $this->page_link();
- $this->fetch_data();
- }
/* 建立分頁 end */
function __destruct() {
}
/* 函數定義(類方法) end */
- }
- /*
- 調用方法
- include_once 'PageLink.php';
- $pageLink = new PageLink(); //執行個體化對象
- $pageLink->set("table", "*(或欄位)", "條件", "排序", "資料條數", "每頁連結數", "頁面(list.php)", "除?page=1外的其他參數(&id=1&name=test)");//傳入參數
- $pageLink->create_page();//建立分頁
- $list = $pageLink->get();//獲得資料
- echo $list[0];//分頁連結
- print_r($list[1]); //列印出資料,或按你的需要迴圈出來
- */
複製代碼 |