Menggunakan paging dalam search

Rintaka Jayadi Brata · Dec 5, 2012
Permisi para suhu2 PHP..
Mau nyari pencerahan nih..
Saya sudah melakukan pagination dalam tabel search saya..
Tapi kenapa pada saat saya tekan tombol next, data berikutnya kosong..
Padahal masih ada 1 page lagi..

Saya sertakan potongan scriptnya..
$dataPerPage= 20 ;

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

$offset=($noPage - 1)* $dataPerPage;
$where = '';
If(isset($_POST['search'])){

	$tgl_awal= $_POST['tgl_awal'];
	$tgl_akhir= $_POST['tgl_akhir'];
		
	$sort = $_POST['ord'];
	
	$nama = $_POST['keyw'];
	
	$where .= " WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND nama LIKE '%$nama%' ORDER BY log_absen $sort LIMIT $offset, $dataPerPage";
	
	$query = "SELECT * FROM absen" .$where;
	
	$result = mysql_query($query) or die(mysql_error());}
elseif (isset($_POST['searchall'])){
	$query = "SELECT * FROM absen ORDER BY log_absen DESC LIMIT $offset, $dataPerPage";
	$result = mysql_query($query) or die(mysql_error());}
else {
	$where .= " WHERE tanggal = CURDATE() ORDER BY log_absen DESC  LIMIT $offset, $dataPerPage";
	$query = "SELECT * FROM absen" .$where;
	$result = mysql_query($query) or die(mysql_error());}
	
	

/*$isiabsen= mysql_query("SELECT * FROM absen ORDER BY log_absen DESC");*/

echo "<h1>Hasil pencarian</h1>";
echo "<table align='center' border='1' cellpadding='5' cellspacing='0' align='center'>
	<tr>";
		echo "<th><font size='2' face='Arial'>Log</font></th>";
		echo "<th><font size='2' face='Arial'>Name</font></th>";
		echo "<th><font size='2' face='Arial'>Location</font></th>";
        echo "<th><font size='2' face='Arial'>Date</font></th>";
		echo "<th><font size='2' face='Arial'>Time</font></th>";
	echo "</tr>";
while($row1= mysql_fetch_array($result))
	{
	
	echo "<tr>";
		echo "<td><font size='2' face='Arial'>" . $row1['log_absen'] . "</font></td>";
		echo "<td><font size='2' face='Arial'>" . $row1['nama'] . "</font></td>";
		echo "<td><font size='2' face='Arial'>" . $row1['lokasi'] . "</font></td>";
		echo "<td><font size='2' face='Arial'>" . $row1['tanggal'] . "</font></td>";
		echo "<td><font size='2' face='Arial'>" . $row1['waktu'] . "</font></td>";
		echo "</tr>";}
	echo "</table>";

$qp="SELECT Count(*)AS jumData FROM absen" .$where;
$hasil= mysql_query($qp);
$data = mysql_fetch_array($hasil);

$jumData = $data['jumData'];

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

if($noPage>1) 
	echo "<font align='center' size='2' face='Arial'><a href='".$_SERVER['PHP_SELF']."?page=".($noPage-1)."'>&lt;&lt; Prev</a></font>";

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

Mohon pencerahannya..
Terima kasih..
Silahkan login untuk menjawab!
0
Loading...
Ellyx Christian · Dec 6, 2012 · 0 Suka · 0 Tidak Suka
pertama kalau ingin hasil search bisa dipaging maka form search harus menggunakan method get.
$dataPerPage= 20 ;
 
if(isset($_GET['page'])){
	$noPage = $_GET['page'];}
else
	$noPage = 1;
 
$offset=($noPage - 1)* $dataPerPage;
$where = '';
If(isset($_GET['search'])){
 
	$tgl_awal= $_GET['tgl_awal'];
	$tgl_akhir= $_GET['tgl_akhir'];
 
	$sort = $_GET['ord'];
 
	$nama = $_GET['keyw'];
 
	$where .= " WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND nama LIKE '%$nama%' ORDER BY log_absen $sort LIMIT $offset, $dataPerPage";
 
	$query = "SELECT * FROM absen" .$where;
 
	$result = mysql_query($query) or die(mysql_error());}
elseif (isset($_POST['searchall'])){
	$query = "SELECT * FROM absen ORDER BY log_absen DESC LIMIT $offset, $dataPerPage";
	$result = mysql_query($query) or die(mysql_error());}
else {
	$where .= " WHERE tanggal = CURDATE() ORDER BY log_absen DESC  LIMIT $offset, $dataPerPage";
	$query = "SELECT * FROM absen" .$where;
	$result = mysql_query($query) or die(mysql_error());}
 
 
 
/*$isiabsen= mysql_query("SELECT * FROM absen ORDER BY log_absen DESC");*/
 
echo "<h1>Hasil pencarian</h1>";
echo "<table align='center' border='1' cellpadding='5' cellspacing='0' align='center'>
	<tr>";
		echo "<th><font size='2' face='Arial'>Log</font></th>";
		echo "<th><font size='2' face='Arial'>Name</font></th>";
		echo "<th><font size='2' face='Arial'>Location</font></th>";
        echo "<th><font size='2' face='Arial'>Date</font></th>";
		echo "<th><font size='2' face='Arial'>Time</font></th>";
	echo "</tr>";
while($row1= mysql_fetch_array($result))
	{
 
	echo "<tr>";
		echo "<td><font size='2' face='Arial'>" . $row1['log_absen'] . "</font></td>";
		echo "<td><font size='2' face='Arial'>" . $row1['nama'] . "</font></td>";
		echo "<td><font size='2' face='Arial'>" . $row1['lokasi'] . "</font></td>";
		echo "<td><font size='2' face='Arial'>" . $row1['tanggal'] . "</font></td>";
		echo "<td><font size='2' face='Arial'>" . $row1['waktu'] . "</font></td>";
		echo "</tr>";}
	echo "</table>";
 
$qp="SELECT Count(*)AS jumData FROM absen" .$where;
$hasil= mysql_query($qp);
$data = mysql_fetch_array($hasil);
 
$jumData = $data['jumData'];
 
$jumPage = ceil($jumData/$dataPerPage);

//bentuk url paramater menggunakan informasi dari $_GET
$params = $_GET;
unset($params['page']);
$urlParam = $params ? '&'.http_build_query($params) : '';
 
if($noPage>1) 
	echo "<font align='center' size='2' face='Arial'><a href='".$_SERVER['PHP_SELF']."?page=".($noPage-1).$urlParam."'>&lt;&lt; Prev</a></font>";
 
for($page = 1; $page <=$jumPage; $page++){
	if((($page>=$noPage - 3) && ($page <= $noPage + 3)) || ($page == 1) || ($page == $jumPage)){
		$showPage = $page;
		if(($showPage == 1) && ($page !=2)) echo " ";
		if(($showPage !=($jumPage - 1))&&($page==$jumPage)) echo " ";
		if ($page == $noPage) echo"<font align='center' size='2' face='Arial'>".$page."</font>";
			else echo"<font align='center' size='2' face='Arial'><a href='".$_SERVER['PHP_SELF']."?page=".$page.$urlParam."'>".$page."</a></font>";
		$showPage = $page;
	}
}
if ($noPage < $jumPage){
	echo "<font size='2' face='Arial'><a href='".$_SERVER['PHP_SELF']."?page=".($noPage+1).$urlParam."'> Next &gt;&gt;</a></font>";}
?>
dan ingat ubah method form dari method="post" menjadi method="get"
0
Loading...
Rintaka Jayadi Brata · Dec 7, 2012 · 0 Suka · 0 Tidak Suka
Wah super sekali mas..
Paging page dua langsung bisa..

Tapi pas code diatas diaplikasikan ada beberapa fitur yang g bisa..
Search All tidak bisa, keluarnya jadi data hari ini.
Pagination page ke 3 dst g bisa cuma sampe 2 aja..
Hmm..

Apa yang kurang y?

Soalnya udah googling tentang gimana caranya nyangkutin isset ke page berikutnya g nemu..

Terima kasih sebelumnya..
0
Loading...
Ellyx Christian · Dec 10, 2012 · 0 Suka · 0 Tidak Suka
ganti

elseif (isset($_POST)){

dengan

elseif (isset($_GET)){
0
Loading...
Rintaka Jayadi Brata · Dec 10, 2012 · 0 Suka · 0 Tidak Suka
Mantab sekali suhu..
Tapi yang paging itu masih sama seperti kemaren..
Pagenya hanya sampai 2.. Tidak ada link untuk lanjut ke page 3..
Di bagian paging page 1 normal 1 2 3 NEXT>>, pada saat page 2 jadi <<PREV
Saya echo $jumPage yang page 1, jumPage bernilai 3 sedangkan di page 2 ada 0..

Mohon Pencerahannya suhu..
Terima kasih..
0
Loading...
Rintaka Jayadi Brata · Dec 13, 2012 · 0 Suka · 0 Tidak Suka
Ternyata ada yang salah dalam script untuk menghitung page-nya..

Dalam Select count, saya memakai limit..
Itu yang bikin script pada page 2 dan seterusnya tidak keluar dalam link paging..

Terima kasih untuk bantuannya..