nstruth2

Need Help With Pagination

Jul 24th, 2023
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en-EN">
  3. <?php
  4. require __DIR__.DIRECTORY_SEPARATOR.'mysqli.php';
  5.  
  6. $r_p_page = 5;
  7. $st_page = $_GET['values'] ?? 5;
  8. $st_page = filter_var($st_page, FILTER_VALIDATE_INT);
  9.  
  10. $sql = 'SELECT COUNT(*) as total FROM `name_stu`';
  11. $countall = $connect->query($sql);
  12. $rowcount = $countall->fetch_object();
  13. $rowcount = filter_var($rowcount->total, FILTER_VALIDATE_INT);
  14.  
  15. $__page = ceil($rowcount / 5);
  16. $start_loop = $st_page / 5;
  17. $last_val = ($__page - 1) * 5;
  18. $start_SH = $start_loop + 4;
  19. $last_page = $last_val / 5;
  20. $page_start_position = (($start_loop - 1) * 5) + 1;
  21. $page_end_position = ($start_loop * 5);
  22. ?>
  23.  <head>
  24.   <title>Page <?php echo $start_loop; ?></title>
  25.   <meta name="viewport" content="width=device-width, initial-scale=1">
  26.   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
  27.   <style>
  28.   a {
  29.    font-weight:bold;
  30.   }
  31.   </style>
  32.  </head>
  33.  <body>
  34.   <div class="container">
  35.    <h3 align="center">Page <?php echo $start_loop; ?></h3>
  36.    <div class="table-responsive">
  37.     <table class="table table-responsive table-bordered table-striped caption-top">
  38.     <caption class="text-center"><?php printf('Displaying %s to %s students', $page_start_position, $page_end_position); ?></caption>
  39.     <thead>
  40.     <tr>
  41.       <th scope="col">#</th>
  42.       <th scope="col">Name</th>
  43.       <th scope="col">Phone</th>
  44.     </tr>
  45.   </thead>
  46.   <tbody>
  47.      <?php
  48. $sql = "SELECT * FROM `name_stu` LIMIT $st_page, $r_p_page";
  49. $result = $connect->query($sql);
  50.  
  51. while ($row = $result->fetch_assoc()) {
  52.     ?>
  53. <tr>
  54.       <th scope="row"><?php echo $row['student_id']; ?> </th>
  55.       <td><?php echo $row['student_name']; ?></td>
  56.       <td><?php echo $row['student_phone']; ?></td>
  57.     </tr>
  58.      <?php
  59. }
  60. ?>
  61. </tbody>
  62. <tfoot>
  63. <tr>
  64.   <td class="offset-3" colspan="3"><p class="m-0 text-center"><?php printf('Database contains %d students', $rowcount); ?></p></td>
  65. </tr>
  66. </tfoot>
  67.     </table>
  68.     <div align="center">
  69. <?php
  70. if ($__page - 1 <= $start_SH) {
  71.     $end_loop = $__page;
  72. } else {
  73.     $end_loop = $start_SH;
  74. }
  75. ?>
  76.  
  77.  
  78. <nav aria-label="Page navigation">
  79.   <ul class="pagination justify-content-center">
  80. <?php
  81. if ($st_page > 5) {
  82.     $vars = ['page' => 1, 'values' => 5];
  83.     $querystring = http_build_query($vars);
  84.     printf('<li class="page-item"><a class="page-link" href="pagination.php?%s">First</a></li>', $querystring);
  85.     printf('<li class="page-item"><a class="page-link" href="pagination.php?values=%d">&laquo;</a></li>', $st_page - 5);
  86. }
  87.  
  88. for ($i = $start_loop; $i < $end_loop; ++$i) {
  89.     $hide_values = $i * 5;
  90.     $currentStyle = ($st_page !== $hide_values) ? 'inactive' : 'active';
  91.     $vars = ['page' => ceil($i), 'values' => $hide_values];
  92.     $querystring = http_build_query($vars);
  93.     printf('<li class="page-item %s"><a class="page-link" href="pagination.php?%s">%d</a></li>', $currentStyle, $querystring, ceil($i));
  94. }
  95.  
  96. if ($__page - 1 != $start_loop) {
  97.     $vars = ['page' => $last_page, 'values' => $last_val];
  98.     $querystring = http_build_query($vars);
  99.     printf('<li class="page-item"><a class="page-link" href="pagination.php?values=%d">&raquo;</a></li>', $st_page + 5);
  100.     printf('<li class="page-item"><a class="page-link" href="pagination.php?%s">Last</a></li>', $querystring);
  101. }
  102. ?>
  103.   </ul>
  104. </nav>
  105.  
  106.     </div>
  107.    </div>
  108.   </div>
  109.  </body>
  110.  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script>
  111. </html>
  112.  
  113.  
  114.  
Advertisement
Add Comment
Please, Sign In to add comment