search
HomeBackend DevelopmentPHP TutorialPHP paging principle and PHP paging code comprehensive examples

  1. /**
  2. * PHP pagination code
  3. **/
  4. /**
  5. * Link to database
  6. * bbs.it-home.org
  7. * @param string $strHost database server host address
  8. * @param string $strAccount database account
  9. * @param string $strPassword database password
  10. * @return resource
  11. **/
  12. function mysqlConnect($strHost,$strAccount,$strPassword,$strDBname)
  13. {
  14. $strHost=trim ($strHost);
  15. $strAcount=trim($strAccount);
  16. $strPassword=trim($strPassword);
  17. $resLink=mysql_connect($strHost,$strAccount,$strPassword);
  18. if(!$resLink)
  19. {
  20. return false;
  21. }
  22. else
  23. { //set names ... set according to the encoding of the database
  24. mysql_query('set names utf8',$resLink);
  25. $isValidate=mysql_select_db($strDBname,$resLink);
  26. if($isValidate)
  27. {
  28. return $resLink;
  29. }
  30. else
  31. {
  32. return false;
  33. }
  34. }
  35. }
  36. /**
  37. *Accept the current page number of paging and calculate the corresponding parameter value
  38. *Include: start page number $arrParameter['start']
  39. * End page number $arrParameter['end']
  40. * Total number of records $arrParameter['all' ]
  41. * Number of records displayed on each page $arrParameter['nums']
  42. * Number of links displayed on the page $arrParameter['links']
  43. * SQL statement to be queried $arrParameter['sql']
  44. * Type of paging bar $arrParameter['tag']
  45. *
  46. int $intPage Current page number value
  47. int $intNums Number of records displayed on each page
  48. int $intLinks Number of links displayed on the page
  49. string $strTablename Display data table in pages
  50. resource $resLink Data connection handle
  51. array
  52. **/
  53. function calculateParamester($intPage,$intNums,$intLinks, $strTablename,$resLink){
  54. $intPage=(int)$intPage;
  55. $intNums=(int)$intNums;
  56. $intLinks=(int)$intLinks;
  57. //When the number of displayed links is not an odd number, adjust it to an odd number.
  58. if($intLinks % 2 == 0){
  59. $intLinks--;
  60. }
  61. //Adjust to 10 when the number of records displayed on each page is not greater than 0
  62. if($intNums $intNums =10;
  63. }
  64. //Calculate the total number of pages
  65. $strSql1="select count(*) as num from `{$strTablename}`";
  66. $resObj1=mysql_query($strSql1,$resLink);
  67. $arrObj1 =mysql_fetch_assoc($resObj1);
  68. $intAllRecords=$arrObj1['num'];
  69. $intAllPage=ceil($intAllRecords/$intNums);
  70. //The first parameter of the sql statement limit keyword
  71. $intOffset=( $intPage-1)*$intNums;
  72. //Only display the previous page and next page, that is, the number of links displayed is not greater than 0
  73. if($intLinks $strSql2="select * from ` {$strTablename}` limit {$intOffset},{$intNums}";
  74. $arrParameter['start']=null;
  75. $arrParameter['end']=null;
  76. $arrParameter['page']=$intPage ;
  77. $arrParameter['nums']=$intNums;
  78. $arrParameter['links']=null;
  79. $arrParameter['all']=$intAllPage;
  80. $arrParameter['sql']=$strSql2;
  81. $ arrParameter['tag']=1;
  82. //When the paging barcode is displayed, that is, the number of links displayed is greater than 0
  83. }else{
  84. //When the total records are greater than 0
  85. if($intAllPage > 0){
  86. //Determine the value of the current page number
  87. if($intPage $intPage=1;
  88. }
  89. if($intPage >= $intAllPage){
  90. $intPage=$intAllPage;
  91. }
  92. $ intHalfLinks=floor($intLinks/2);
  93. //Calculate the value of the start page number
  94. $intStartPage=$intPage-$intHalfLinks;
  95. if($intStartPage $intStartPage=1;
  96. }
  97. if( ($intAllPage-$intPage) //$intStartPage=$intPage-$intHalfLinks-($intHalfLinks-($intAllPage-$intPage));
  98. //$intStartPage=$intPage-$intHalfLinks- $intHalfLinks+$intAllPage-$intPage;
  99. $intStartPage=$intAllPage-2*$intHalfLinks;
  100. }
  101. //Calculate the value of the end page number
  102. $intEndPage=$intPage+$intHalfLinks;
  103. if($intEndPage "select * from `{$strTablename}` limit {$intOffset},{$intNums}";
  104. $arrParameter['start']=$intStartPage;
  105. $arrParameter['end']=$intEndPage;
  106. $arrParameter[ 'page']=$intPage;
  107. $arrParameter['nums']=$intNums;
  108. $arrParameter['links']=$intLinks;
  109. $arrParameter['all']=$intAllPage;
  110. $arrParameter['sql ']=$strSql2;
  111. $arrParameter['tag']=2;
  112. //When the total records are equal to 0
  113. }else{
  114. $arrParameter['start']=null;
  115. $arrParameter['end' ]=null;
  116. $arrParameter['page']=null;
  117. $arrParameter['nums']=null;
  118. $arrParameter['links']=null;
  119. $arrParameter['all']=null;
  120. $ arrParameter['sql']=null;
  121. $arrParameter['tag']=3;
  122. }
  123. }
  124. return $arrParameter;
  125. }
  126. /**
  127. * Create paging bar
  128. *
  129. * @param int $intPage The currently displayed page number value
  130. * @param int $intStartPage The starting page number
  131. * @param int $intEndPage The ending page number
  132. * @param int $intAllRecords The total number of records
  133. * @param int $intTag paging bar type tag
  134. * @return string
  135. **/
  136. function createPagingItem($intPage,$intStartPage,$intEndPage,$intAllPage,$intTag){
  137. $strPageItem='';
  138. //Only display the previous page and the next page. The number of links displayed is not greater than 0
  139. if($intTag == 1){
  140. if($intAllPage $strPageItem.='Homepage  Last Page';
  141. }else{
  142. if( $intPage == 1){
  143. $strPageItem.="Home  Previous page";
  144. $strPageItem.="  ";
  145. }else{
  146. $strPageItem.="Homepage";
  147. $strPageItem.="  ";
  148. $strPageItem.="Previous page";
  149. $strPageItem.="  ";
  150. }
  151. if($intPage == $intAllPage){
  152. $strPageItem.="Next page   Last page";
  153. }else{
  154. $strPageItem.="Next page";
  155. $strPageItem.="  ";
  156. $strPageItem.="Last page";
  157. }
  158. }
  159. }
  160. //When displaying paging barcode, the number of links displayed is greater than 0
  161. if($intTag == 2){
  162. if($intPage == 1){
  163. $strPageItem.="Homepage   Previous page";
  164. $strPageItem.="  ";
  165. }else{
  166. $strPageItem.="Homepage";
  167. $ strPageItem.="  ";
  168. $strPageItem.="Previous page";
  169. $ strPageItem.="  ";
  170. }
  171. for($i=$intStartPage;$i if($i == $intPage){
  172. $strPageItem.=$i ;
  173. }else{
  174. $strPageItem.="[".$i."]";
  175. }
  176. $strPageItem.="   ";
  177. }
  178. if($intPage == $intAllPage){
  179. $strPageItem.="Next page  Last page";
  180. }else{
  181. $strPageItem.="Next page";
  182. $strPageItem.="  ";
  183. $strPageItem.="Last page";
  184. }
  185. }
  186. //When the total records are equal to 0
  187. if($intTag == 3){
  188. $strPageItem.='Home page =mysql_query($strSql,$resLink);
  189. $arrObj=array();
  190. $strOutPutData='';
  191. $arrFieldsCode=array_keys($arrFields);
  192. while(@$arrRow=mysql_fetch_assoc($resObj)){
  193. $arrObj[]=$arrRow;
  194. }
  195. $strOutPutData.="";
  196. $strOutPutData.='
  197. ';
  198. foreach($arrFieldsCode as $strVal){
  199. $strOutPutData.="
  200. ";
  201. }
  202. $strOutPutData.="
  203. ";
  204. foreach($arrObj as $arrVal){
  205. $strOutPutData.="
  206. ";
  207. foreach($arrFieldsCode as $strVal){
  208. $strOutPutData.="
  209. ";
  210. }
  211. $strOutPutData.="
  212. ";
  213. }
  214. $strOutPutData.="
  215. ".$arrFields[trim($strVal)]."
    ".$arrVal[trim($strVal)]."
    ";
  216. return $strOutPutData;
  217. }
  218. // Connect and select the database
  219. // Note: You should modify the database account and password as well as the database name
  220. $resLink=mysqlConnect('localhost','root','root','ztlibrary');
  221. // Find out Note on paging parameters: You should modify the data table name
  222. $arrParameter=calculateParamester(@$_GET['page']?$_GET['page']:1,
  223. 10,5,'book_info',$resLink);
  224. / /The data to be displayed is composed of the field name of the table and its key value. The Chinese interpretation of the field name is its element value
  225. //Note: You should modify the following array according to your data table
  226. $arrFields=array('Id_code' =>'Book_name'=>'Book name',
  227. 'Book_ISBN'=>'ISBN','Contribute_man'=>'Source','Issue_time'=>'Publication time' ,'Storing_time'=>'Storing time');
  228. ?>
  229. li>"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  230. php paging demonstration--www.yuju100.com
  231. < ;div>
  232. //Output paging data
  233. echo outPutData($arrParameter['sql'],$arrFields,$resLink);
  234. ?>
  • < ;?php
  • //Display paging bar
  • echo createPagingItem($arrParameter['page'],$arrParameter['start'],$arrParameter['end'],
  • $arrParameter['all'],$arrParameter[' tag']);
  • ?>
  • 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
    When would you use a trait versus an abstract class or interface in PHP?When would you use a trait versus an abstract class or interface in PHP?Apr 10, 2025 am 09:39 AM

    In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

    What is a Dependency Injection Container (DIC) and why use one in PHP?What is a Dependency Injection Container (DIC) and why use one in PHP?Apr 10, 2025 am 09:38 AM

    Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

    Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Apr 10, 2025 am 09:37 AM

    SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

    How does PHP handle file uploads securely?How does PHP handle file uploads securely?Apr 10, 2025 am 09:37 AM

    PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

    What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?Apr 10, 2025 am 09:33 AM

    In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.

    What is Content Security Policy (CSP) header and why is it important?What is Content Security Policy (CSP) header and why is it important?Apr 09, 2025 am 12:10 AM

    CSP is important because it can prevent XSS attacks and limit resource loading, improving website security. 1.CSP is part of HTTP response headers, limiting malicious behavior through strict policies. 2. The basic usage is to only allow loading resources from the same origin. 3. Advanced usage can set more fine-grained strategies, such as allowing specific domain names to load scripts and styles. 4. Use Content-Security-Policy-Report-Only header to debug and optimize CSP policies.

    What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used?What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used?Apr 09, 2025 am 12:09 AM

    HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

    What is HTTPS and why is it crucial for web applications?What is HTTPS and why is it crucial for web applications?Apr 09, 2025 am 12:08 AM

    HTTPS is a protocol that adds a security layer on the basis of HTTP, which mainly protects user privacy and data security through encrypted data. Its working principles include TLS handshake, certificate verification and encrypted communication. When implementing HTTPS, you need to pay attention to certificate management, performance impact and mixed content issues.

    See all articles

    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 agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment