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
    Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

    PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

    How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

    In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

    Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

    The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

    Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

    Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

    What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

    Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

    How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

    Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

    What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

    Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

    How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

    Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

    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

    Video Face Swap

    Video Face Swap

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

    Hot Tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor