Belajar Online PHP Pagination

Arief | 20.59 | |
PHP pagination atau pemberian nomor halaman. Pagination penting buat menampilkan data yang sangat banyak, dengan pagination maka data2 tersebut akan ditampilkan bagian per bagian sesuai dengan permintaan. Misal ada data 1000, tidak mungkin kita tampilkan seluruhnya karena akan membuat halaman web tersebut panjang ke bawah yang akan menyulitkan user, dengan pagination data akan dibagi 10 halaman dan tiap halaman ditampilkan 100 data. User yang ingin melihat data selanjutnya tinggal klik ke halaman berikutnya.Disini saya menggunakan bahasa PHP dalam membuat pagination dan tentu saja scriptnya bukan bikinan sendiri cuman cuman copy paste. Langkah pertama copy script dibawah dan save dengan nama ps_pagination.php.

===================================================================
<?php
/**
* PHPSense Pagination Class
*
* PHP tutorials and scripts
*
* @package PHPSense
* @author Jatinder Singh Thind
* @copyright Copyright (c) 2006, Jatinder Singh Thind
* @link http://www.phpsense.com
*/

// ------------------------------------------------------------------------

class PS_Pagination {
var $php_self;
var $rows_per_page; //Number of records to display per page
var $total_rows; //Total number of rows returned by the query
var $links_per_page; //Number of links to display per page
var $sql;
var $debug = false;
var $conn;
var $page;
var $max_pages;
var $offset;

/**
* Constructor
*
* @param resource $connection Mysql connection link
* @param string $sql SQL query to paginate. Example : SELECT * FROM users
* @param integer $rows_per_page Number of records to display per page. Defaults to 10
* @param integer $links_per_page Number of links to display per page. Defaults to 5
*/

function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5) {
$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_page = $rows_per_page;
$this->links_per_page = $links_per_page;
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);
if(isset($_GET['page'])) {
$this->page = intval($_GET['page']);
}
}

/**
* Executes the SQL query and initializes internal variables
*
* @access public
* @return resource
*/
function paginate() {
if(!$this->conn) {
if($this->debug) echo "MySQL connection missing<br />";
return false;
}

$all_rs = @mysql_query($this->sql);
if(!$all_rs) {
if($this->debug) echo "SQL query failed. Check your query.<br />";
return false;
}
$this->total_rows = mysql_num_rows($all_rs);
@mysql_close($all_rs);

$this->max_pages = ceil($this->total_rows/$this->rows_per_page);
//Check the page value just in case someone is trying to input an aribitrary value
if($this->page > $this->max_pages $this->page <= 0) {
$this->page = 1;
}

//Calculate Offset
$this->offset = $this->rows_per_page * ($this->page-1);

//Fetch the required result set
$rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}");
if(!$rs) {
if($this->debug) echo "Pagination query failed. Check your query.<br />";
return false;
}
return $rs;
}

/**
* Display the link to the first page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'First'
* @return string
*/
function renderFirst($tag='First') {
if($this->page == 1) {
return '<div style="margin-right:0px;padding:1px;float:left;border:0px solid black;"><font face=arial size=2><b>'.$tag.'</b></font></div>';
}
else {
return '<div style="margin-right:2px;padding:2px;float:left;border:0px solid black;"><a href="'.$this->php_self.'?page=1"><font face=arial size=2><b>'.$tag.'</b></font></a></div>';
}
}

/**
* Display the link to the last page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'Last'
* @return string
*/
function renderLast($tag='Last') {
if($this->page == $this->max_pages) {
return '<div style="margin-left:2px;width:35px;padding:2px;float:left;border:0px solid black;"><font face=arial size=2><b>'.$tag.'</b></font></div>';
}
else {
return '<div style="margin-left:2px;padding:2px;width:35px;float:left;border:0px solid black;"><a href="'.$this->php_self.'?page='.$this->max_pages.'"><font face=arial size=2><b>'.$tag.'</b></font></a></div>';
}
}

/**
* Display the next link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '>>'
* @return string
*/
function renderNext($tag=' &gt;&gt;') {
if($this->page < $this->max_pages) {
return '<div style="margin-left:2px;width:5px;height:5px;padding:2px;float:left;border:0px solid black;"><a href="'.$this->php_self.'?page='.($this->page+1).'"><font face=arial size=2><b>'.$tag.'</b></font></a></div>';
}
else {
return '<div style="margin-left:2px;width:5px;height:5px;padding:2px;float:left;border:0px solid black;"><font face=arial size=2><b>'.$tag.'</b></font></div>';
}
}

