Paginasi merupakan proses mengambil keselurahan hasil dan menyebarkan ke beberapa halaman dengan 1 halaman yang record baris di sesuaikan dengan keinginan, supaya membuat lebih mudah untuk dilihat atau dibaca datanya.
Berikut adalah cara membuat Paginasi :
1. Buat file Paginator.class.php
Kelas paginator hanya akan memiliki dua metode dan constructor.
Code kelas Paginator
<?php
class Paginator {private $_conn;private $_limit;private $_page;private $_query;private $_total;
}
Code Kelas construct :
<?php
public function __construct( $conn, $query ) {$this->_conn = $conn;$this->_query = $query;$rs= $this->_conn->query( $this->_query );$this->_total = $rs->num_rows;}
Code Kelas mengambil data ( pada pengambilan data per hal 10 baris isi ) :
<?php
public function getData( $limit = 10, $page = 1 ) {$this->_limit = $limit;$this->_page = $page;if ( $this->_limit == 'all' ) {$query = $this->_query;} else {$query = $this->_query . " LIMIT " . ( ( $this->_page - 1 ) * $this->_limit ) . ", $this->_limit";}$rs = $this->_conn->query( $query );while ( $row = $rs->fetch_assoc() ) {$results[] = $row;}$result = new stdClass();$result->page = $this->_page;$result->limit = $this->_limit;$result->total = $this->_total;$result->data = $results;return $result;
}
Code kelas membuat link paginasi :
<?php
public function createLinks( $links, $list_class ) {if ( $this->_limit == 'all' ) {return '';}$last = ceil( $this->_total / $this->_limit );$start = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1;$end = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last;$html = '<ul class="' . $list_class . '">';$class = ( $this->_page == 1 ) ? "disabled" : "";$html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">«</a></li>';if ( $start > 1 ) {$html .= '<li><a href="?limit=' . $this->_limit . '&page=1">1</a></li>';$html .= '<li class="disabled"><span>...</span></li>';}for ( $i = $start ; $i <= $end; $i++ ) {$class = ( $this->_page == $i ) ? "active" : "";$html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a></li>';}if ( $end < $last ) {$html .= '<li class="disabled"><span>...</span></li>';$html .= '<li><a href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a></li>';}$class = ( $this->_page == $last ) ? "disabled" : "";$html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '">»</a></li>';$html .= '</ul>';return $html;}
2. Buat file index.php ( untuk menggunakan class Paginator dan menampilkan data ) juga menggunakan bootstrap dasar styling halaman:
<!DOCTYPE html>
<head>
<title>PHP Pagination</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<?phprequire_once 'Paginator.class.php';$conn = new mysqli( '127.0.0.1', 'root', 'root', 'world' );$limit = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25;$page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;$links = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;$query = "SELECT City.Name, City.CountryCode, Country.Code, Country.Name AS Country, Country.Continent, Country.Region FROM City, Country WHERE City.CountryCode = Country.Code";$Paginator = new Paginator( $conn, $query );$results = $Paginator->getData( $page, $limit );?>
<body>
<div class="container">
<div class="col-md-10 col-md-offset-1">
<h1>PHP Pagination</h1>
<table class="table table-striped table-condensed table-bordered table-rounded">
<thead>
<?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?>
<tr><td><?php echo $results->data[$i]['Name']; ?></td><td><?php echo $results->data[$i]['Country']; ?></td><td><?php echo $results->data[$i]['Continent']; ?></td><td><?php echo $results->data[$i]['Region']; ?></td></tr><?php endfor; ?>
</tr>
</thead>
<tbody></tbody>
</table>
<?php echo $Paginator->createLinks( $links, 'pagination pagination-sm' ); ?>
</div>
</div>
</body>
</html>
Hasil :