Read 164 times | Created 2013-03-12 08:40:44 | Updated 2013-03-12 08:44:10 | | |

 

<?php
class paging{
  public $total_page=0;
  public $current_page;
  public $position=array();
  public $limit=5;
  public $data_per_page=5;
  
  function paging($total_data,$data_per_page=5,$limit=5){
    $this->current_page=isset($_GET['halaman'])?$_GET['halaman']:1;
    $this->limit=$limit;
    $this->get_position($total_data);
    $this->data_per_page=$data_per_page;
    $this->get_total($total_data, $data_per_page);
  }
  
  function get_position($total_data){
    $this->position['start']=($this->current_page-1) * $this->data_per_page +1;
    $this->position['end']=$this->position['start']+$this->data_per_page-1;
    $this->position['end']=$this->position['end']+$this->data_per_page<$total_data?
                           $this->position['end']:$total_data;
  }

  function get_total($total_data, $data_per_page){
    $this->total_page = ceil($total_data/$data_per_page);
  }
  
  function show(){
    $link='';
    for($i=$this->current_page;$i<$this->current_page+$this->limit;$i++){
      if($i<$this->total_page){
        $link.="<a href='?halaman=$i'"
              .($i==$this->current_page?" class='current_page'":"")
              .">$i</a> | ";
      }
    }
    if($this->current_page<$this->total_page){
      $link .= " <a href='?halaman=".($this->current_page+1)."'>next</a> | ";
      $link .= " <a href='?halaman=".$this->total_page."'>last</a>";

    }
    if($this->current_page>1){
      $link = " <a href='?halaman=1'> first</a> | "
            ." <a href='?halaman=".($this->current_page-1)."'> prev</a> | ".$link;
    }
    return $link;
  }
  
}
//== CONTOH PEMAKAIAN ===
$jumlah_data             = 54;
$jumlah_data_per_halaman = 5;
$jumlah_link_maksimal    = 5;
$p=new paging($jumlah_data,$jumlah_data_per_halaman,$jumlah_link_maksimal);
?>
<!DOCTYPE html>
<html>
  <head>
    <title>paging</title>
    <style>
      a {text-decoration:none;}
      a:hover{text-decoration:none;border-bottom:solid 1px #999;}
      .current_page {color:#900;font-weight:bold;}
    </style>
  </head>
  <body>
    <h1>HALAMAN #<?php echo isset($_GET['halaman'])?$_GET['halaman']:1?></h1>
    <h3>
    	data yang ditampilkan dari : 
    	<?php echo $p->position['start']." s.d. ".$p->position['end'];?>
    	dari total : <?php echo $jumlah_data;?> data
    </h3>
    <?php
    echo $p->show();
    ?>
  </body>
</html>