Effective Paging in PHP

i've got an interesting code to code paging in php. and it works great. I copy and paste it from other site, check it out...


mysql_connect('localhost', 'root', 'root');
mysql_select_db('contoh');


// jumlah data yang akan ditampilkan per halaman

$dataPerPage = 5;

// apabila $_GET['page'] sudah didefinisikan, gunakan nomor halaman tersebut,
// sedangkan apabila belum, nomor halamannya 1.

if(isset($_GET['page']))
{
$noPage = $_GET['page'];
}
else $noPage = 1;

// perhitungan offset

$offset = ($noPage - 1) * $dataPerPage;

// query SQL untuk menampilkan data perhalaman sesuai offset

$query = "SELECT * FROM guestbook LIMIT $offset, $dataPerPage";

$result = mysql_query($query) or die('Error');

// menampilkan data

echo "";
echo "";
while($data = mysql_fetch_array($result))
{
echo "";
}

echo "
NamaEmailTanggalKomentar
".$data['nama']."".$data['email']."".$data['tanggal']."".$data['komentar']."
";

// mencari jumlah semua data dalam tabel guestbook

$query = "SELECT COUNT(*) AS jumData FROM guestbook";
$hasil = mysql_query($query);
$data = mysql_fetch_array($hasil);

$jumData = $data['jumData'];

// menentukan jumlah halaman yang muncul berdasarkan jumlah semua data

$jumPage = ceil($jumData/$dataPerPage);

// menampilkan link previous

if ($noPage > 1) echo "<a href='".$_SERVER['PHP_SELF']."?page=".($noPage-1)."'><< Prev";

// memunculkan nomor halaman dan linknya

for($page = 1; $page <= $jumPage; $page++)
{
if ((($page >= $noPage - 3) && ($page <= $noPage + 3)) || ($page == 1) || ($page == $jumPage))
{
if (($showPage == 1) && ($page != 2)) echo "...";
if (($showPage != ($jumPage - 1)) && ($page == $jumPage)) echo "...";
if ($page == $noPage) echo " ".$page." ";
else echo " <a href='".$_SERVER['PHP_SELF']."?page=".$page."'>".$page."</a> ";
$showPage = $page;
}
}

// menampilkan link next

if ($noPage < $jumPage) echo "<a href='".$_SERVER['PHP_SELF']."?page=".($noPage+1)."'>Next >>";

Komentar

Postingan Populer