Guest User

Untitled

a guest
Jan 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. <?php
  2. // Script and tutorial written by Adam Khoury @ developphp.com
  3. // Line by line explanation : youtube.com/watch?v=T2QFNu_mivw
  4. include_once("mysqli_connection.php");
  5. // This first query is just to get the total count of rows
  6. $sql = "SELECT COUNT(id) FROM testimonials WHERE approved='1'";
  7. $query = mysqli_query($db_conx, $sql);
  8. $row = mysqli_fetch_row($query);
  9. // Here we have the total row count
  10. $rows = $row[0];
  11. // This is the number of results we want displayed per page
  12. $page_rows = 10;
  13. // This tells us the page number of our last page
  14. $last = ceil($rows/$page_rows);
  15. // This makes sure $last cannot be less than 1
  16. if($last < 1){
  17. $last = 1;
  18. }
  19. // Establish the $pagenum variable
  20. $pagenum = 1;
  21. // Get pagenum from URL vars if it is present, else it is = 1
  22. if(isset($_GET['pn'])){
  23. $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
  24. }
  25. // This makes sure the page number isn't below 1, or more than our $last page
  26. if ($pagenum < 1) {
  27. $pagenum = 1;
  28. } else if ($pagenum > $last) {
  29. $pagenum = $last;
  30. }
  31. // This sets the range of rows to query for the chosen $pagenum
  32. $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
  33. // This is your query again, it is for grabbing just one page worth of rows by applying $limit
  34. $sql = "SELECT id, firstname, lastname, datemade FROM testimonials WHERE approved='1' ORDER BY id DESC $limit";
  35. $query = mysqli_query($db_conx, $sql);
  36. // This shows the user what page they are on, and the total number of pages
  37. $textline1 = "Testimonials (<b>$rows</b>)";
  38. $textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
  39. // Establish the $paginationCtrls variable
  40. $paginationCtrls = '';
  41. // If there is more than 1 page worth of results
  42. if($last != 1){
  43. /* First we check if we are on page one. If we are then we don't need a link to
  44. the previous page or the first page so we do nothing. If we aren't then we
  45. generate links to the first page, and to the previous page. */
  46. if ($pagenum > 1) {
  47. $previous = $pagenum - 1;
  48. $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
  49. // Render clickable number links that should appear on the left of the target page number
  50. for($i = $pagenum-4; $i < $pagenum; $i++){
  51. if($i > 0){
  52. $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
  53. }
  54. }
  55. }
  56. // Render the target page number, but without it being a link
  57. $paginationCtrls .= ''.$pagenum.' &nbsp; ';
  58. // Render clickable number links that should appear on the right of the target page number
  59. for($i = $pagenum+1; $i <= $last; $i++){
  60. $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
  61. if($i >= $pagenum+4){
  62. break;
  63. }
  64. }
  65. // This does the same as above, only checking if we are on the last page, and then generating the "Next"
  66. if ($pagenum != $last) {
  67. $next = $pagenum + 1;
  68. $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
  69. }
  70. }
  71. $list = '';
  72. while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
  73. $id = $row["id"];
  74. $firstname = $row["firstname"];
  75. $lastname = $row["lastname"];
  76. $datemade = $row["datemade"];
  77. $datemade = strftime("%b %d, %Y", strtotime($datemade));
  78. $list .= '<p><a href="testimonial.php?id='.$id.'">'.$firstname.' '.$lastname.' Testimonial</a> - Click the link to view this testimonial<br>Written '.$datemade.'</p>';
  79. }
  80. // Close your database connection
  81. mysqli_close($db_conx);
  82. ?>
  83. <!DOCTYPE html>
  84. <html>
  85. <head>
  86. <style type="text/css">
  87. body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
  88. div#pagination_controls{font-size:21px;}
  89. div#pagination_controls > a{ color:#06F; }
  90. div#pagination_controls > a:visited{ color:#06F; }
  91. </style>
  92. </head>
  93. <body>
  94. <div>
  95. <h2><?php echo $textline1; ?> Paged</h2>
  96. <p><?php echo $textline2; ?></p>
  97. <p><?php echo $list; ?></p>
  98. <div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
  99. </div>
  100. </body>
  101. </html>
Add Comment
Please, Sign In to add comment