Home Backend Development PHP Tutorial PHP paging principle and PHP paging code comprehensive examples

PHP paging principle and PHP paging code comprehensive examples

Jul 25, 2016 am 08:52 AM

  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 <= 0){
  63. $intNums =10;
  64. }
  65. //Calculate the total number of pages
  66. $strSql1="select count(*) as num from `{$strTablename}`";
  67. $resObj1=mysql_query($strSql1,$resLink);
  68. $arrObj1 =mysql_fetch_assoc($resObj1);
  69. $intAllRecords=$arrObj1['num'];
  70. $intAllPage=ceil($intAllRecords/$intNums);
  71. //The first parameter of the sql statement limit keyword
  72. $intOffset=( $intPage-1)*$intNums;
  73. //Only display the previous page and next page, that is, the number of links displayed is not greater than 0
  74. if($intLinks <= 0){
  75. $strSql2="select * from ` {$strTablename}` limit {$intOffset},{$intNums}";
  76. $arrParameter['start']=null;
  77. $arrParameter['end']=null;
  78. $arrParameter['page']=$intPage ;
  79. $arrParameter['nums']=$intNums;
  80. $arrParameter['links']=null;
  81. $arrParameter['all']=$intAllPage;
  82. $arrParameter['sql']=$strSql2;
  83. $ arrParameter['tag']=1;
  84. //When the paging barcode is displayed, that is, the number of links displayed is greater than 0
  85. }else{
  86. //When the total records are greater than 0
  87. if($intAllPage > 0){
  88. //Determine the value of the current page number
  89. if($intPage <= 0){
  90. $intPage=1;
  91. }
  92. if($intPage >= $intAllPage){
  93. $intPage=$intAllPage;
  94. }
  95. $ intHalfLinks=floor($intLinks/2);
  96. //Calculate the value of the start page number
  97. $intStartPage=$intPage-$intHalfLinks;
  98. if($intStartPage <= 0){
  99. $intStartPage=1;
  100. }
  101. if( ($intAllPage-$intPage) < $intHalfLinks){
  102. //$intStartPage=$intPage-$intHalfLinks-($intHalfLinks-($intAllPage-$intPage));
  103. //$intStartPage=$intPage-$intHalfLinks- $intHalfLinks+$intAllPage-$intPage;
  104. $intStartPage=$intAllPage-2*$intHalfLinks;
  105. }
  106. //Calculate the value of the end page number
  107. $intEndPage=$intPage+$intHalfLinks;
  108. if($intEndPage < $intLinks && $ intAllPage > "select * from `{$strTablename}` limit {$intOffset},{$intNums}";
  109. $arrParameter['start']=$intStartPage;
  110. $arrParameter['end']=$intEndPage;
  111. $arrParameter[ 'page']=$intPage;
  112. $arrParameter['nums']=$intNums;
  113. $arrParameter['links']=$intLinks;
  114. $arrParameter['all']=$intAllPage;
  115. $arrParameter['sql ']=$strSql2;
  116. $arrParameter['tag']=2;
  117. //When the total records are equal to 0
  118. }else{
  119. $arrParameter['start']=null;
  120. $arrParameter['end' ]=null;
  121. $arrParameter['page']=null;
  122. $arrParameter['nums']=null;
  123. $arrParameter['links']=null;
  124. $arrParameter['all']=null;
  125. $ arrParameter['sql']=null;
  126. $arrParameter['tag']=3;
  127. }
  128. }
  129. return $arrParameter;
  130. }
  131. /**
  132. * Create paging bar
  133. *
  134. * @param int $intPage The currently displayed page number value
  135. * @param int $intStartPage The starting page number
  136. * @param int $intEndPage The ending page number
  137. * @param int $intAllRecords The total number of records
  138. * @param int $intTag paging bar type tag
  139. * @return string
  140. **/
  141. function createPagingItem($intPage,$intStartPage,$intEndPage,$intAllPage,$intTag){
  142. $strPageItem='';
  143. //Only display the previous page and the next page. The number of links displayed is not greater than 0
  144. if($intTag == 1){
  145. if($intAllPage <= 0){
  146. $strPageItem.='Homepage Last Page';
  147. }else{
  148. if( $intPage == 1){
  149. $strPageItem.="Home Previous page";
  150. $strPageItem.=" ";
  151. }else{
  152. $strPageItem.="Homepage";
  153. $strPageItem.="  ";
  154. $strPageItem.="Previous page";
  155. $strPageItem.="  ";
  156. }
  157. if($intPage == $intAllPage){
  158. $strPageItem.="Next page   Last page";
  159. }else{
  160. $strPageItem.="Next page";
  161. $strPageItem.="  ";
  162. $strPageItem.="Last page";
  163. }
  164. }
  165. }
  166. //When displaying paging barcode, the number of links displayed is greater than 0
  167. if($intTag == 2){
  168. if($intPage == 1){
  169. $strPageItem.="Homepage   Previous page";
  170. $strPageItem.="  ";
  171. }else{
  172. $strPageItem.="Homepage";
  173. $ strPageItem.="  ";
  174. $strPageItem.="Previous page";
  175. $ strPageItem.="  ";
  176. }
  177. for($i=$intStartPage;$i<=$intEndPage;$i++){
  178. if($i == $intPage){
  179. $strPageItem.=$i ;
  180. }else{
  181. $strPageItem.="[".$i."]";
  182. }
  183. $strPageItem.="   ";
  184. }
  185. if($intPage == $intAllPage){
  186. $strPageItem.="Next page  Last page";
  187. }else{
  188. $strPageItem.="Next page";
  189. $strPageItem.="  ";
  190. $strPageItem.="Last page";
  191. }
  192. }
  193. //When the total records are equal to 0
  194. if($intTag == 3){
  195. $strPageItem.='Home page =mysql_query($strSql,$resLink);
  196. $arrObj=array();
  197. $strOutPutData='';
  198. $arrFieldsCode=array_keys($arrFields);
  199. while(@$arrRow=mysql_fetch_assoc($resObj)){
  200. $arrObj[]=$arrRow;
  201. }
  202. $strOutPutData.="";
  203. $strOutPutData.='
  204. ';
  205. foreach($arrFieldsCode as $strVal){
  206. $strOutPutData.="
  207. ";
  208. }
  209. $strOutPutData.="
  210. ";
  211. foreach($arrObj as $arrVal){
  212. $strOutPutData.="
  213. ";
  214. foreach($arrFieldsCode as $strVal){
  215. $strOutPutData.="
  216. ";
  217. }
  218. $strOutPutData.="
  219. ";
  220. }
  221. $strOutPutData.="
  222. ".$arrFields[trim($strVal)]."
    ".$arrVal[trim($strVal)]."
    ";
  223. return $strOutPutData;
  224. }
  225. // Connect and select the database
  226. // Note: You should modify the database account and password as well as the database name
  227. $resLink=mysqlConnect('localhost','root','root','ztlibrary');
  228. // Find out Note on paging parameters: You should modify the data table name
  229. $arrParameter=calculateParamester(@$_GET['page']?$_GET['page']:1,
  230. 10,5,'book_info',$resLink);
  231. / /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
  232. //Note: You should modify the following array according to your data table
  233. $arrFields=array('Id_code' =>'Book_name'=>'Book name',
  234. 'Book_ISBN'=>'ISBN','Contribute_man'=>'Source','Issue_time'=>'Publication time' ,'Storing_time'=>'Storing time');
  235. ?>
  236. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  237. php paging demonstration--www.yuju100.com
  238. < ;div>
  239. //Output paging data
  240. echo outPutData($arrParameter['sql'],$arrFields,$resLink);
  241. ?>
  • < ;?php
  • //Display paging bar
  • echo createPagingItem($arrParameter['page'],$arrParameter['start'],$arrParameter['end'],
  • $arrParameter['all'],$arrParameter[' tag']);
  • ?>
  • 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...

    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.

    How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

    Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

    See all articles