Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. <?php include "config.php"; ?>
  2. <div>
  3. <table>
  4. <thead>
  5. <tr>
  6. <th>Number</th>
  7. <th>name</th>
  8. <th>address</th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?>
  13. <tr>
  14. <td><?php echo $page++; ?></td>
  15. <td><?php echo $results->data[$i]['name']; ?></td>
  16. <td><?php echo $results->data[$i]['address']; ?></td>
  17. </tr>
  18. <?php endfor; ?>
  19. </tbody>
  20. </table>
  21. </div>
  22. <?php echo $Paginator->createLinks($links); ?>
  23.  
  24. <?php
  25. $user = "root";
  26. $pass = "";
  27. try {
  28. $connect = new PDO('mysql:host=localhost;dbname=database', $user, $pass);
  29. } catch (PDOException $e) {
  30. print "Error!: " . $e->getMessage() . "<br/>";
  31. die();
  32. }
  33. $limit = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 5;
  34. $page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
  35. $links = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;
  36. $query = "SELECT * FROM table ORDER BY id DESC";
  37.  
  38. require_once 'Paginator.php';
  39. $Paginator = new Paginator($connect, $query);
  40. $results = $Paginator->getData($limit, $page);
  41. ?>
  42.  
  43. <?php
  44. class Paginator {
  45. private $konek;
  46. private $batas;
  47. private $halaman;
  48. private $habeit;
  49. private $semua;
  50.  
  51. public function __construct($conn, $query) {
  52.  
  53. $this->konek = $conn;
  54. $this->habeit = $query;
  55.  
  56. $rs= $this->konek->prepare( $this->habeit );
  57. $rs->execute();
  58. $this->semua = $rs->rowCount();
  59.  
  60. }
  61.  
  62. public function getData( $limit = 10, $page = 1 ) {
  63.  
  64. $this->batas = $limit;
  65. $this->halaman = $page;
  66.  
  67. if ( $this->batas == 'all' ) {
  68. $query = $this->habeit;
  69. } else {
  70. $query = $this->habeit . " LIMIT " . ( ( $this->halaman - 1 ) * $this->batas ) . ", $this->batas";
  71. }
  72. $rs = $this->konek->prepare( $query );
  73. $rs->execute();
  74. while ( $row = $rs->fetch(PDO::FETCH_ASSOC) ) {
  75. $results[] = $row;
  76. }
  77.  
  78. $result = new stdClass();
  79. $result->page = $this->halaman;
  80. $result->limit = $this->batas;
  81. $result->total = $this->semua;
  82. $result->data = $results;
  83.  
  84. return $result;
  85. }
  86.  
  87. public function createLinks( $links ) {
  88. if ( $this->batas == 'all' ) {
  89. return '';
  90. }
  91. $last = ceil( $this->semua / $this->batas );
  92.  
  93. $start = ( ( $this->halaman - $links ) > 0 ) ? $this->halaman - $links : 1;
  94. $end = ( ( $this->halaman + $links ) < $last ) ? $this->halaman + $links : $last;
  95.  
  96. $html = '<ul class="pagination">';
  97.  
  98. $class = ( $this->halaman == 1 ) ? "disabled" : "";
  99. $html .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=1">&laquo;</a></li>';
  100. $html .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=' . ( $this->halaman - 1 ) . '">&lsaquo;</a></li>';
  101.  
  102. if ( $start > 1 ) {
  103. $html .= '<li><a href="?limit=' . $this->batas . '&page=1">1</a></li>';
  104. $html .= '<li><span class="titik">...</span></li>';
  105. }
  106.  
  107. for ( $i = $start ; $i <= $end; $i++ ) {
  108. $class = ( $this->halaman == $i ) ? "active" : "";
  109. $html .= '<li class="' . $class . '"><a href="?limit=' . $this->batas . '&page=' . $i . '">' . $i . '</a></li>';
  110. }
  111.  
  112. if ( $end < $last ) {
  113. $html .= '<li class="disabled"><span>...</span></li>';
  114. $html .= '<li><a href="?limit=' . $this->batas . '&page=' . $last . '">' . $last . '</a></li>';
  115. }
  116.  
  117. $class = ( $this->halaman == $last ) ? "disabled" : "";
  118. $html .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=' . ( $this->halaman + 1 ) . '">&rsaquo;</a></li>';
  119. $html .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=' . ( $last ) . '">&raquo;</a></li>';
  120.  
  121. $html .= '</ul>';
  122.  
  123. return $html;
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement