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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles