Pages

Subscribe:

Labels

Selasa, 15 Januari 2013

Membuat Pagination dengan PHP

Cara membuat Pagination dengan PHP adalah :
1.  Buat script dbc.php buat konek ke database, masukkan script berikut kemudian simpan dengan nama dbc.php
<?php
mysql_connect
(“host”,“user_db”,“pass_db”);
mysql_select_db(“nama_db”);
?>

Sesuaikan settingan databasenya dengan punya Anda
2. Buat file halaman.php masukkan script berikut kemudian simpan sebagai halaman.php
PHP Code:
<?php
class PageNumber{
var
$limit;
var
$query;
var
$page;
var
$start;
var
$TotalRecord;
var
$TotalPage;
var
$TotalPageNumber;
var
$FirstPageNumber;
var
$LastPageNumber;
var
$FirstRecordPos;
var
$LastRecordPos;
var
$next;
var
$prev;
function
GetJmlHal(){
$hasil = $this->TotalRecord/$this->limit;
if((
$temp = (int)$hasil)<$hasil) $hasil = $temp + 1;
return(
$hasil);
}
function
GetStart(){
if(
$this->page > 1){
return (
$this->page-1)*$this->limit;
}
return(
0);
}
function
GetFirstNumber(){
$temp = $this->page % $this->TotalPageNumber;
if(
$temp){
$result = $this->page-$temp+1;
if(
$result==$this->TotalPage){
$result = $this->page-$this->TotalPageNumber;
}
}else{
$result = $this->page-$this->TotalPageNumber+1;
}
return(
$result);
}
function
GetLastNumber(){
$result = $this->FirstPageNumber+($this->TotalPageNumber-1);
if(
$result > $this->TotalPage || ($this->TotalPage-$result == 1)){
$result = $this->TotalPage;
}
return(
$result);
}
function
GetFirstRecordPos(){
return(
$this->start+1);
}
function
GetLastRecordPos(){
$result = $this->start+$this->limit;
if(
$result > $this->TotalRecord) $result = $this->TotalRecord;
return(
$result);
}
function
GenerateAll(){
$result = mysql_query($this->query);
$row = mysql_fetch_array($result);
$this->TotalRecord = $row[0];
$this->TotalPage = $this->GetJmlHal();
if(
$this->page > $this->TotalPage) $this->page = $this->TotalPage;
$this->start = $this->GetStart();
$this->FirstPageNumber = $this->GetFirstNumber();
$this->LastPageNumber = $this->GetLastNumber();
$this->next = $this->page+1;
$this->prev = $this->page-1;
$this->FirstRecordPos = $this->GetFirstRecordPos();
$this->LastRecordPos = $this->GetLastRecordPos();
mysql_free_result($result);
}
}
?>


3. Buat halaman untuk menampilkan pagenation
PHP Code:
<?php
 include('dbc.php');
 include('halaman.php');

 $pagenumber = new PageNumber();
 //Show Record
 $pagenumber->limit  = 4;
 $pagenumber->page  = $_GET['page'] ? $_GET['page'] : 1;
 $pagenumber->query = "select count(*) from formiklan";
 //Show PageNumber
 $pagenumber->TotalPageNumber  = 3;
 $pagenumber->GenerateAll();

 if($pagenumber->TotalRecord){

echo(" Menampilkan halaman {$pagenumber->page} dari {$pagenumber->TotalPage} halaman <br/>");
  if($pagenumber->TotalPage > 1){

   if($pagenumber->page<=1){
    echo("&lt;&lt;Prev");
   }else{
    $prev = $page-1;
    echo("<a href=\"?page={$pagenumber->prev}\">&lt;&lt;Prev</a>");
   }

   for($i=$pagenumber->FirstPageNumber;$i<=$pagenumber->LastPageNumber;$i++){
    echo("<a href=\"?page={$i}\"> $i </a>");
   }

   if($pagenumber->page==$pagenumber->TotalPage){
    echo("Next&gt;&gt;");
   }else{
    echo("<a href=\"?page={$pagenumber->next}\">Next&gt;&gt;</a>");
   }
  }

include("dbc.php");
  $query=mysql_query("SELECT * FROM formiklan ORDER BY id desc limit {$pagenumber->start}, {$pagenumber->limit}");
  while ($record=mysql_fetch_array($query)){
   echo "<table width=\"450px\"><tr><td width=\"100px\" align=\"left\">Nama</td> <td width=\"5px\">:</td><td align=\"left\"> ".$record['nama']."</td></tr>";
   echo "<tr><td width=\"100px\" align=\"left\">Contact Person</td> <td width=\"5px\">:</td><td align=\"left\"> ".$record['cp']."</td></tr>";
   echo "<tr><td width=\"100px\" valign=\"top\">Deskripsi Iklan</td> <td width=\"5px\" valign=\"top\">:</td><td align=\"left\"> ".$record['detail']."</td></tr>";
   echo "<hr/><br/></table><br/>";

  }

  echo(" Menampilkan halaman {$pagenumber->page} dari {$pagenumber->TotalPage} halaman <br/>");
  if($pagenumber->TotalPage > 1){

   if($pagenumber->page<=1){
    echo("&lt;&lt;Prev");
   }else{
    $prev = $page-1;
    echo("<a href=\"?page={$pagenumber->prev}\">&lt;&lt;Prev</a>");
   }

   for($i=$pagenumber->FirstPageNumber;$i<=$pagenumber->LastPageNumber;$i++){
    echo("<a href=\"?page={$i}\"> $i </a>");
   }

   if($pagenumber->page==$pagenumber->TotalPage){
    echo("Next&gt;&gt;");
   }else{
    echo("<a href=\"?page={$pagenumber->next}\">Next&gt;&gt;</a>");
   }
  }

 }
?>
Sesuaikan dengan settingan database Anda dan apa yang mau ditampilkan,



sumber

0 komentar:

Posting Komentar