Home  >  Article  >  Backend Development  >  PHP paging class code, customizable parameters PHP paging code

PHP paging class code, customizable parameters PHP paging code

WBOY
WBOYOriginal
2016-07-25 08:52:34817browse
  1. class PageLink {

  2. /* Paging display parameter settings*/
  3. private $db_table = "";
  4. private $db_table_field = "";//Fields in the database to be displayed
  5. private $ condition = ""; //Query conditions
  6. private $sort = ""; //Sort conditions
  7. private $page_size = 0; //Number of records displayed on each page
  8. private $link_num = 0; //Display the number of page number links
  9. private $page = 1; //Current page number
  10. private $records = 0; //Total number of records in the table
  11. private $page_count = 0; //Total number of pages
  12. private $pagestring = ""; //Paging link characters before and after String
  13. private $linkUrl = ""; //Current page path
  14. private $urlPara = ""; //Current page url parameter
  15. private $linkUrlNum = 1;
  16. /*
  17. * Custom paging
  18. */
  19. private $linkFormat = "";

  20. /* Obtained data*/

  21. private $page_data = "";//Data obtained from the database,

  22. // $page_data is a two-dimensional array received

  23. private $dbHelper;

  24. /* Variable definition part end */

  25. /* Function definition (class method) begin * /

  26. function __construct() {include_once 'DBHelper/DBHelper.php';//This is the database operation class

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

  30. * Set paging information begin
  31. * @param $db_table table
  32. * @param $db_table_field field
  33. * @param $condition condition
  34. * @param $sort sort
  35. * @param $page_size display Number of items
  36. * @param $link_num number of numeric links
  37. * @param $url page path
  38. * @param $para url parameter
  39. */
  40. public function set($db_table, $db_table_field, $condition, $sort, $page_size, $link_num, $url, $para) {
  41. $this->db_table = $db_table;//Table name
  42. $this->db_table_field = $db_table_field; //Field array,
  43. $this->condition = $ condition; //Sort condition
  44. $this->sort = $sort; //Sort condition
  45. //The field name to be displayed
  46. //Write into the array
  47. /* db parameter setting end */

  48. /* Paging parameter setting begin */

  49. $this->page_size = $page_size;//The number of records displayed on each page
  50. $this->link_num = $link_num;//Display page turning links Number
  51. $this->linkUrl = $url;
  52. $this->urlPara = $para;
  53. /* Setting paging parameters end */
  54. }

  55. /* Setting paging informationend */

  56. /* Get paging link data begin */

  57. public function get() {

  58. $page_data[0] = $this->pagestring ;
  59. $page_data[1] = $this->page_data;
  60. return $page_data;
  61. }

  62. /* Get pagination link data end */

  63. private function set_page() {

  64. if (isset($_REQUEST["page"])) {
  65. $this->page = intval($ _REQUEST["page"]);
  66. } else {
  67. $this->page = 1;
  68. }
  69. }

  70. /* Page number processing end */

  71. < ;p>/* Get the number of records in db begin */

  72. private function get_records() {

  73. $this->records = $this->dbHelper->counts($ this->db_table, $this->condition);
  74. }

  75. /* Get the number of records in db end */

  76. /* Create Page turning link string begin */

  77. private function page_link() {

  78. $checkPage = intval($this->page / $this->link_num);
  79. $startPage = 1 ;
  80. $stopPage = 1;
  81. if($checkPage == 0 && $this->page < $this->link_num){
  82. $startPage = 1;
  83. }
  84. $linkPage = ($this-> link_num / 2);
  85. if($this->page > $linkPage){
  86. $startPage = $this->page - $linkPage;
  87. }
  88. if(($startPage+$this->link_num)> ;$this->page_count){
  89. $startPage = $this->page_count - $this->link_num + 1;
  90. }
  91. if($startPage<1){
  92. $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;$i<=$stopPage;$i++){
  110. if($i == $this->page){
  111. $currPage .= "".$i."";
  112. }
  113. else{
  114. $currPage .= "".$i."";
  115. }
  116. }
  117. if(($this->page_count - $this->page) > $linkPage && $this->page_count > $this->link_num){
  118. $currPage .= "...".$this->page_count."";
  119. }
  120. $jumpPage .= " ";
  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("頁次", $currStr, $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 < $this->page_count){
  138. $tempLinkFormat = str_replace("下一頁", $nextLink, $tempLinkFormat);
  139. }
  140. else{
  141. $tempLinkFormat = str_replace("下一頁", $noNextLink, $tempLinkFormat);
  142. }
  143. $tempLinkFormat = str_replace("尾頁", $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 < $this->page_count){
  164. $this->pagestring.=$nextLink;
  165. }
  166. $this->pagestring.=$endLink.$jumpPage;
  167. }
  168. }

  169. /* Create page turning link string end */

  170. /* Get data 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. /* Get data end */

  178. /* Create paging 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. /* Create pagination end */

  187. function __destruct() {

  188. }

  189. /* Function definition (class method) end */

  190. }
  191. /*
  192. Call method
  193. include_once 'PageLink.php';
  194. $pageLink = new PageLink() ; //Instantiate the object
  195. $pageLink->set("table", "*(or field)", "condition", "sort", "number of data items", "number of links per page", "page( list.php)", "Other parameters except ?page=1 (&id=1&name=test)");//Pass in parameters
  196. $pageLink->create_page();//Create paging
  197. $list = $ pageLink->get();//Get data
  198. echo $list[0];//Page link
  199. print_r($list[1]); //Print out the data, or loop it out as you need
  200. */

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn