pertama buat database dan beri nama db_uploader seperti ini :
kedua buat file index.php
<?php
error_reporting(E_ALL);
require_once "db_class.php";
require_once "paging_class.php";
$db = new db_class();
if(!$db->connect()){
// jika opsi error di configurasi di munculkan maka error akan tampak
$db->display_errors();
}
$paging = new pagination();
?>
<html>
<head>
<title>kelas paging</title>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
</head>
<body>
<?php
// Inisialisasi query
$number_page = (isset($_GET['page'])) ? $_GET['page'] : "";
$sql = "SELECT * FROM student";
// Jika data tidak ada proses tidak dilanjutkan
if(($num = $db->select($sql,true)) < 1){
$db->display_errors();
echo '<p class="bad">Tidak Ada Data pada tabel</p>';
return FALSE;
}
$query = $sql." LIMIT ".$paging->position($num,$number_page).",". $paging->paging_size;
?>
<div class="container">
<div class="panel-heading">
<table class="table" width="300px">
<tr>
<th>No</th>
<th>Nama</th>
<th>Class</th>
<th>Mark</th>
<th>Sex</th>
</tr>
<?php
$r=$db->select($query);
$i=$paging->position($num,$number_page);
while($row=$db->get_row($r)){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->class; ?></td>
<td><?php echo $row->mark; ?></td>
<td><?php echo $row->sex; ?></td>
</tr>
<?php } ?>
</table>
</div>
<br>
<?php
$paging->paging($_SERVER['PHP_SELF']);
?>
</div>
</body>
</html>
Ketiga buat file paging_class.php
<?php
class pagination {
public $paging_size = 5; // default 5
public $page_interval = 5; //interval for panging 1...10 11...20 etc, default 10
protected $get_page;
protected $max_page;
protected $current_page;
protected $startpaging;
protected $endpaging;
protected $total;
function __construct($config = array()){
if(count($config) > 0)
foreach($config as $key=>$value){
if(is_int($value) && $value > 0) $this->$key = $value;
}
}
function position($num_query,$get_page){ // for page number
$num_query = (!is_numeric($num_query) || $num_query == '') ? 0 : $num_query;
$this->total = $num_query;
$this->max_page = ceil($num_query/$this->paging_size);
$this->get_page=$get_page;
if($this->get_page == "" || $this->get_page < 1) {
$currentPage=0;
$this->get_page=1;
} elseif($this->get_page > $this->max_page) {
$currentPage=($this->max_page-1)*$this->paging_size;
} else $currentPage=($this->get_page-1)*$this->paging_size;
return $currentPage;
}
protected function set_link(){
if($this->get_page <= $this->page_interval){
$this->startpaging = 1;
$this->endpaging = ($this->max_page > $this->page_interval) ? $this->page_interval : $this->max_page;
} else {
if($this->get_page > $this->max_page || $this->get_page == $this->max_page){
$size = ceil($this->max_page/$this->page_interval);
$this->startpaging = ($this->max_page > $this->page_interval) ? ($size - 1) * $this->page_interval + 1 : 1;
$this->endpaging = $this->max_page;
} elseif($this->get_page > $this->page_interval && $this->get_page < $this->max_page){
$size = ceil($this->get_page/$this->page_interval);
$this->startpaging = ($size - 1) * $this->page_interval + 1;
$this->endpaging = (($var = $size * $this->page_interval) > $this->max_page) ? $this->max_page : $var;
}
}
}
function paging($page = ''){
if(empty($page)) return false;
$limiter = (strpos($page, "=") === false) ? "?" : "&";
if($this->max_page > 1){
$this->set_link();
$link = "<center><ul class='pagination'>";
// Link for First and Previous
if($this->max_page > $this->page_interval){
if ($this->get_page > 1){
$previous = $this->get_page-1;
$link .= "<li><a href='$page$limiter"."page=1' ><span aria-hidden='true'>←</span> First</a></li>";
$link .= "<li><a href=\"$page$limiter"."page=$previous\"> Previous</a></li>";
} else $link .= "<li class='disabled'><span> First </span></li><li class='disabled'><span> Previous </span></li>";
}
// Link for page 1,2,3, ...
$num=$this->startpaging - 1;
$link .= ($this->get_page > $this->page_interval && $this->max_page >= $this->get_page || $this->max_page < $this->get_page) ? "<li><a href=\"$page$limiter"."page=$num\" class=\"paging\" > ... </a></li>" : "";
for($i=$this->startpaging;$i<=$this->endpaging;$i++){
if ($i == $this->get_page)
{
$link .= "<li class='active'><span>$i</span></li>";
$this->current_page = $i;
}
else
{
if ($i == $this->endpaging && $this->get_page > $this->max_page)
{
$link .= "<span class=\"current\" >$i</span>";
$this->current_page = $i;
}
else $link .= "<li><a href=\"$page$limiter"."page=$i\" class=\"paging\" >$i</a></li>";
}
}
$link .= ($i-1 < $this->max_page) ? "<li><a href=\"$page$limiter"."page=$i\" class='paging' > ... </a></li>" : "";
// Link for Next dan Last
if($this->max_page > $this->page_interval)
{
if ($this->get_page < $this->max_page)
{
$next=$this->get_page+1;
$link .= "<li><a href=\"$page$limiter"."page=$next\" >Next</a></li>";
$link .= "<li><a href=\"$page$limiter"."page=$this->max_page\" >Last <span aria-hidden='true'>→</span></a></li>";
}
else $link .= "<li class='disabled'><span> Next </span></li><li class='disabled'><span> Last </span></li>";
}
?></ul></center><?php
$link .= "<center><div class='btn btn-primary'>Pages <span class='badge'>{$this->current_page}</span> of <span class='badge'>{$this->total}</span>";
$link .= " Total Record : <span class='badge'>{$this->total}</span> Records</div></center>";
echo $link;
}
}
function __destruct(){
// Jika mau di isi silahkan.
// jikalaupun tidak diisi, biarkan fungsi ini tetap ada, PHP otomatis mengoptimalkan
// objek yang akan dihancurkan ketika proses selesai
}
}
?>
ke empat buat file db_class.php
<?php
class configsys{
//global configuration
var $debug_mode = true; // nilai true untuk mengaktifan mode debug/development, nilai false untuk tidak aktif
var $exit_on_error = true; // nilai true untuk keluar dari eksekusi program jika terjadi error, upayakan selalu bernilai true
//Database setting
var $db_host = "localhost";
var $db_user = "root";
var $db_password = "";
var $db_name = "db_uploader";
}
class db_class extends configsys {
// Variable-variable
protected $fieldtype;
protected $fieldname;
protected $error_msg = array();
var $db_link;
var $auto_slashes;
function __construct() {
$this->auto_slashes = true;
}
// Fungsi untuk melakukan koneksi
function connect($persistant=true) {
if ($persistant) $this->db_link = @mysql_pconnect($this->db_host, $this->db_user, $this->db_password);
else $this->db_link = @mysql_connect($this->db_host, $this->db_user, $this->db_password);
if (!$this->db_link) {
// Jika terjadi error koneksi set pesan error
$this->set_error("Couldn't connect <br/>mysql_error : ".mysql_error());
return false;
}
if (!$this->select_db($this->db_name)){
// Jika terjadi error/database tidak ditemukan set pesan error
$this->set_error("Couldn't find database <br/>mysql_error : ".mysql_error());
return false;
}
return $this->db_link;
}
protected function select_db($db) {
if (!@mysql_select_db($this->db_name)) {
return false;
}
return true;
}
// mengambil data dari tabel
// kembalian berupa array data
function select($sql,$getnumrows=false) {
$r = mysql_query($sql);
if (!$r) {
$this->set_error("An Error on : $sql <br/>mysql_error : ".mysql_error());
return false;
}
if($getnumrows==true) {
$num_rows = mysql_num_rows($r);
mysql_free_result($r);
return $num_rows;
} else return $r;
}
// mengambil data dari hasil "function select($sql,$getnumrows=false)"
// kembalian berupa array data
function get_row($result) {
if (!$result) {
return false;
}
$row = mysql_fetch_object($result);
if (!$row) {
$this->set_error("Can't find result for your request");
return false;
}
if ($this->auto_slashes) {
foreach ($row as $obj => $value) {
$row->$obj = stripslashes($value);
}
}
return $row;
}
// Fungsi untuk menampung error selama pengoperasian
protected function set_error($msg){
if (is_array($msg)) {
foreach ($msg as $val){
$this->error_msg[] = $msg;
}
} else $this->error_msg[] = $msg;
}
// Fungsi untuk memunculkan pesan error selama pengoperasian
function display_errors($open = '-', $close = '<br/><br/>') {
if(count($this->error_msg) == 0) return false;
$str = '';
foreach ($this->error_msg as $val) {
$str .= $val.$close;
}
if($this->debug_mode){
$html_msg = '
<div style="border: 1px solid red; font-size: 9pt; font-family: monospace; color: red; padding: .5em; margin: 8px; -moz-border-radius:4px;border-radius:4px;-webkit-border-radius:4px;background-color: #FFE2E2;">
<span style="font-weight: bold">Error Message:</span><br>'.$str.'
</div>';
echo $html_msg;
if($this->exit_on_error) exit();
}
}
function __destruct(){
}
}
?>
.
atau bisa download source code nya disini
0 komentar:
Posting Komentar