Home > Backend Development > PHP Tutorial > PHP paging principle and PHP paging code comprehensive examples

PHP paging principle and PHP paging code comprehensive examples

WBOY
Release: 2016-07-25 08:52:45
Original
1912 people have browsed it
  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


  • source:php.cn
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template