phpMySQL

Buat paging di PHP

Ini script paging yang biasa saya gunakan, silahkan dimodifikasi sesuai keinginan Anda.

<?php
mysql_connect(‘namahost’, ‘dbuser’, ‘dbpassword’);
mysql_select_db(‘dbname’);

$dataPerPage = 5;

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

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

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

$result = mysql_query($query) or die(‘Error’);

echo “<table border=’1′>”;
echo “<tr><td>Nama</td><td>Email</td><td>Tanggal</td><td>Komentar</td></tr>”;
while($data = mysql_fetch_array($result))
{
echo “<tr><td>”.$data[‘nama’].”</td><td>”.$data[’email’].”</td><td>”.$data[‘tanggal’].”</td><td>”.$data[‘komentar’].”</td></tr>”;
}

echo “</table>”;

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

$jumData = $data[‘jumData’];

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

if ($noPage > 1) echo  “<a href='”.$_SERVER[‘PHP_SELF’].”?page=”.($noPage-1).”‘>&lt;&lt; Prev</a>”;

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 ” <b>”.$page.”</b> “;
else echo ” <a href='”.$_SERVER[‘PHP_SELF’].”?page=”.$page.”‘>”.$page.”</a> “;
$showPage = $page;
}
}

if ($noPage < $jumPage) echo “<a href='”.$_SERVER[‘PHP_SELF’].”?page=”.($noPage+1).”‘>Next &gt;&gt;</a>”;

?>

Leave a Reply

Your email address will not be published. Required fields are marked *