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)

    Hot Topics

    Java Tutorial
    1662
    14
    PHP Tutorial
    1261
    29
    C# Tutorial
    1234
    24
    Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

    There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

    PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

    PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

    Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

    In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

    PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

    PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

    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.

    PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 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

    Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

    In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

    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.

    See all articles