/**
* Display the previous link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '<<'
* @return string
*/
function renderPrev($tag='&lt;&lt;') {
if($this->page > 1) {
return '<div style="margin-left:2px;width:5px;height:5px;padding:2px;float:left;border:0px solid black;"><a href="'.$this->php_self.'?page='.($this->page-1).'"><font face=arial size=2><b>'.$tag.'</b></font></a></div>';
}
else {
return '<div style="margin-left:2px;width:5px;height:5px;padding:2px;float:left;border:0px solid black;"><font face=arial size=2><b>'.$tag.'</b></font></div>';
}
}

/**
* Display the page links
*
* @access public
* @return string
*/
function renderNav() {
for($i=1;$i<=$this->max_pages;$i+=$this->links_per_page) {
if($this->page >= $i) {
$start = $i;
}
}

if($this->max_pages > $this->links_per_page) {
$end = $start+$this->links_per_page;
if($end > $this->max_pages) $end = $this->max_pages+1;
}
else {
$end = $this->max_pages;
}

$links = '';

for( $i=$start ; $i<$end ; $i++) {
if($i == $this->page) {
$links .= "<div style='margin-left:2px;width:5px;height:5px;padding:2px;float:left;background:white;border:1px solid black;'><font face=arial size=2><b> $i </b></font></div> ";
}
else {
$links .= '<div style="margin-left:2px;width:5px;height:5px;padding:2px;float:left;background:white;border:1px solid black;"> <a href="'.$this->php_self.'?page='.$i.'"><font face=arial size=2><b>'.$i.'</b></font></a></div> ';
}
}

return $links;
}

/**
* Display full pagination navigation
*
* @access public
* @return string
*/
function renderFullNav() {
return $this->renderFirst().''.$this->renderPrev().''.$this->renderNav().''.$this->renderNext().''.$this->renderLast();
}

/**
* Set debug mode
*
* @access public
* @param bool $debug Set to TRUE to enable debug messages
* @return void
*/
function setDebug($debug) {
$this->debug = $debug;
}
}
?>
====================================================================
setelah disimpan kini buat file buat menampilkan datanya :

====================================================================
<?php
include('ps_pagination.php');
$conn = mysql_connect('localhost','root');
mysql_select_db('data',$conn);
$sql = "SELECT * from anggota"
$pager = new PS_Pagination($conn,$sql,50,5);-->yg dibold=untuk mengatur jumlah data yg akan ditampilkan,jumlah halaman yg akan ditampilkan.
$rs = $pager->paginate();
echo("page dari $pager->max_pages halaman");
echo("<table style="" cellpadding="2" cellspacing="1">");
echo("<tr bgcolor=#2F4F4F align=center><td><span style=";" ><b>No</b></span></td><td><span style=";" ><b>No Nama</b></span></td><td><span style=";" ><b>Alamat</b></span></td><td><span style=";" ><b>Tgl Lahir</b></span></td></tr>");
$pa=$pager->page;
$no=($pa-1)*50;
while($row = mysql_fetch_array($rs)) {
$no=$no+1; --> yg dibold= agar no urutnya bersambung ke halaman berikutnya
$no1=$no;
$no2=$no1/2;
$no3=round($no1/2);
if ($no2==$no3) {
$color="#DCDCDC";
} else {
$color="#F5F5F5";
}

$nama=$row['nama'];
$alamat=$row['alamat'];
$lahir=$data['lahir'];
echo("<tr align="center"><td style="" bgcolor="#0c0000">$no1</td><td bgcolor="#0c0000" width="75">$nama</td><td align="center" bgcolor="#0c0000" width="65">$alamat</td><td align="right" bgcolor="#0c0000" width="85">$lahir</td></tr>");
}
echo("</tbody></table>");
echo $pager->renderfullnav(); -->
====================================================================
Simpan file dengan nama view.php dan paggil file view.php.
Maka data yang ditampilkan per halaman sebanyak 50 data.

8 komentar:

sitorus mengatakan...

terima kasih artikelnya, tak coba dulu yah

arief mengatakan...

@sitorus=sama-sama, monggo dicoba

roman mengatakan...

mas KLo dihubungkan dengan component joomla bsa nggak yah....
jadi databasenya = intranet;


bsa gagh di tambahkan ke component sectionex dari joomla

trims.........

Anonim mengatakan...

mas KLo dihubungkan dengan component joomla bsa nggak yah....
jadi databasenya = intranet;


bsa gagh di tambahkan ke component sectionex dari joomla

trims.........
jawab di email saya yah......

perpusbalit@yahoo.com

Anonim mengatakan...

mantap tulisan nya...tengkyu...ini yg selama ini yg dicari..simple tapi power full dan gampang di mengerti

risdiyanto mengatakan...

gak da penjelasannya?

Donny mengatakan...

Artikelnya bagus,,,makasi ya buat ilmunya...semoga bermanfaan untuk saya

World of Php mengatakan...

@risdiyanto:hehe maaf kalau penjelasannya kurang lengkap soalnya saya cuman copy paste sourcenya aja :).

@donny:sama2 :)

Search