首頁 >後端開發 >php教程 >php分頁類別程式碼,可自訂參數php分頁程式碼

php分頁類別程式碼,可自訂參數php分頁程式碼

WBOY
WBOY原創
2016-07-25 08:52:34878瀏覽
  1. class PageLink {

  2. /* 分頁顯示參數設定*/
  3. private $db_table = "";
  4. private $ db_table_field = "";//要顯示的資料庫中的欄位
  5. private $condition = ""; //查詢條件
  6. private $sort = ""; //排序條件
  7. private $page_size = 0 ; //每頁顯示的記錄數目
  8. private $link_num = 0; //顯示頁碼連結的數目
  9. private $page = 1; //目前頁碼
  10. private $records = 0; //表中記錄總數
  11. private $page_count = 0; //總頁數
  12. private $pagestring = ""; //前後分頁連結字串
  13. private $linkUrl = ""; //目前頁路徑
  14. private $urlPara = ""; //目前頁面url參數
  15. private $linkUrlNum = 1;
  16. /*
  17. * 自訂分頁
  18. */
  19. private $linkFormat = "" ;
  20. /* 取得的資料*/

  21. private $page_data = "";//從資料庫取得的數據,
  22. //$ page_data為二維陣列接收

  23. private $dbHelper;

  24. /* 變數定義部分end */
  25. /* 函式定義(類別方法) begin */

  26. function __construct() {include_once 'DBHelper/DBHelper.php';//這是資料庫操作類別

  27. $this->dbHelper = new DBHelper();
  28. }
  29. /*

  30. * 設定分頁資訊begin
  31. * @param $db_table 表
  32. * @param $db_table_field 欄位
  33. * @param $condition 條件
  34. * @param $sort 排序
  35. * @param $page_size 顯示條數
  36. * @param $link_num 數字鏈接數
  37. * @param $url 頁面路徑
  38. * @param $para url參數
  39. */
  40. public function set($db_table, $db_table_field, $condition, $sort, $page_size, $link_num, $url, $para) {
  41. $this->db_table = $db_table;///表名
  42. $this->db_table_field = $db_table_field; //欄位數組,
  43. $this->condition = $condition; //排序條件
  44. $this->sort = $sort; //排序條件
  45. //將要顯示的欄位名稱
  46. //寫入此陣列
  47. /* db參數設定end */
  48. /* 分頁參數設定begin */

  49. $this->page_size = $page_size;//每頁顯示記錄的數量
  50. $this->link_num = $link_num;//顯示翻頁連結的數量
  51. $this->linkUrl = $url ;
  52. $this->urlPara = $para;
  53. /* 分頁參數設定end */
  54. }
  55. /* 設定分頁訊息end */

  56. /* 取得分頁連結資料begin */

  57. public function get() {

  58. $page_data[0] = $this->pagestring;
  59. $ page_data[1] = $this->page_data;
  60. return $page_data;
  61. }
  62. /* 取得分頁連結資料end */

  63. /* 頁碼處理begin */

  64. private function set_page() {

  65. if (isset($_REQUEST["page"])) {
  66. $this->page = intval($_REQUEST["page"]);
  67. } else {
  68. $this->page = 1;
  69. }
  70. }
  71. /* 頁碼處理end */

  72. /* 取得db中記錄的數目begin */

  73. private function get_records() {

  74. $this->records = $ this->dbHelper->counts($this->db_table, $this->condition);
  75. }
  76. /* 取得db所記錄的數目end */

  77. /* 建立翻頁連結字串begin */

  78. private function page_link() {

  79. $checkPage = intval($this->page / $this- >link_num);
  80. $startPage = 1;
  81. $stopPage = 1;
  82. if($checkPage == 0 && $this->page link_num){
  83. $startPage = 1;
  84. }
  85. $linkPage = ($this->link_num / 2);
  86. if($this->page > $linkPage){
  87. $startPage = $this->page - $ linkPage;
  88. }
  89. if(($startPage $this->link_num)>$this->page_count){
  90. $startPage = $this->page_count - $this->link_num 1;
  91. }
  92. if($startPage$startPage = 1;
  93. }
  94. $stopPage = $startPage ($this->link_num - 1);
  95. if($stopPage> $this->page_count){
  96. $stopPage = $this->page_count;
  97. }
  98. $countStr = "共".$this->records."筆記錄";
  99. $currStr = "".$this->page."/" .$this- >page_count."頁";
  100. $beginLink = "首頁";
  101. $preLink = "上一頁";
  102. $nextLink = "下一頁";
  103. $noPreLink = "上一頁";
  104. $noNextLink = "下一頁";
  105. $endLink = "尾頁";
  106. if($this->page > ($linkPage 1) && $this->page_count > $this->link_num){
  107. $currPage .= "1";
  108. }
  109. for($i=$startPage;$iif($i == $this-> ;頁){
  110. $currPage .= "".$i."";
  111. }
  112. else{
  113. $currPage . = "".$i."";
  114. }
  115. }
  116. if(($this->page_count - $this->page) > $linkPage && $this->page_count > $this->link_num){
  117. $currPage .= "..."。 $this->page_count."";
  118. }
  119. $jumpPage .= "<輸入型別='文字' id='page_text_".$this->linkUrlNum.
  120. "' onmouseover='選擇();' size='1' maxlength='4' onkeyup="this.value=this.value.replace(/\D /g,'');" //>";
  121. $jumpPage .= "linkUrl
  122. ."?page=' document.getElementById('page_text_".$this->linkUrlNum." ').value ' ".$this->urlPara
  123. ."'" name='page_submit' value='GO' />";
  124. $this->linkUrlNum ;
  125. if( !empty($this-> linkFormat )){
  126. $tempLinkFormat = $this->linkFormat;
  127. $tempLinkFormat = str_replace("總記錄", $countStr, $tempLinkFormat);
  128. $tempLinkFormat = str_replace("頁次", $ currStrStr , $tempLinkFormat);
  129. if($this->page_count > 1){
  130. $tempLinkFormat = str_replace("首頁", $beginLink, $tempLinkFormat);
  131. if($this->page > 1){
  132. $tempLinkFormat = str_replace("上一頁", $preLink, $tempLinkFormat);
  133. }
  134. else{
  135. $ tempLinkFormat = str_replace("上一頁", $noPreLink, $tempLinkFormat);
  136. }
  137. if($this->page page_count){
  138. $tempLinkFormat = str_replace( "下一頁" , $nextLink, $tempLinkFormat);
  139. }
  140. else{
  141. $tempLinkFormat = str_replace("下一頁", $noNextLink, $tempLinkFormat);
  142. }
  143. $tempLinkFormplace( "尾頁", $endLink, $tempLinkFormat);
  144. $tempLinkFormat = str_replace("分頁", $currPage, $tempLinkFormat);
  145. $tempLinkFormat = str_replace("跳轉", $jumpPage , $tempLinkFormat) ;
  146. }
  147. else{
  148. $tempLinkFormat = str_replace("首頁", "", $tempLinkFormat);
  149. $tempLinkFormat = str_replace("上一頁", "", $tempLinkFormat);
  150. $tempLinkFormat = str_replace("下一頁", "", $tempLinkFormat);
  151. $tempLinkFormat = str_replace("尾頁", "", $tempLinkFormat);
  152. $tempLinkFormat = str_replace("分頁", "", $tempLinkFormat);
  153. $tempLinkFormat = str_replace("跳轉", "", $tempLinkFormat);
  154. }
  155. }

  156. $this ->pagestring.=$countStr." ".$currStr;
  157. if($this->page_count >; 1){
  158. $this->pagestring.=$beginLink;
  159. if($this->page > 1){
  160. $this->pagestring.=$preLink;
  161. }
  162. $this->pagestring.=$currPage;
  163. if($this->page page_count){
  164. $this->pagestring.=$nextLink;
  165. }
  166. $this->pagestring.=$endLink.$jumpPage;
  167. }
  168. }
  169. /* 建立翻頁連結字串end */

  170. /* 取得資料begin */

  171. private function fetch_data() {

  172. if ($this->records) {
  173. $limit = ($this->page - 1) * $this->page_size . ",$this->page_size";
  174. $this->page_data = $this->dbHelper->fetch($this->db_table, $this->condition, $this->sort, $limit);
  175. }
  176. }
  177. /* 取得資料end */

  178. /* 建立分頁begin */

  179. public function create_page() {

  180. $this ->set_page();
  181. $this->get_records();
  182. $this->page_count = ceil($this->records / $this->page_size);
  183. $this->page_link( );
  184. $this->fetch_data();
  185. }
  186. /* 建立分頁end */

  187. function __destruct() {

  188. }

  189. /* 函式定義(類別方法) end */

  190. }
  191. /*
  192. 呼叫方法
  193. include_once ' PageLink.php';
  194. $pageLink = new PageLink(); //實例化物件
  195. $pageLink->set("table", "*(或欄位)", "條件", "排序", "資料條數", "每頁連結數", "頁面(list.php)", "除?page=1以外的其他參數(&id=1&name=test)");//傳入參數
  196. $pageLink->create_page();//建立分頁
  197. $list = $pageLink->get();//取得資料
  198. echo $list[0];//分頁連結
  199. print_r($ list[1]); //印出數據,或依你的需求循環出來
  200. */
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn