Home Backend Development PHP Tutorial PHP paging class code, customizable parameters PHP paging code

PHP paging class code, customizable parameters PHP paging code

Jul 25, 2016 am 08:52 AM

  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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles