Reputation: 187
The pagination works well when the results are unfiltered. But once you check something in the check box then go to page 2 for example, the query will change back to the original and redirects to the page with the unfiltered query.
Here is my code:
<?php
require("functions.php");
require_once './class.PaginationLinks.php';
$dbconn = dbconn();
$printTable = true;
$page = 1;
if(isset($_POST["submit"])){
if(isset($_POST["Kitchen"])){
$arguments1[] = "Kitchen";
}
if (isset($_POST["Common_CR"])) {
$arguments1[] = "Common CR";
}
if (isset($_POST["CR_per_room"])) {
$arguments1[] = "CR per room";
}
if (isset($_POST["WiFi"])) {
$arguments1[] = "WiFi";
}
if (isset($_POST["Lobby"])) {
$arguments1[] = "Lobby";
}
if (isset($_POST["Laundry_Area"])) {
$arguments1[] = "Laundry Area ";
}
if (isset($_POST["Fire_Extinguisher"])) {
$arguments1[] = "Fire Extinguisher";
}
if (isset($_POST["Water_Pump"])) {
$arguments1[] = "Water Pump";
}
if (isset($_POST["Dirty_Kitchen"])) {
$arguments1[] = "Dirty Kitchen";
}
if (isset($_POST["Television"])) {
$arguments1[] = "Television";
}
if (isset($_POST["Emergency_Lights"])) {
$arguments1[] = "Emergency Lights";
}
if (isset($_POST["Canteen"])) {
$arguments1[] = "Canteen";
}
if (isset($_POST["Water_Dispenser"])) {
$arguments1[] = "Water Cooler";
}
if (isset($_POST["Rooftop_Gazebo"])) {
$arguments1[] = "Rooftop Gazebo";
}
if(!empty($_POST['loc'])){
$selectedRadio = $_POST['loc'];
if($selectedRadio == "dorm"){
$area = "dorm.location = 'dormArea'";
}
elseif($selectedRadio=="banwa"){
$area = "dorm.location = 'banwa'";
}
else{}
}
if(!empty($arguments1) && empty($selectedRadio)) {
$size = count($arguments1);
$query = "SELECT dorm.DormId, dorm.DormName, CONCAT(address.StreetName,', ', address.Barangay),owner.Name, dorm.HousingType, dorm.thumbnailpic
FROM dorm, address,owner
WHERE dorm.AddressId = address.AddressId
AND dorm.OwnerId = owner.OwnerId
AND dorm.DormId IN (SELECT all_facilities.DormId
FROM all_facilities
WHERE all_facilities.facilityName IN ('".implode("','",$arguments1)."')
GROUP BY all_facilities.DormId
HAVING COUNT(all_facilities.facilityNo)>= $size)";
unset($arguments1);
}
elseif (!empty($arguments1) && !empty($selectedRadio)) {
echo "both checkbox and radio are not empty";
$size = count($arguments1);
$query = "SELECT dorm.DormId, dorm.DormName, CONCAT(address.StreetName,', ', address.Barangay),owner.Name, dorm.HousingType, dorm.thumbnailpic
FROM dorm, address,owner
WHERE dorm.AddressId = address.AddressId
AND dorm.OwnerId = owner.OwnerId
AND $area
AND dorm.DormId IN (SELECT all_facilities.DormId
FROM all_facilities
WHERE all_facilities.facilityName IN ('".implode("','",$arguments1)."')
GROUP BY all_facilities.DormId
HAVING COUNT(all_facilities.facilityNo)>= $size)";
$selectedRadio='';
unset($arguments1);
}
elseif(empty($arguments1) && !empty($selectedRadio)){
echo "empty checkbox but RADIO IS ON!";
$query = "SELECT dorm.DormId,dorm.DormName, CONCAT(address.streetName,', ',address.Barangay), owner.Name, dorm.HousingType, dorm.thumbnailpic
FROM dorm, address, owner
WHERE dorm.AddressId = address.AddressId AND dorm.OwnerId = owner.OwnerId AND $area";
$selectedRadio='';
}
else{
echo "both empty";
$query = "SELECT dorm.DormID, dorm.DormName, CONCAT(address.streetName,', ',address.Barangay), owner.Name, dorm.HousingType, dorm.thumbnailpic
FROM dorm, address, owner
WHERE dorm.AddressId = address.AddressId AND dorm.OwnerId = owner.OwnerId";
}
}
else{
$query = "SELECT dorm.DormId,dorm.DormName, CONCAT(address.streetName,', ',address.Barangay), owner.Name, dorm.HousingType, dorm.thumbnailpic
FROM dorm, address, owner
WHERE dorm.AddressId = address.AddressId AND dorm.OwnerId = owner.OwnerId";
}
if(isset($_POST['find'])){
$key = $_POST['keyword'];
$query = "SELECT dorm.DormId,dorm.DormName, CONCAT(address.streetName,', ',address.Barangay), owner.Name, dorm.HousingType, dorm.thumbnailpic
FROM dorm, address, owner
WHERE dorm.AddressId = address.AddressId AND dorm.OwnerId = owner.OwnerId AND dorm.DormId in (SELECT dorm.DormId
FROM dorm
WHERE dorm.DormName LIKE '%$key%') ";
}
$result = mysqli_query($dbconn,$query);
if(mysqli_num_rows($result)==0){
$printTable = false;
}
?>
<!DOCTYPE html>
<html>
<head> <title>DorMe</title> </head>
<style type="text/css">
#pagination > li{
display: inline-block;
}
</style>
<body>
<form method="post">
<input type="text" name="keyword">
<input type="submit" name="find" value="SEARCH">
</form>
<div id="header">
<h1>Welcome to DorMe!</h1>
</div>
<?php
$start = 0;
$lim = 4;
if(isset($_GET['page'])){
$page = $_GET['page'];
$start = ($page-1) * $lim;
}
else{
$page = 1;
}
$countQuery = mysqli_affected_rows($dbconn);
$countQuery = ceil($countQuery/$lim);
$query = $query . " LIMIT $start, $lim";
$res = mysqli_query($dbconn, $query);
?>
<div id="content">
<div id="filter">
<form method="post">
<fieldset>
<legend>Filter:</legend>
<input type="checkbox" name="Kitchen">Kitchen
<input type="checkbox" name="Common_CR">Common CR
<input type="checkbox" name="CR_per_room">CR per room
<input type="checkbox" name="WiFi">WiFi
<input type="checkbox" name="Lobby">Lobby
<input type="checkbox" name="Laundry_Area">Laundry Area
<input type="checkbox" name="Fire_Extinguisher">Fire Extinguisher
<input type="checkbox" name="Water_Pump">Water Pump
<input type="checkbox" name="Dirty_Kitchen">Dirty Kitchen
<input type="checkbox" name="Television">Television
<input type="checkbox" name="Emergency_Lights">Emergency Lights
<input type="checkbox" name="Canteen">Canteen
<input type="checkbox" name="Water_Dispenser">Water Dispenser
<input type="checkbox" name="Rooftop_Gazebo">Rooftop Gazebo
<input type="radio" name="loc" value="dorm">Dorm Area
<input type="radio" name="loc" value="banwa"> Banwa
<input type="submit" name="submit" value="Filter">
</fieldset>
</form>
</div>
<div id="table">
<?php
if(!$printTable){
?>
<p>No results in database found!</p>
<?php
}else{
?>
<table border="1">
<thead>
<th>Picture</th>
<th>Information</th>
</thead>
<?php
while(list($DormId, $estName, $address,$owner,$housingType, $thumbnailpic)=mysqli_fetch_row($res)){
?>
<tr>
<td rowspan="4"><img src="<?=$thumbnailpic?>" style="max-width: 50%; max-height: 50%;"></td>
<td><label>Establishment Name: </label><a href="viewdorm.php?ID=<?=$dormid?>"><?=$estName?></a></td>
</tr>
<tr>
<td><label>Address: </label><?=$address?></td>
</tr>
<tr>
<td><label>Owner: </label><?=$owner?></td>
</tr>
<tr>
<td><label>Housing Type: </label><?=determine($housingType)?></td>
</tr>
<?php
}
} ?>
</table>
</div>
<!-- PAGINATION QUERY -->
<ul id = "pagination">
<?php
// if($filt != 1){
if($countQuery > 1){
if($page>1){ ?>
<li><a href="?page=<?php echo ($page-1)?>">«</a></li>
<?php }
for($x = 1; $x <= $countQuery; $x++){
if($x == $page){ ?>
<li><a class="current" href="?page=<?php echo $x?>"><?=$x?></a></li>
<?php
}
else{ ?>
<li><a href="?page=<?php echo $x?>"><?=$x?></a></li>
<?php }
}
if($page!=$countQuery){ ?>
<li><a href="?page=<?php echo ($page+1)?>">»</a></li>
<?php }
} elseif ($countQuery < 1) {
?>
<p> No match found! </p>
<?php } ?>
</ul>
</div>
<?php
mysqli_close($dbconn);
?>
</body>
</html>
Upvotes: 0
Views: 57
Reputation: 16963
... once you check something in the check box then go to page 2 for example, the query will change back to the original and redirects to the page with the unfiltered query.
The problem is because of the post
method. See here,
<form method="post">
^^^^ <== see the post method here
<fieldset>
<legend>Filter:</legend>
<input type="checkbox" name="Kitchen">Kitchen
...
</fieldset>
</form>
When you hit the pagination link and goes to page 2, the $_POST
data will not be retained. Better, use GET
method for your form, like this:
<form method="get">
...
</form>
Subsequently, instead of $_POST
, you have to process the form elements using $_GET
superglobal, like this:
if(isset($_GET["submit"])){
if(isset($_GET["Kitchen"])){
$arguments1[] = "Kitchen";
}
if (isset($_GET["Common_CR"])) {
$arguments1[] = "Common CR";
}
...
}
And finally, you have to use the query part of the URL to correctly display the pagination links. So your pagination-links code should be like this:
<ul id = "pagination">
<?php
parse_str($_SERVER["QUERY_STRING"], $url_array);
unset($url_array['page']);
$url = http_build_query($url_array);
// if($filt != 1){
if($countQuery > 1){
if($page > 1){ ?>
<li><a href="?page=<?php echo ($page-1); ?><?php echo isset($url) && !empty($url) ? "&" . $url : ""; ?>">«</a></li>
<?php
}
for($x = 1; $x <= $countQuery; $x++){
if($x == $page){ ?>
<li><a class="current" href="?page=<?php echo $x; ?><?php echo isset($url) && !empty($url) ? "&" . $url : ""; ?>"><?=$x?></a></li>
<?php
}else{ ?>
<li><a href="?page=<?php echo $x; ?><?php echo isset($url) && !empty($url) ? "&" . $url : ""; ?>"><?=$x?></a></li>
<?php
}
}
if($page!=$countQuery){ ?>
<li><a href="?page=<?php echo ($page+1); ?><?php echo isset($url) && !empty($url) ? "&" . $url : ""; ?>">»</a></li>
<?php
}
} elseif ($countQuery < 1) { ?>
<p> No match found! </p>
<?php
}
?>
</ul>
Upvotes: 1