Advertisement
Guest User

Pagination

a guest
Aug 31st, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>ThaiCreate.Com PHP & MySQL Tutorial</title>
  4. </head>
  5. <body>
  6. <?php
  7. $host = "localhost";
  8. $user = "root";
  9. $pass = "";
  10. $dbname = "page";
  11. $pdo = new PDO("mysql:dbname=$dbname;host=$host", $user, $pass);
  12. $strSQL = "SELECT * FROM customer ";
  13. $objQuery = $pdo->prepare($strSQL);
  14. $objQuery->execute();
  15. $Num_Rows = $objQuery->rowCount();
  16.  
  17. $Per_Page = 2; // Per Page
  18.  
  19. $Page = $_GET["Page"];
  20. if(!$_GET["Page"])
  21. {
  22. $Page=1;
  23. }
  24.  
  25. $Prev_Page = $Page-1;
  26. $Next_Page = $Page+1;
  27.  
  28. $Page_Start = (($Per_Page*$Page)-$Per_Page);
  29. if($Num_Rows<=$Per_Page)
  30. {
  31. $Num_Pages =1;
  32. }
  33. else if(($Num_Rows % $Per_Page)==0)
  34. {
  35. $Num_Pages =($Num_Rows/$Per_Page) ;
  36. }
  37. else
  38. {
  39. $Num_Pages =($Num_Rows/$Per_Page)+1;
  40. $Num_Pages = (int)$Num_Pages;
  41. }
  42.  
  43. $strSQL .=" order by CustomerID ASC LIMIT $Page_Start , $Per_Page";
  44. $objQuery = $pdo->prepare($strSQL);
  45. $objQuery->execute();
  46. ?>
  47. <table width="600" border="1">
  48. <tr>
  49. <th width="91"> <div align="center">CustomerID </div></th>
  50. <th width="98"> <div align="center">Name </div></th>
  51. <th width="198"> <div align="center">Email </div></th>
  52. <th width="97"> <div align="center">CountryCode </div></th>
  53. <th width="59"> <div align="center">Budget </div></th>
  54. <th width="71"> <div align="center">Used </div></th>
  55. </tr>
  56. <?php
  57. while($objResult = $objQuery->fetch(PDO::FETCH_ASSOC))
  58. {
  59. ?>
  60. <tr>
  61. <td><div align="center"><?php echo $objResult["CustomerID"];?></div></td>
  62. <td><?php echo $objResult["Name"];?></td>
  63. <td><?php echo $objResult["Email"];?></td>
  64. <td><div align="center"><?php echo $objResult["CountryCode"];?></div></td>
  65. <td align="right"><?php echo $objResult["Budget"];?></td>
  66. <td align="right"><?php echo $objResult["Used"];?></td>
  67. </tr>
  68. <?php
  69. }
  70. ?>
  71. </table>
  72.  
  73. <br>
  74. Total <?php echo $Num_Rows;?> Record : <?php echo $Num_Pages;?> Page :
  75. <?php
  76. if($Prev_Page)
  77. {
  78. echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
  79. }
  80.  
  81. for($i=1; $i<=$Num_Pages; $i++){
  82. if($i != $Page)
  83. {
  84. echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
  85. }
  86. else
  87. {
  88. echo "<b> $i </b>";
  89. }
  90. }
  91. if($Page!=$Num_Pages)
  92. {
  93. echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
  94. }
  95. ?>
  96. </body>
  97. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement