Php paging principle and php paging code integration example

Source: Internet
Author: User
Php paging principle and php paging code integration example

  1. /**
  2. * PHP paging code
  3. **/
  4. /**
  5. * Link to the database
  6. * Bbs.it-home.org
  7. * @ Param string $ strHost address of the database server host
  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 database encoding
  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 and calculate the corresponding parameter value
  38. * Including: 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 ['link']
  43. * SQL statement $ arrParameter ['SQL']
  44. * Page entry type $ arrParameter ['tag']
  45. *
  46. Int $ intPage current page number
  47. Int $ intNums number of records per page
  48. Number of links displayed on the int $ intLinks page
  49. String $ strTablename display data tables by page
  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. // Adjust to an odd number when the number of displayed links is not an odd number
  58. If ($ intLinks % 2 = 0 ){
  59. $ IntLinks --;
  60. }
  61. // Adjust to 10 if 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 limit keyword in the SQL statement
  72. $ Effecffset = ($ intPage-1) * $ intNums;
  73. // Only displays the next page of the previous page. that is, the number of links displayed is not greater than 0.
  74. If ($ intLinks <= 0 ){
  75. $ StrSql2 = "select * from '{$ strTablename}' limit {$ effecffset}, {$ intNums }";
  76. $ ArrParameter ['start'] = null;
  77. $ ArrParameter ['end'] = null;
  78. $ ArrParameter ['Page'] = $ intPage;
  79. $ ArrParameter ['nums'] = $ intNums;
  80. $ ArrParameter ['link'] = null;
  81. $ ArrParameter ['all'] = $ intAllPage;
  82. $ ArrParameter ['SQL'] = $ strSql2;
  83. $ ArrParameter ['tag'] = 1;
  84. // When a paging bar code is displayed, the number of links displayed is greater than 0.
  85. } Else {
  86. // When the total number of records 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. // Calculate 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-($ intAllPage-$ intPage ));
  103. // $ IntStartPage = $ intPage-$ intHalfLinks + $ intAllPage-$ intPage;
  104. $ IntStartPage = $ intAllPage-2 * $ intHalfLinks;
  105. }
  106. // Calculate 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 the SQL statement to be executed
  115. $ StrSql2 = "select * from '{$ strTablename}' limit {$ effecffset}, {$ intNums }";
  116. $ ArrParameter ['start'] = $ intStartPage;
  117. $ ArrParameter ['end'] = $ intEndPage;
  118. $ ArrParameter ['Page'] = $ intPage;
  119. $ ArrParameter ['nums'] = $ intNums;
  120. $ ArrParameter ['link'] = $ intLinks;
  121. $ ArrParameter ['all'] = $ intAllPage;
  122. $ ArrParameter ['SQL'] = $ strSql2;
  123. $ ArrParameter ['tag'] = 2;
  124. // When the total number of records is equal to 0
  125. } Else {
  126. $ ArrParameter ['start'] = null;
  127. $ ArrParameter ['end'] = null;
  128. $ ArrParameter ['Page'] = null;
  129. $ ArrParameter ['nums'] = null;
  130. $ ArrParameter ['link'] = null;
  131. $ ArrParameter ['all'] = null;
  132. $ ArrParameter ['SQL'] = null;
  133. $ ArrParameter ['tag'] = 3;
  134. }
  135. }
  136. Return $ arrParameter;
  137. }
  138. /**
  139. * Create a page entry
  140. *
  141. * @ Param int $ the current page number displayed on intPage
  142. * @ Param int $ intStartPage start page number
  143. * @ Param int $ intEndPage ending page number
  144. * @ Param int $ total number of intAllRecords records
  145. * @ Param int $ intTag the type of the paging bar
  146. * @ Return string
  147. **/
  148. Function createPagingItem ($ intPage, $ intStartPage, $ intEndPage, $ intAllPage, $ intTag ){
  149. $ StrPageItem = '';
  150. // Only displays the next page of the previous page. that is, the number of links displayed is not greater than 0.
  151. If ($ intTag = 1 ){
  152. If ($ intAllPage <= 0 ){
  153. $ StrPageItem. = 'homepage end page ';
  154. } Else {
  155. If ($ intPage = 1 ){
  156. $ StrPageItem. = "homepage previous ";
  157. $ StrPageItem. = "";
  158. } Else {
  159. $ StrPageItem. = "homepage ";
  160. $ StrPageItem. = "";
  161. $ StrPageItem. = "previous page ";
  162. $ StrPageItem. = "";
  163. }
  164. If ($ intPage = $ intAllPage ){
  165. $ StrPageItem. = "Last page of the next page ";
  166. } Else {
  167. $ StrPageItem. = "next page ";
  168. $ StrPageItem. = "";
  169. $ StrPageItem. = "Last page ";
  170. }
  171. }
  172. }
  173. // When a paging bar code is displayed, the number of links displayed is greater than 0.
  174. If ($ intTag = 2 ){
  175. If ($ intPage = 1 ){
  176. $ StrPageItem. = "homepage previous ";
  177. $ StrPageItem. = "";
  178. } Else {
  179. $ StrPageItem. = "homepage ";
  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. = "Last page of the next page ";
  194. } Else {
  195. $ StrPageItem. = "next page ";
  196. $ StrPageItem. = "";
  197. $ StrPageItem. = "Last page ";
  198. }
  199. }
  200. // When the total number of records is equal to 0
  201. If ($ intTag = 3 ){
  202. $ StrPageItem. = 'homepage end page ';
  203. }
  204. Return $ strPageItem;
  205. }
  206. /**
  207. * Obtain and output data
  208. *
  209. * @ Param string $ SQL statement queried by strSql
  210. * @ Param array $ arrFields an array composed of fields 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 to and select a database
  240. // Note: you should modify the database account, password, and database name.
  241. $ ResLink = mysqlConnect ('localhost', 'root', 'root', 'ztlibrary ');
  242. // Obtain the paging parameters. 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 Table field name is its key value, and the field name is interpreted as its element value in Chinese.
  246. // Note: you should modify the following array based on your data table.
  247. $ ArrFields = array ('id _ code' => 'Book encoding', 'Book _ name' => 'Book name ',
  248. & Apos; Book _ ISBN & apos; = & apos; ISBN & apos;, & apos; composite _ man & apos; = & apos; source & apos;, & apos; Issue _ time & apos ', 'storing _ time' => 'Warehouse receiving time ');
  249. ?>
  250. Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
  251. Php paging demonstration --www.yuju100.com
  252. // Output paging data
  253. Echo outPutData ($ arrParameter ['SQL'], $ arrFields, $ resLink );
  254. ?>
  255. // Display pagination bar
  256. Echo createPagingItem ($ arrParameter ['Page'], $ arrParameter ['start'], $ arrParameter ['end'],
  257. $ ArrParameter ['all'], $ arrParameter ['tag']);
  258. ?>

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.