-
-
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 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$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 = "首頁";
- $preLink = "上一頁";
- $nextLink = "下一頁";
- $noPreLink = "上一頁";
- $noNextLink = "下一頁";
- $endLink = "尾頁";
- if($this->page > ($linkPage 1) && $this->page_count > $this->link_num){
- $currPage .= "1";
- }
- for($i=$startPage;$iif($i == $this-> ;頁){
- $currPage .= "".$i."";
- }
- else{
- $currPage . = "".$i."";
- }
- }
- if(($this->page_count - $this->page) > $linkPage && $this->page_count > $this->link_num){
- $currPage .= "..."。 $this->page_count."";
- }
- $jumpPage .= "<輸入型別='文字' id='page_text_".$this->linkUrlNum.
- "' onmouseover='選擇();' size='1' maxlength='4' onkeyup="this.value=this.value.replace(/\D /g,'');" //>";
- $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("頁次", $ currStrStr , $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 page_count){
- $tempLinkFormat = str_replace( "下一頁" , $nextLink, $tempLinkFormat);
- }
- else{
- $tempLinkFormat = str_replace("下一頁", $noNextLink, $tempLinkFormat);
- }
- $tempLinkFormplace( "尾頁", $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 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]); //印出數據,或依你的需求循環出來
- */
-
複製程式碼
|