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
    PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

    PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

    PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

    PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

    PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

    PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

    PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

    PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

    PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

    The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

    Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

    PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

    What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

    In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

    Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

    The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

    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
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    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