Nah untuk cara Kedua ini lebih sederhana hanya saja kamu membutuhkan file class untuk pengaturan link halaman, dll.
Pertama buat dulu File Class nya:
Simpan dengan nama: page_class.php
<?php
class PaginateIt {
var $currentPage, $itemCount, $itemsPerPage, $linksHref, $linksToDisplay;
var $pageJumpBack, $pageJumpNext, $pageSeparator;
var $queryString, $queryStringVar;
function SetCurrentPage($reqCurrentPage){
$this->currentPage = (integer) abs($reqCurrentPage);
}
function SetItemCount($reqItemCount){
$this->itemCount = (integer) abs($reqItemCount);
}
function SetItemsPerPage($reqItemsPerPage){
$this->itemsPerPage = (integer) abs($reqItemsPerPage);
}
function SetLinksHref($reqLinksHref){
$this->linksHref = $reqLinksHref;
}
function SetLinksFormat($reqPageJumpBack, $reqPageSeparator, $reqPageJumpNext){
$this->pageJumpBack = $reqPageJumpBack;
$this->pageSeparator = $reqPageSeparator;
$this->pageJumpNext = $reqPageJumpNext;
}
function SetLinksToDisplay($reqLinksToDisplay){
$this->linksToDisplay = (integer) abs($reqLinksToDisplay);
}
function SetQueryStringVar($reqQueryStringVar){
$this->queryStringVar = $reqQueryStringVar;
}
function SetQueryString($reqQueryString){
$this->queryString = $reqQueryString;
}
function GetCurrentCollection($reqCollection){
if($this->currentPage < 1){
$start = 0;
}
elseif($this->currentPage > $this->GetPageCount()){
$start = $this->GetPageCount() * $this->itemsPerPage - $this->itemsPerPage;
}
else {
$start = $this->currentPage * $this->itemsPerPage - $this->itemsPerPage;
}
return array_slice($reqCollection, $start, $this->itemsPerPage);
}
function GetPageCount(){
return (integer)ceil($this->itemCount/$this->itemsPerPage);
}
function GetPageLinks(){
$strLinks = '';
$pageCount = $this->GetPageCount();
$queryString = $this->GetQueryString();
$linksPad = floor($this->linksToDisplay/2);
if($this->linksToDisplay == -1){
$this->linksToDisplay = $pageCount;
}
if($pageCount == 0){
$strLinks = '1';
}
elseif($this->currentPage - 1 <= $linksPad || ($pageCount - $this->linksToDisplay + 1 == 0) || $this->linksToDisplay > $pageCount){
$start = 1;
}
elseif($pageCount - $this->currentPage <= $linksPad){
$start = $pageCount - $this->linksToDisplay + 1;
}
else {
$start = $this->currentPage - $linksPad;
}
if(isset($start)){
if($start > 1){
if(!empty($this->pageJumpBack)){
$pageNum = $start - $this->linksToDisplay + $linksPad;
if($pageNum < 1){
$pageNum = 1;
}
$strLinks .= '<a class="bodLink" href="'.$this->linksHref.$queryString.$pageNum.'">';
$strLinks .= $this->pageJumpBack.'</a>'.$this->pageSeparator;
}
$strLinks .= '<a class="bodLink" href="'.$this->linksHref.$queryString.'1">1...</a>'.$this->pageSeparator;
}
if($start + $this->linksToDisplay > $pageCount){
$end = $pageCount;
}
else {
$end = $start + $this->linksToDisplay - 1;
}
for($i = $start; $i <= $end; $i ++){
if($i != $this->currentPage){
$strLinks .= '<a class="bodLink" href="'.$this->linksHref.$queryString.($i).'">';
$strLinks .= ($i).'</a>'.$this->pageSeparator;
}
else {
$strLinks .= $i.$this->pageSeparator;
}
}
$strLinks = substr($strLinks, 0, -strlen($this->pageSeparator));
if($start + $this->linksToDisplay - 1 < $pageCount){
$strLinks .= $this->pageSeparator.'<a class="bodLink" href="'.$this->linksHref.$queryString.$pageCount.'">';
$strLinks .= '...'.$pageCount.'</a>'.$this->pageSeparator;
if(!empty($this->pageJumpNext)){
$pageNum = $start + $this->linksToDisplay + $linksPad;
if($pageNum > $pageCount){
$pageNum = $pageCount;
}
$strLinks .= '<a class="bodLink" href="'.$this->linksHref.$queryString.$pageNum.'">';
$strLinks .= $this->pageJumpNext.'</a>';
}
}
}
return $strLinks;
}
function GetQueryString(){
$pattern = array('/'.$this->queryStringVar.'=[^&]*&?/', '/&$/');
$replace = array('', '');
$queryString = preg_replace($pattern, $replace, $this->queryString);
$queryString = str_replace('&', '&', $queryString);
if(!empty($queryString)){
$queryString.= '&';
}
return '?'.$queryString.$this->queryStringVar.'=';
}
function GetSqlLimit(){
return ' LIMIT '.($this->currentPage * $this->itemsPerPage - $this->itemsPerPage).', '.$this->itemsPerPage;
}
function GetLimit(){
return ' LIMIT '.($this->currentPage-1)*$this->itemsPerPage.', '.$this->itemsPerPage;
}
function PaginateIt(){
$this->SetCurrentPage(1);
$this->SetItemsPerPage(10);
$this->SetItemCount(0);
$this->SetLinksFormat('« Back',' • ','Next »');
$this->SetLinksHref($_SERVER['PHP_SELF']);
$this->SetLinksToDisplay(10);
$this->SetQueryStringVar('page');
$this->SetQueryString($_SERVER['QUERY_STRING']);
if(isset($_GET[$this->queryStringVar]) && is_numeric($_GET[$this->queryStringVar])){
$this->SetCurrentPage($_GET[$this->queryStringVar]);
}
}
}
$PaginateIt = new PaginateIt();
?>
WOKE lu bikin satu lagi file dengan code seperti ini, untuk uji coba:Simpan dengan nama paginate2.php
<?
//Koneksi
$_userDbase="aku"; //ganti dengan user Dbase
$_passDbase="padamu_sayang"; //ganti dengan pass Dbase
$_namaDbase="database_gallery"; //ganti dengan nama Dbase
$con=mysql_connect("localhost","$_userDbase","$_passDbase");
$dBase=mysql_select_db("$_namaDbase", $con);
include('inc/page_class.php');
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemsPerPage(15); //jumlah data perhalaman yg dikehendaki
$PaginateIt->SetLinksToDisplay(3); // jumlah link navigasi halaman
//QUERY pertama , jumlah aktual data
$QRY = mysql_query("SELECT * FROM com_w_tips order by comPostTime desc");
$result = mysql_num_rows($QRY);
$PaginateIt->SetItemCount($result);
// QUERY kedua, mengambil data yang di harapkan saja
$SQL = "SELECT * FROM com_w_tips order by comPostTime desc" . $PaginateIt->GetSqlLimit();
$QRY = mysql_query($SQL);
// looping perintah QUERY kedua
if($result>0) {
echo"
<div align=center>
Hal. "; $PaginateIt->SetLinksFormat(" << Prev "," | "," Next >> ");
echo $PaginateIt->GetPageLinks();
echo"</div>
<br><br>
LIST DATA TIPS, TRIK, ARTIKEL dremi.info - Jumlah Data $result
";
echo"<table width='100%' border='0' cellspacing='2' cellpadding='1'>";
for ($i=1;$i<=$result;$i++) {
while($ROW=mysql_fetch_array($QRY))
{
echo"
<tr>
<td width='3%'>".$i++."</td>
<td width='97%'><a href='wwwtips.php?cID=$ROW[comID]' class='bodLink'>$ROW[comName]</a></td>
</tr>
<tr>
<td></td>
<td><font class='textfootenable'>Posted: $ROW[comPostTime]</font></td>
</tr>";
}
echo"</table>";
}
}
?>
Cara kedua ini memiliki kelebihan dapat memproses keluaran data yang sebelumnya kita atur dalam class, dan ini tentunya akan menghemat energy dalam pembuatannya, karena untuk mengubah jumlah tampilan data perhalaman, cukup dengan mengubah angka pada baris :
$PaginateIt->SetItemsPerPage(
15);
Dan untuk variable halaman ($page) itu ga perlu di set, nantinya akan otomatis akan mengambil sendiri variablenya.
Wah panjang yak, hahahaa ya gitu deeee...
Ni untuk contoh hasil jadinya:
http://www.dremi.info/paginate2.phpKalo mau download file jadina klik ini:
http://www.dremi.info/web/tips/download/paginate2.zipSemoga bermanfaat
