Kita akan membuat Paging dengan PHP. Lanjut dari file index.php dari Buku Tamu dengan PHP dan MYSQL yang telah dibahas. Berikut ini script membuat paging.
<html>
<head><title>Buku Tamu</title></head>
<body>
<form method=”post” action=”buku_tamu_doupdate.php”>
<h2>Form Buku Tamu</h2>
<table>
<tr><td>Nama</td><td><input type=”text” name=”nama” size=”20″></tr>
<tr><td>Email</td><td><input type=”text” name=”email” size=”30″></tr>
<tr><td>Alamat</td><td><textarea name=”alamat” rows=”10″ cols=”30″></textarea></tr>
<tr><td>Kota</td><td><input type=”text” name=”kota” size=”30″></tr>
<tr>
<td>Pesan</td>
<td><textarea name=”pesan” rows=”10″ cols=”30″></textarea></tr>
<tr><td></td><td><input type=”submit” name=”tombol” value=”Kirim”></tr>
<tr>
<td colspan=”2″ align=”center”><font color=”#FF0000″><? $err=$_GET[‘err’];
echo “$err”;?>
</font></td>
</tr>
</table>
</form>
<font color=”#0000FF”><b>Daftar Buku Tamu</b></font></br>
<table width=”626″ border=”1″ cellpadding=”0″ cellspacing=”0″>
<tr bgcolor=”#CCCCCC”>
<td width=”20″>No</td>
<td width=”183″>Nama</td>
<td width=”128″>Email</td>
<td width=”191″>Alamat</td>
<td width=”70″>Kota</td>
<td width=”70″>Pesan</td>
</tr>
<? include “mysql_connect.php”;
// how many rows to show per page
$rowsPerPage = 1;
// by default we show first page
$pageNum = 1;
// if $_GET[‘page’] defined, use it as page number
if(isset($_GET[‘page’]))
{
$pageNum = $_GET[‘page’];
}
// counting the offset
$offset = ($pageNum – 1) * $rowsPerPage;
$result = mysql_query(“SELECT * FROM buku_tamu order by id ASC LIMIT $offset, $rowsPerPage”);
//$init=0;
$init=($pageNum * $rowsPerPage) – $rowsPerPage;
while ($row = mysql_fetch_array ($result)) {
$init++;
if ( $init%2 == 1 ) {
$warna = “#FEF4E7”;
} else {
$warna = “#E0E9F3″;
}
$nama=$row[2];
$email=$row[3];
$alamat=$row[4];
$kota=$row[5];
$pesan=$row[6];
?>
<tr>
<td bgcolor=”<? echo $warna ?>”><? echo “$init”;?></td>
<td bgcolor=”<? echo $warna ?>”><? echo “$nama”;?></td>
<td bgcolor=”<? echo $warna ?>”><? echo “$email”;?></td>
<td bgcolor=”<? echo $warna ?>”><? echo “$alamat”;?></td>
<td bgcolor=”<? echo $warna ?>”><? echo “$kota”;?></td>
<td bgcolor=”<? echo $warna ?>”><? echo “$pesan”;?></td>
</tr>
<? }?>
</table>
<center><?
// how many rows we have in database
$MyQuery = “SELECT COUNT(id) AS numrows FROM buku_tamu “;
//echo “Q4:$MyQuery<br><br>”;
$MyRst = mysql_query($MyQuery,$GLOBALS[MyDB]);
if ($MyRow=mysql_fetch_array($MyRst)) {
$numrows = $MyRow[0];
}
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER[‘PHP_SELF’]; //atau nama halamannya misal index.php
// creating ‘previous’ and ‘next’ link
// plus ‘first page’ and ‘last page’ link
// print ‘previous’ link only if we’re not
// on page one
if ($pageNum > 1)
{
$page = $pageNum – 1;
$prev = ” <a href=\”$self?page=$page\”>Prev</a> “;
$first = ” <a href=\”$self?page=1\”>First</a> “;
}
else
{
$prev = ‘ Prev ‘; // we’re on page one, don’t enable ‘previous’ link
$first = ‘ First ‘; // nor ‘first page’ link
}
// print ‘next’ link only if we’re not
// on the last page
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = ” <a href=\”$self?page=$page\”>Next</a> “;
$last = ” <a href=\”$self?page=$maxPage\”>Last</a> “;
}
else
{
$next = ‘ Next ‘; // we’re on the last page, don’t enable ‘next’ link
$last = ‘ Last ‘; // nor ‘last page’ link
}
// print the page navigation link
if ($language==” || $language==’0′) {
echo $first . $prev . ” Tampil hal <strong>$pageNum</strong> dari <strong>$maxPage</strong> halaman” . $next . $last;
} else {
echo $first . $prev . ” Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages ” . $next . $last;
}
?></center>
</body>
</html>
Selamat mencoba. ^_^