php分頁類代碼,可自訂參數php分頁代碼

來源:互聯網
上載者:User
  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 < $this->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<1){
  93. $startPage = 1;
  94. }
  95. $stopPage = $startPage+($this->link_num - 1);
  96. if($stopPage> $this->page_count){
  97. $stopPage = $this->page_count;
  98. }
  99. $countStr = "共".$this->records."條記錄";
  100. $currStr = "".$this->page."/".$this->page_count."頁 ";
  101. $beginLink = "linkUrl."?page=1".$this->urlPara."'>首頁";
  102. $preLink = "linkUrl."?page=".($this->page - 1).$this->urlPara."'>上一頁";
  103. $nextLink = "linkUrl."?page=".($this->page + 1).$this->urlPara."'>下一頁";
  104. $noPreLink = "上一頁";
  105. $noNextLink = "下一頁";
  106. $endLink = "linkUrl."?page=".$this->page_count.$this->urlPara."'>尾頁";
  107. if($this->page > ($linkPage + 1) && $this->page_count > $this->link_num){
  108. $currPage .= "linkUrl."?page=1".$this->urlPara."'>1";
  109. }
  110. for($i=$startPage;$i<=$stopPage;$i++){
  111. if($i == $this->page){
  112. $currPage .= "".$i."";
  113. }
  114. else{
  115. $currPage .= "linkUrl."?page=".$i.$this->urlPara."'>".$i."";
  116. }
  117. }
  118. if(($this->page_count - $this->page) > $linkPage && $this->page_count > $this->link_num){
  119. $currPage .= "....$this->linkUrl."?page=".$this->page_count.$this->urlPara."'>".$this->page_count."";
  120. }
  121. $jumpPage .= " ";
  122. $jumpPage .= "linkUrl
  123. ."?page='+document.getElementById('page_text_".$this->linkUrlNum."').value+'".$this->urlPara
  124. ."'\" name='page_submit' value='GO' />";
  125. $this->linkUrlNum++;
  126. if(!empty($this->linkFormat)){
  127. $tempLinkFormat = $this->linkFormat;
  128. $tempLinkFormat = str_replace("總記錄", $countStr, $tempLinkFormat);
  129. $tempLinkFormat = str_replace("頁次", $currStr, $tempLinkFormat);
  130. if($this->page_count > 1){
  131. $tempLinkFormat = str_replace("首頁", $beginLink, $tempLinkFormat);
  132. if($this->page > 1){
  133. $tempLinkFormat = str_replace("上一頁", $preLink, $tempLinkFormat);
  134. }
  135. else{
  136. $tempLinkFormat = str_replace("上一頁", $noPreLink, $tempLinkFormat);
  137. }
  138. if($this->page < $this->page_count){
  139. $tempLinkFormat = str_replace("下一頁", $nextLink, $tempLinkFormat);
  140. }
  141. else{
  142. $tempLinkFormat = str_replace("下一頁", $noNextLink, $tempLinkFormat);
  143. }
  144. $tempLinkFormat = str_replace("尾頁", $endLink, $tempLinkFormat);
  145. $tempLinkFormat = str_replace("分頁", $currPage, $tempLinkFormat);
  146. $tempLinkFormat = str_replace("跳轉", $jumpPage, $tempLinkFormat);
  147. }
  148. else{
  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. $tempLinkFormat = str_replace("跳轉", "", $tempLinkFormat);
  155. }

  156. }

  157. $this->pagestring.=$countStr." ".$currStr;
  158. if($this->page_count > 1){
  159. $this->pagestring.=$beginLink;
  160. if($this->page > 1){
  161. $this->pagestring.=$preLink;
  162. }
  163. $this->pagestring.=$currPage;
  164. if($this->page < $this->page_count){
  165. $this->pagestring.=$nextLink;
  166. }
  167. $this->pagestring.=$endLink.$jumpPage;
  168. }
  169. }

  170. /* 建立翻頁連結字串 end */

  171. /* 擷取資料 begin */

  172. private function fetch_data() {

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

  178. /* 擷取資料 end */

  179. /* 建立分頁 begin */

  180. public function create_page() {

  181. $this->set_page();
  182. $this->get_records();
  183. $this->page_count = ceil($this->records / $this->page_size);
  184. $this->page_link();
  185. $this->fetch_data();
  186. }

  187. /* 建立分頁 end */

  188. function __destruct() {

  189. }

  190. /* 函數定義(類方法) end */

  191. }
  192. /*
  193. 調用方法
  194. include_once 'PageLink.php';
  195. $pageLink = new PageLink(); //執行個體化對象
  196. $pageLink->set("table", "*(或欄位)", "條件", "排序", "資料條數", "每頁連結數", "頁面(list.php)", "除?page=1外的其他參數(&id=1&name=test)");//傳入參數
  197. $pageLink->create_page();//建立分頁
  198. $list = $pageLink->get();//獲得資料
  199. echo $list[0];//分頁連結
  200. print_r($list[1]); //列印出資料,或按你的需要迴圈出來
  201. */

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.