Advertisement
Guest User

pagination for product category

a guest
Sep 23rd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <footer>
  4.  
  5. <?php
  6. // connect to database
  7. $con = mysqli_connect('localhost', 'root', '');
  8. mysqli_select_db($con, 'bobashop');
  9.  
  10. // define how many results you want per page
  11. $results_per_page = 8;
  12.  
  13.  
  14. // find out the number of results stored in database
  15.  
  16. //DONT NEED THIS AS WE USING THE PRODUCTS ONE INSTEAD
  17. // $sql = 'SELECT * FROM tblProducts';
  18. // $result = mysqli_query($con, $sql);
  19.  
  20.  
  21. $number_of_results = count($products);
  22.  
  23. // determine number of total pages available
  24. $number_of_pages = ceil($number_of_results / $results_per_page);
  25.  
  26.  
  27. // determine which page number visitor is currently on
  28. if (!isset($_GET['page'])) {
  29. $page = 1;
  30. } else {
  31. $page = $_GET['page'];
  32. }
  33.  
  34.  
  35. // determine the sql LIMIT starting number for the results on the displaying page
  36. $this_page_first_result = ($page - 1) * $results_per_page;
  37.  
  38. // retrieve selected results from database and display them on page
  39. $sql = "SELECT * FROM tblProducts LIMIT "
  40. . $this_page_first_result . ',' . $results_per_page;
  41.  
  42. $result = mysqli_query($con, $sql);
  43.  
  44.  
  45. while ($row = mysqli_fetch_array($result)) {
  46. unset($rows);
  47. ?>
  48. <li>
  49. <a href="?action=view_product&amp;product_id=<?php echo $row['PKProductID']; ?>">
  50. <?php echo $row['ProductName']; ?>
  51. </a>
  52. </li>
  53. <?php
  54. }
  55.  
  56. // display the links to the pages
  57. for ($page = 1; $page <= $number_of_pages; $page++) {
  58. echo '<a href="index.php?page=' . $page . '">' . $page . '</a> ';
  59. }
  60. ?>
  61.  
  62.  
  63.  
  64. </footer>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement