Paging using php (first prev 1 234567 next last)

May 21, 2009 at 10:54 am 1 comment

Paging alias penomoran halaman sering sekali digunakan di website, terutama untuk menampilkan data yang banyak dan perlu ditampilkan dalam beberapa halaman, misalnya bukutamu, di buku tamu suatu web banyak sekali yang posting, sehingga jika ditampilkan dari awal posting sampai yang terakhir mungkin akan panjang sekali. Sehingga perlu ditampilkan dalam beberapa halaman. Sebagai contoh saya coba menampilkan data mahasiswa sebagai berikut :

Buat file Paging.php dengan coding sebagai berikut :

<?

class Paging {

private $conn;

private $root;

private $rows;

private $total_rows;

private $links;

private $sql;

private $page;

private $max;

function __construct($conn, $sql, $root, $rows, $links) {

$this->conn = $conn;

$this->sql = $sql;

$this->root = $root;

$this->rows = $rows;

$this->links = $links;

if (isset($_GET[‘page’])) {

$this->page = intval($_GET[‘page’]);

}

}

function paging() {

$rs1 = $this->conn->query($this->sql);

if (!rs1){

return false;

}

$this->total_rows = $rs1->num_rows;

$rs1->close();

$this->max= ceil($this->total_rows / $this->rows);

if ($this->page > $this->max || $this->page <= 0) {

$this->page = 1;

}

$offset = $this->rows * ($this->page-1);

$rs = $this->conn->query($this->sql . ‘ LIMIT ‘ . $offset . ‘, ‘ . $this->rows);

if (!$rs) {

return false;

}

return $rs;

}

public function show() {

if ($this->page==1) {

$first = ‘first’;

} else {

$first = ‘<a href=”‘.$this->root.’?page=1″>first</a>’;

}

if ($this->page > 1 ) {

$prev = ‘<a href=”‘.$this->root.’?page=’.($this->page-1).'”>prev</a>’;

} else {

$prev = ‘prev’;

}

for ($i = 1; $i <= $this->max;  $i += $this->links) {

if ($this->page >= $i) {

$start = $i;

}

}

if ($this->max > $this->links){

$end = $start + $this->links;

if ($end > $this->max) {

$end = $this->max+1;

}

}   else {

$end = $this->max;

}

$num = ”;

for ($i = $start; $i < $end; $i++) {

if ($i == $this->page) {

$num .=’ ‘ .$i. ‘ ‘;

} else {

$num .='<a href=”‘ .$this->root. ‘?page=’ .$i. ‘”>’ .$i. ‘</a>’;

}

}

if ($this->page < $this->max) {

$next = ‘<a href=”‘ .$this->root.’?page=’.($this->page+1). ‘”>next</a>’;

} else {

$next = ‘next’;

}

if ($this->page == $this->max) {

$last = ‘last’;

} else {

$last = ‘<a href=”‘ .$this->root. ‘?page=’ .$this->max. ‘”>last</a>’;

}

echo $first, ‘ ‘, $prev, ‘ ‘, $num, ‘ ‘,$next,’ ‘,$last;

}

}

?>

Kemudian buat file tampil123.php dengan coding sebagai berikut :

<?

require ‘Paging.php’;

require ‘GeneratorTable.php’;

$mysqli = new mysqli(‘localhost’,’root’,’password’,’t78187_amik’);

$sql = ‘select nim,nama,tmplhr from mahasiswa’;

$self = $_SERVER[‘PHP_SELF’];

$pg = new Paging($mysqli,$sql,$self,10,7);

$res = $pg->paging();

$gt = new GeneratorTable($res);

$gt->showTabel();

$pg->show();

?>

Dari coding di atas ada instansiansi dari fungsi GeneratorTable, jadi file GeneratorTable.php masih saya gunakan. Kemudian coba dijalankan file tampil123.php sehingga hasilnya seperti berikut :

generatorTable2

jadi khan ?

Entry filed under: Web Programming. Tags: .

Generator Table using php Menambah menu Dosen dengan struts

1 Comment Add your own

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


May 2009
M T W T F S S
« Mar   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031

%d bloggers like this: