<?php
session_start();
$user = "root";
$pass = "";
$host = "localhost";
$db = "test";
$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db);
if (!$conn) {
echo"Gagal Koneksi ke Database";
}
$kataKunci = '';
if (isset($_GET['kataKunci'])) {
$kataKunci = $_GET['kataKunci'];
}
$limitPaging = 5;
if ($kataKunci != "") {
$sqlPaging = "SELECT COUNT(*) as TOTAL FROM nilai_s WHERE Nama like'%" . $kataKunci . "%'";
} else {
$sqlPaging = "SELECT COUNT(*) as TOTAL FROM nilai_s";
}
$resultPaging = mysql_query($sqlPaging);
$dataPaging = mysql_fetch_array($resultPaging);
$totalData = $dataPaging['TOTAL'];
if ($totalData > 0) {
$totalPage = ceil($totalData / $limitPaging);
$page = $_GET['page'];
{
if ($page == "") {
$offset = 0;
$currentPage = 1;
} else {
$currentPage = $page;
$offset = ($page - 1) * $limitPaging;
}
$firstPage = 1;
$lastPage = $totalPage;
$nextPage = $currentPage + 1;
$prevPage = $currentPage - 1;
}
}
?>
<form action="?p=4&&page=1" method="get">
<fieldset>
<legend>Form Pencarian</legend>
<input type="text" name="kataKunci" />
<input type="hidden" name="p" value="4"/>
<input type="hidden" name="page" value="1"/>
<input name="search" type="submit" value="search" />
<b>Kata Kunci Anda : </b>"<?php echo $kataKunci; ?>"
</fieldset>
</form><br/><br/>
<?php
if ($totalData > 0) {
?>
<table align="center" width="100%">
<tr>
<th width="15%"> </th>
<th width="15%">
<?php if ($currentPage != 1) { ?>
<a href="<?php echo "?p=4&&page={$firstPage}&kataKunci={$kataKunci}"; ?>"> << Hal. Pertama </a>
<?php } ?>
</th>
<th width="15%">
<?php if ($currentPage != 1) { ?>
<a href="<?php echo "?p=4&&page={$prevPage}&kataKunci={$kataKunc}i"; ?>">< Hal. Sebelumnya</a>
<?php } ?>
</th>
<th> <?php echo"$currentPage/$totalPage " ?> </th>
<th width="15%">
<?php if ($currentPage != $lastPage) { ?>
<a href="<?php echo "?p=4&&page={$nextPage}&kataKunci={$kataKunci}"; ?>">Hal. Selanjutnya ></a>
<?php } ?>
</th>
<th width="15%">
<?php if ($currentPage != $lastPage) { ?>
<a href="<?php echo "?p=4&&page={$lastPage}&kataKunci={$kataKunci}"; ?>">Hal. Terakhir > > </a>
<?php } ?>
</th>
<th width="15%"> </th>
</tr>
</table><br/>
<?php
if ($kataKunci != "") {
$sqlOrang = "SELECT * FROM nilai_s WHERE Nama LIKE '%" . $kataKunci . "%' LIMIT $offset,$limitPaging";
} else {
$sqlOrang = "SELECT * FROM nilai_s LIMIT $offset,$limitPaging";
}
$resultOrang = mysql_query($sqlOrang);
?>
<table class="bordered" >
<tr>
<th >No</th>
<th>Nama</th>
</tr>
<?php
$no = $offset + 1;
while ($dataOrang = mysql_fetch_array($resultOrang)) {
echo"<tr>";
echo"<td>" . $no . "</td>";
echo"<td>" . $dataOrang['Nama'] . "</td>";
echo"</tr>";
$no++;
}
?>
</table>
<?php
} else {
echo"Data dengan Kata Kunci : <b>'" . $kataKunci . "'</b> Tidak Ditemukan !!!";
}
?>
<table align="center" width="100%">
<tr>
<th width="15%"> </th>
<th width="15%">
<?php if ($currentPage != 1) { ?>
<a href="<?php echo "?p=4&&page={$firstPage}&kataKunci={$kataKunci}"; ?>"> << Hal. Pertama </a>
<?php } ?>
</th>
<th width="15%">
<?php if ($currentPage != 1) { ?>
<a href="<?php echo "?p=4&&page={$prevPage}&kataKunci={$kataKunci}"; ?>">< Hal. Sebelumnya</a>
<?php } ?>
</th>
<?php
for($i = 1; $i <= $totalPage; $i++){?>
<th>
<?php if($i == $currentPage){
echo $i;
}else{?>
<a href="<?php echo "?p=4&&page={$i}&kataKunci={$kataKunc}"; ?>"><?php echo $i?></a>
<?php }?>
</th>
<?php
}
?>
<th width="15%">
<?php if ($currentPage != $lastPage) { ?>
<a href="<?php echo "?p=4&&page={$nextPage}&kataKunci={$kataKunci}"; ?>">Hal. Selanjutnya ></a>
<?php } ?>
</th>
<th width="15%">
<?php if ($currentPage != $lastPage) { ?>
<a href="<?php echo "?p=4&&page={$lastPage}&kataKunci={$kataKunci}"; ?>">Hal. Terakhir > > </a>
<?php } ?>
</th>
<th width="15%"> </th>
</tr>
</table>
tutorial pagination http://www.myphptutorials.com/tutorials/31/membuat-pagination <?php
//file digg.php
//function to return the pagination string
function getPaginationString($page = 1, $totalitems, $limit = 15, $targetpage = "/", $pagestring = "?page=")
{
//defaults
$adjacents = 1;
if(!$limit) $limit = 15;
if(!$page) $page = 1;
if(!$targetpage) $targetpage = "/";
//other vars
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($totalitems / $limit); //lastpage is = total items / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\"";
if($margin || $padding)
{
$pagination .= " style=\"";
if($margin)
$pagination .= "margin: $margin;";
if($padding)
$pagination .= "padding: $padding;";
$pagination .= "\"";
}
$pagination .= ">";
//previous button
if ($page > 1)
$pagination .= "<a href=\"$targetpage$pagestring$prev\">« prev</a>";
else
$pagination .= "<span class=\"disabled\">« prev</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
}
}
elseif($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 3))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
}
$pagination .= "<span class=\"elipses\">...</span>";
$pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
$pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
$pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
$pagination .= "<span class=\"elipses\">...</span>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
}
$pagination .= "...";
$pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
$pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
$pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
$pagination .= "<span class=\"elipses\">...</span>";
for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination .= "<a href=\"" . $targetpage . $pagestring . $next . "\">next »</a>";
else
$pagination .= "<span class=\"disabled\">next »</span>";
$pagination .= "</div>\n";
}
return $pagination;
}
kemudian untuk buat paginginclude_once 'digg.php';
echo getPaginationString($currentPage, $totalData, $limitPaging, "?p=4&kataKunci={$kataKunci}", "&page=");