PHP Class Pagination alias Pengaturan Data per Halaman
Screenshot sebagai berikut:
Berikut php class pagination, pada script disertakan juga keterangan komentar pada file script.
<?php class JinPagination { // fungsi pengaturan/option $this->blabla = "value nya blabla" function setOption($field, $value) { $this->$field = $value; } // fungsi paginasi generate array berupa total jumlah halaman, pagination, data dan posisi start // berguna untuk diatur secara fleksible terutama untuk berbasis menggunakan template function build() { // SETUP $tabel = $this->tabel; $where = $this->where; $limit = $this->limit; $order = $this->order; $page = $this->page; // SETUP OPTIONAL if(!isset($this->web_url_page)) { $web_url_page = "?page="; } else { $web_url_page = $this->web_url_page; } if(!isset($this->adjacents)) { $adjacents = "3"; } else { $adjacents = $this->adjacents; } if(!isset($this->txt_prev)) { $txt_prev = "« prev"; } else { $txt_prev = $this->txt_prev; } if(!isset($this->txt_next)) { $txt_next = "next »"; } else { $txt_next = $this->txt_next; } if(!isset($this->txt_titik)) { $txt_titik = "..."; } else { $txt_titik = $this->txt_titik; } $query = mysql_query("SELECT * FROM ".$tabel." ".$where.""); //$total_pages = mysql_fetch_array(mysql_query($query)); //$total_pages = $total_pages['num']; $total_pages = mysql_num_rows($query); if($page) { $start = ($page - 1) * $limit; //first item to display on this page } else { $start = 0; //if no page var is given, set start to 0 } // Get data. $query = "SELECT * FROM ".$tabel." ".$where." ".$order." LIMIT ".$start.", ".$limit.""; $hasil = mysql_query($query); /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = ""; if($lastpage > 1) { $pagination .= "<div class=\"pagination\">"; //previous button if ($page > 1) { $pagination .= "<a href=\"".$web_url_page.$prev."".$extra_href."\">" . $txt_prev . "</a>"; } else { $pagination .= "<span class=\"disabled\">" . $txt_prev . "</span>"; } //pages if ($lastpage < 7 + ($adjacents * 2)) { //not enough pages to bother breaking it up for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) { $pagination .= "<span class=\"current\">".$counter."</span>"; } else { $pagination .= "<a href=\"".$web_url_page.$counter."".$extra_href."\">".$counter."</a>"; } } } elseif($lastpage > 5 + ($adjacents * 2)) { //enough pages to hide some if($page < 1 + ($adjacents * 2)) { //close to beginning; only hide later pages for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) { $pagination .= "<span class=\"current\">".$counter."</span>"; } else { $pagination .= "<a href=\"".$web_url_page.$counter."".$extra_href."\">$counter</a>"; } } $pagination .= $txt_titik; $pagination .= "<a href=\"".$web_url_page.$lpm1."".$extra_href."\">".$lpm1."</a>"; $pagination .= "<a href=\"".$web_url_page.$lastpage."".$extra_href."\">".$lastpage."</a>"; } elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { //in middle; hide some front and some back $pagination .= "<a href=\"".$web_url_page."1".$extra_href."\">1</a>"; $pagination .= "<a href=\"".$web_url_page."2".$extra_href."\">2</a>"; $pagination .= $txt_titik; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) { $pagination .= "<span class=\"current\">".$counter."</span>"; } else { $pagination .= "<a href=\"".$web_url_page.$counter."".$extra_href."\">".$counter."</a>"; } } $pagination .= $txt_titik; $pagination .= "<a href=\"".$web_url_page.$lpm1."".$extra_href."\">".$lpm1."</a>"; $pagination .= "<a href=\"".$web_url_page.$lastpage."".$extra_href."\">".$lastpage."</a>"; } else { //close to end; only hide early pages $pagination .= "<a href=\"".$web_url_page."1".$extra_href."\">1</a>"; $pagination .= "<a href=\"".$web_url_page."2".$extra_href."\">2</a>"; $pagination .= $txt_titik; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) { $pagination .= "<span class=\"current\">".$counter."</span>"; } else { $pagination .= "<a href=\"".$web_url_page.$counter."".$extra_href."\">".$counter."</a>"; } } } } //next button if ($page < $counter - 1) { $pagination .= "<a href=\"".$web_url_page.$next."".$extra_href."\">" . $txt_next . "</a>"; } else { $pagination .= "<span class=\"disabled\">" . $txt_next . "</span>"; $pagination .= "</div>\n"; } } // hasil dari fungsi build() return array( "pagination" => $pagination, "total" => number_format($total_pages), "hasil" => $hasil, "start" => $start ); } } // Penggunaan Class // koneksi DB // asumsikan kita sudah terkoneksi dengan database MySQL // Setting CSS $isi = ""; $isi .= " <style> /* optional */ body { background-color:#fff; font-size:11px; font-family:Verdana; margin:0; padding:0; color:#555555; } /* END - optional */ /* PAGINATION */ div.pagination { padding: 3px 0 3px 0; margin: 0; } div.pagination a { padding: 2px 5px 2px 5px; margin: 2px; border: 1px solid #AAAADD; text-decoration: none; /* no underline */ color: #000099; } div.pagination a:hover, div.pagination a:active { border: 1px solid #000099; color: #000; } div.pagination span.current { padding: 2px 5px 2px 5px; margin: 2px; border: 1px solid #000099; font-weight: bold; background-color: #000099; color: #FFF; } div.pagination span.disabled { padding: 2px 5px 2px 5px; margin: 2px; border: 1px solid #EEE; color: #DDD; } /* END - PAGINATION */ </style> "; // optional CSS display echo $isi; // bikin object $hal = new JinPagination; // setup paginasi $hal->setOption("tabel", "jaw_airport"); // nama tabel database $hal->setOption("where", "WHERE `cek_aktif`='1'"); // where kondisi, kosongkan jika tidak memakai WHERE $hal->setOption("limit", "10"); // LIMIT tampilan per halaman $hal->setOption("order", "ORDER BY `code` DESC"); // urutan, kosongkan jika tidak memakai urutan $hal->setOption("page", $_REQUEST["page"]); // setup untuk ambil variable angka halaman (berguna jika menggunakan SEO url, ubah sesuai dgn kebutuhan) $hal->setOption("web_url_page", "?page="); // setup alamat url (berguna jika menggunakan SEO url, ubah sesuai dgn kebutuhan) // optional setup $hal->setOption("adjacents", "5"); // tampil berapa angka ke kanan dan ke kiri nya, jika kita diposisi tengah halaman $hal->setOption("txt_prev", "« sebelumnya"); // mengubah text "prev" menjadi "sebelumnya" $hal->setOption("txt_next", "berikutnya »"); // mengubah text "next" menjadi "berikutnya" // generate hasil pagination $hal_array = $hal->build(); // setup penomoran $no = $hal_array["start"] + 1; // tampilkan echo "<h1>Demo Data Pagination</h1>"; echo $hal_array["pagination"]; // tampilkan pagination diatas echo "<br /><br />"; // data while($data = mysql_fetch_array($hal_array["hasil"])){ echo $no . ". " . $data['code'] . " - ".$data['nama']." <br>"; $no++; } echo "<br />"; echo "total: " . $hal_array["total"]; // tampilkan total data echo $hal_array["pagination"]; // tampilkan pagination dibawah ?>
Demo lainnya: klik disini
Download file contoh script komplit nya:
class.pagination.rar (2.5 KiB, 2,601 hits)
semoga bermanfaat…
Tambahan:
untuk pagination Style, dapat Anda pilih disini: some-styles-for-your-pagination



(6 votes, average: 4.83 out of 5)