Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. <?php
  2. // database connection
  3. $dbHost = 'localhost';
  4. $dbUsername = 'root';
  5. $dbPassword = 'secret';
  6. $dbName = 'database_name';
  7. $conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
  8. if ($conn->connect_error) {
  9. die("Connection failed: " . $conn->connect_error);
  10. }
  11.  
  12. // how many records should be displayed on a page?
  13. $records_per_page = 10;
  14.  
  15. // include the pagination class
  16. require 'path/to/Zebra_Pagination.php';
  17.  
  18. // instantiate the pagination object
  19. $pagination = new Zebra_Pagination();
  20.  
  21. // show records in reverse order
  22. $pagination->reverse(true);
  23.  
  24. // when showing records in reverse order, we need to know the total number
  25. // of records from the beginning
  26. $count = mysqli_query($conn, 'SELECT COUNT(id) AS records FROM countries') or die (mysqli_error($conn));
  27.  
  28. // pass the total number of records to the pagination class
  29. $total = mysqli_fetch_assoc($count);
  30. $pagination->records(array_pop($total));
  31.  
  32. // records per page
  33. $pagination->records_per_page($records_per_page);
  34.  
  35. // the MySQL statement to fetch the rows
  36. // note the LIMIT - use it exactly like that!
  37. // also note that we're ordering data descendingly - most important when we're
  38. // showing records in reverse order!
  39. $sql = '
  40. SELECT
  41. country
  42. FROM
  43. countries
  44. ORDER BY
  45. country DESC
  46. LIMIT
  47. ' . (($pagination->get_pages() - $pagination->get_page()) * $records_per_page) . ', ' . $records_per_page . '
  48. ';
  49.  
  50. // run the query
  51. $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
  52. ?>
  53.  
  54. <table>
  55. <tr><th>Country</th></tr>
  56. <?php $index = 0; while ($row = mysqli_fetch_assoc($result)): ?>
  57. <tr<?php echo $index++ % 2 ? ' class="even"' : ''; ?>>
  58. <td><?php echo $row['country']; ?></td>
  59. </tr>
  60. <?php endwhile; ?>
  61. </table>
  62.  
  63. <?php
  64. // render the pagination links
  65. $pagination->render();
  66. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement