A comprehensive example of PHP paging principle and PHP page code

Source: Internet
Author: User
  1. /**
  2. * PHP Paging code
  3. * */
  4. /**
  5. * Link 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 the page, calculate the corresponding parameter value
  38. * Includes: Start Page $arrparameter[' start ']
  39. * End page number $arrparameter[' End ']
  40. * Total number of records $arrparameter[' all '
  41. * Number of records displayed per page $arrparameter[' nums ']
  42. * Number of links displayed on the page $arrparameter[' links '
  43. * SQL statement to query $arrparameter[' SQL ']
  44. * Paging bar type $arrparameter[' tag ']
  45. *
  46. int $intPage Current page number value
  47. int $intNums The number of records displayed per page
  48. int $intLinks The number of links displayed in the page
  49. String $strTablename pagination display data table
  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. The number of links displayed is not odd when adjusted to odd
  58. if ($intLinks% 2 = = 0) {
  59. $intLinks--;
  60. }
  61. The number of records displayed per page is not greater than 0 o'clock adjusted to 10
  62. if ($intNums <= 0) {
  63. $intNums = 10;
  64. }
  65. Calculate the total number of pages
  66. $STRSQL 1= "SELECT COUNT (*) as num from ' {$strTablename} '";
  67. $RESOBJ 1=mysql_query ($strSql 1, $resLink);
  68. $ARROBJ 1=mysql_fetch_assoc ($resObj 1);
  69. $intAllRecords = $arrObj 1[' num '];
  70. $intAllPage =ceil ($intAllRecords/$intNums);
  71. The first parameter of the SQL statement limit keyword
  72. $intOffset = ($intPage-1) * $intNums;
  73. Shows the number of links not greater than 0 if only the next page of the previous page is displayed
  74. if ($intLinks <= 0) {
  75. $STRSQL 2= "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 ']= $STRSQL 2;
  83. $arrParameter [' tag ']=1;
  84. Displays the number of links that are greater than 0 when the paging bar code is displayed
  85. }else{
  86. When the total record is 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. Calculates 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. Calculates the value of the end page number
  107. $intEndPage = $intPage + $intHalfLinks;
  108. if ($intEndPage < $intLinks && $intAllPage >= $intLinks) {
  109. $intEndPage = $intLinks;
  110. }
  111. if ($intEndPage > $intAllPage) {
  112. $intEndPage = $intAllPage;
  113. }
  114. Create an SQL statement to execute
  115. $STRSQL 2= "SELECT * from ' {$strTablename} ' limit {$intOffset},{$intNums}";
  116. $arrParameter [' Start ']= $intStartPage;
  117. $arrParameter [' End ']= $intEndPage;
  118. $arrParameter [' page ']= $intPage;
  119. $arrParameter [' Nums ']= $intNums;
  120. $arrParameter [' Links ']= $intLinks;
  121. $arrParameter [' All ']= $intAllPage;
  122. $arrParameter [' SQL ']= $STRSQL 2;
  123. $arrParameter [' tag ']=2;
  124. When the total record equals 0 of the case
  125. }else{
  126. $arrParameter [' Start ']=null;
  127. $arrParameter [' End ']=null;
  128. $arrParameter [' Page ']=null;
  129. $arrParameter [' Nums ']=null;
  130. $arrParameter [' Links ']=null;
  131. $arrParameter [' All ']=null;
  132. $arrParameter [' SQL ']=null;
  133. $arrParameter [' tag ']=3;
  134. }
  135. }
  136. return $arrParameter;
  137. }
  138. /**
  139. * Create a paging bar
  140. *
  141. * @param int $intPage The currently displayed page number value
  142. * @param int $intStartPage Start Page
  143. * @param int $intEndPage End page number
  144. * Total number of records @param int $intAllRecords
  145. * @param int $intTag page Bar type tag
  146. * @return String
  147. * */
  148. function Createpagingitem ($intPage, $intStartPage, $intEndPage, $intAllPage, $intTag) {
  149. $strPageItem = ";
  150. Shows the number of links not greater than 0 if only the next page of the previous page is displayed
  151. if ($intTag = = 1) {
  152. if ($intAllPage <= 0) {
  153. $strPageItem. = ' home Page last ';
  154. }else{
  155. if ($intPage = = 1) {
  156. $strPageItem. = "First Prev";
  157. $strPageItem. = "";
  158. }else{
  159. $strPageItem. = "Home";
  160. $strPageItem. = "";
  161. $strPageItem. = "Previous page";
  162. $strPageItem. = "";
  163. }
  164. if ($intPage = = $intAllPage) {
  165. $strPageItem. = "Next Page last";
  166. }else{
  167. $strPageItem. = "Next page";
  168. $strPageItem. = "";
  169. $strPageItem. = "Last";
  170. }
  171. }
  172. }
  173. Displays the number of links that are greater than 0 when the paging bar code is displayed
  174. if ($intTag = = 2) {
  175. if ($intPage = = 1) {
  176. $strPageItem. = "First Prev";
  177. $strPageItem. = "";
  178. }else{
  179. $strPageItem. = "Home";
  180. $strPageItem. = "";
  181. $strPageItem. = "Previous page";
  182. $strPageItem. = "";
  183. }
  184. for ($i = $intStartPage; $i <= $intEndPage; $i + +) {
  185. if ($i = = $intPage) {
  186. $strPageItem. = $i;
  187. }else{
  188. $strPageItem. = "[". $i. "]";
  189. }
  190. $strPageItem. = "";
  191. }
  192. if ($intPage = = $intAllPage) {
  193. $strPageItem. = "Next Page last";
  194. }else{
  195. $strPageItem. = "Next page";
  196. $strPageItem. = "";
  197. $strPageItem. = "Last";
  198. }
  199. }
  200. When the total record equals 0 of the case
  201. if ($intTag = = 3) {
  202. $strPageItem. = ' home Page last ';
  203. }
  204. return $strPageItem;
  205. }
  206. /**
  207. * Get and output data
  208. *
  209. * SQL statement @param string $STRSQL query
  210. * @param array $arrFields The fields that need to be displayed
  211. * @param resource $resLink data connection handle
  212. * @return String
  213. * */
  214. function Outputdata ($STRSQL, $arrFields, $resLink) {
  215. $RESOBJ =mysql_query ($STRSQL, $resLink);
  216. $ARROBJ =array ();
  217. $strOutPutData = ";
  218. $arrFieldsCode =array_keys ($arrFields);
  219. while (@ $arrRow =mysql_fetch_assoc ($RESOBJ)) {
  220. $ARROBJ []= $arrRow;
  221. }
  222. $strOutPutData. = "
  223. $strOutPutData. = '
  224. foreach ($arrFieldsCode as $strVal) {
  225. }
  226. foreach ($arrObj as $arrVal) {
  227. foreach ($arrFieldsCode as $strVal) {
  228. }
  229. }
  230. $strOutPutData. = "
  231. $strOutPutData. = "
  232. $strOutPutData. = "
  233. $strOutPutData. = "
  234. $strOutPutData. = "
  235. $strOutPutData. = "
  236. "; '; "; "; "; "; ";
    ". $arrFields [Trim ($strVal)]."
    ". $arrVal [Trim ($strVal)]."
    ";
  237. return $strOutPutData;
  238. }
  239. Connect and select a database
  240. Note: You should modify the database account and password as well as the database name
  241. $resLink =mysqlconnect (' localhost ', ' root ', ' root ', ' ztlibrary ');
  242. To find the paging parameter note: You should modify the data table name
  243. $arrParameter =calculateparamester (@$_get[' page ']?$_get[' page ']:1,
  244. 10,5, ' Book_info ', $resLink);
  245. The data to be displayed, the field name of the table is its key value, the Chinese of the field name is interpreted as its element value
  246. Note: You should modify the following array according to your data table
  247. $arrFields =array (' id_code ' = ' book Code ', ' book_name ' = ' book name ',
  248. ' BOOK_ISBN ' + ' ISBN ', ' contribute_man ' + ' source ', ' issue_time ' and ' publication Time ', ' storing_time ' + ' ' storage time ';
  249. ?>
  250. "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
  251. PHP Pagination Demo--www.yuju100.com
  252. Output Paging data
  253. Echo outputdata ($arrParameter [' SQL '], $arrFields, $resLink);
  254. ?>
  255. Show Pagination Bar
  256. Echo Createpagingitem ($arrParameter [' page '], $arrParameter [' Start '], $arrParameter [' End '],
  257. $arrParameter [' All '], $arrParameter [' tag '];
  258. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.