Jump to content

Recommended Posts

Hello,

 

I have been looking all over the internet for example on how to filter a query using multiple check boxes. To best explain what I need I believe showing you an example is the best way to go about it.  http://www.whirlpool.com/Kitchen-1/Kitchen_Refrigeration_Refrigerators-3/102110023+102110368/ If you click on the link you will notice on the left side of the page there is a filter results section that allows you to pick and choose what you want.

 

I want to learn how to filter my query using multiple checkboxes

 

I will post my code below and would appreciate any help.

<!doctype html>
<html lang="en">
 <head>
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <link href="http://fonts.googleapis.com/css?family=Bree+Serif" rel="stylesheet" type="text/css">
  <link href="http://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet" type="text/css">
  <link href='http://fonts.googleapis.com/css?family=Lusitana' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <title>Quailty Appliances</title>
 </head>
 <body>
  <?php 
	include 'config/header.php';

	// connect to database
	include 'config/config.php';
	include 'config/opendb.php';

	$inventory__cat = 4; // by default category is set to 1 (Dishwashers)
	$pageNum        = 1; // by default we show first page
	$ErrMessage     = "False";

	// if $_GET['cat'] defined, use it as set category
	if(isset($_GET['cat'])){
		$inventory__cat = clean_input($_GET["cat"]);
	}

	// if $_GET['page'] defined, use it as page number
	if(isset($_GET['page'])){
		$pageNum = $_GET['page'];
	}

	$targetpage = "inventory.php"; 	
	$limit = 6; 
	
	$query  =  mysqli_query($conn,"SELECT COUNT(*) as num FROM products WHERE cat_id = '$inventory__cat'");
	$total_pages = mysqli_fetch_array($query);
	$total_pages = $total_pages['num'];
	
	$stages = 3;
	$page = mysqli_escape_string($conn, $pageNum);
	if($page){
		$start = ($page - 1) * $limit; 
	}else{
		$start = 0;	
		}	
	
    // Get page data
	$result = mysqli_query($conn,"SELECT * FROM products WHERE cat_id = '$inventory__cat' LIMIT $start, $limit");
	$category_exist = mysqli_num_rows($result);
	
	// checking to see if query returns a result.
	if($category_exist == 0){
		$ErrMessage = "True"; // display message if no results are returned.
	}
	
	// Initial page num setup
	if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;					
	
	
	$paginate = '';
	if($lastpage > 1)
	{	
		$paginate .= "<div class='paginate'>";
		// Previous
		if ($page > 1){
			$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=$prev'>previous</a>";
		}else{
			$paginate.= "<span class='disabled'>previous</span>";	}
			

		
		// Pages	
		if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=$counter'>$counter</a>";}					
			}
		}
		elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($page < 1 + ($stages * 2))		
			{
				for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=$lastpage'>$lastpage</a>";		
			}
			// Middle hide some front and some back
			elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
			{
				$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=1'>1</a>";
				$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=$lastpage'>$lastpage</a>";		
			}
			// End only hide early pages
			else
			{
				$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=1'>1</a>";
				$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=$counter'>$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($page < $counter - 1){ 
			$paginate.= "<a href='$targetpage?cat=$inventory__cat&page=$next'>next</a>";
		}else{
			$paginate.= "<span class='disabled'>next</span>";
			}
			
		$paginate.= "</div>";		
	
	
}

    // get category name
    $cat = mysqli_query($conn,"SELECT cat_name FROM categories WHERE cat_id = '$inventory__cat'");
    while ($rowCAT = mysqli_fetch_array($cat)){
		$inv_cat = $rowCAT['cat_name'];

		if ($inv_cat =="Stoves_Ranges"){ 
			$inv_cat = "Stoves & Ranges"; 
		}
		if ($inv_cat =="Washers_Dryers"){ 
			$inv_cat = "Washers & Dryers"; 
		} 
	}

	// function to clean $_GET.
	function clean_input($data){
		$data = trim($data);
		$data = stripslashes($data);
		$data = htmlspecialchars($data);
		return $data;
	}
  ?>
  <!-- Main Body Table START -->
	 <table class="BodyTable">
	  <tr>
	   <td>
	    <table width="100%" cellspacing="0" cellpadding="0" border="0">
	     <tr>
	      <td colspan="2"><h1><?php echo "$inv_cat";?></h1></td>
	     </tr>
	     <tr>
	      <td colspan="2">
		   <?php
	        if($ErrMessage == "False"){
				echo "<table width='100%' cellspacing='0' cellpadding='0' border='0'>
	                   <tr>
		                <td align='right'>$paginate</td>
		                <td align='right' width='80px' style='color:#696868;'>$total_pages Results</td>
		               </tr>
	                  </table>";
	        }
	       ?>
		  </td>
	     </tr>
	     <tr>
	      <td width="205" valign="top">
		   <form method="post" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
		   <input type="hidden" value="4" name="cat" />
		   <div id="filterContent">
		    <div id="filterContentTitle">Filter Results</div>

		    <div id="filterContentResults">
		     <div id="filterContentCat">Categories</div>
		     <?php
		     $query_cat = mysqli_query($conn,"SELECT * FROM categories ORDER BY cat_name ASC");
             while ($rowcat = mysqli_fetch_array($query_cat)){
				 echo "<input type='checkbox' name='categories' value='{$rowcat['cat_id']}'"; ?><?php if (isset($manufacturer) && $manufacturer=="Fratelli") echo "checked";?> <?php echo" /><label for='categories'>{$rowcat['cat_name']}</label><br />";
			 }
		     ?>
			</div>
			<hr />
			<div id="filterContentResults">
		     <div id="filterContentCat">Brands</div>
		     <?php
		     $query_brand = mysqli_query($conn,"SELECT DISTINCT _manufacturer FROM products WHERE cat_id = '$inventory__cat'");
             while ($rowbrand = mysqli_fetch_array($query_brand)){
				 echo "<input type='checkbox' name='brand[]' value='{$rowbrand['_manufacturer']}' onclick='this.form.submit()'; /><label for='brand'>{$rowbrand['_manufacturer']}</label><br />";
			 }
		     ?>
		    </div>
			<hr />
		    <div id="filterContentResults">
		     <div id="filterContentCat"><?php echo "$inv_cat";?> Type</div>
		     <?php
		     $query_type = mysqli_query($conn,"SELECT DISTINCT sub_cat FROM products WHERE cat_id = '$inventory__cat'");
             while ($rowtype = mysqli_fetch_array($query_type)){
				 echo "<input type='checkbox' name='subCategory' value='{$rowtype['sub_cat']}'><label for='subCategory'>{$rowtype['sub_cat']}</label><br />";
			 }
		     ?>
		    </div>
			<hr />
			<div id="filterContentResults">
		     <div id="filterContentCat"><?php echo "$inv_cat";?> Capacity</div>
		     <?php
		     $query_cubic = mysqli_query($conn,"SELECT DISTINCT cubic_feet FROM products WHERE cat_id = '$inventory__cat'");
             while ($rowcolor = mysqli_fetch_array($query_cubic)){
				 echo "<input type='checkbox' name='capacity' value='{$rowcolor['cubic_feet']}'><label for='capacity'>{$rowcolor['cubic_feet']}</label><br />";
			 }
		     ?>
			</div>
			<hr />
			<div id="filterContentResults">
		     <div id="filterContentCat">Condition</div>
		     <?php
		     $query_cond = mysqli_query($conn,"SELECT DISTINCT _Pcondition FROM products WHERE cat_id = '$inventory__cat'");
             while ($rowcond = mysqli_fetch_array($query_cond)){
				 echo "<input type='checkbox' name='condition' value='{$rowcond['_Pcondition']}'><label for='condition'>{$rowcond['_Pcondition']}</label><br />";
			 }
		     ?>
			</div>
			<hr />
			<div id="filterContentResults">
		     <div id="filterContentCat">Color</div>
		     <?php
		     $query_color = mysqli_query($conn,"SELECT DISTINCT _color FROM products WHERE cat_id = '$inventory__cat'");
             while ($rowcolor = mysqli_fetch_array($query_color)){
				 echo "<input type='checkbox' name='color' value='{$rowcolor['_color']}'><label for='color'>{$rowcolor['_color']}</label><br />";
			 }
		     ?>
		    </div>
		   </div>
		   </form>
		  </td>
	      <td valign="top">
		  <hr />
		   <table cellspacing="0" cellpadding="0" border="0">
		   <?php
		   
		   if($ErrMessage == "True"){
			   echo "<tr><td><p><span class=\"error2\">No Products Have Been Entered Under This category.</span></p></td></tr>";
		   } else if ($ErrMessage == "False"){
			   
			   // // // display data results
			   while($row = mysqli_fetch_array($result)){
				   $price        = $row['_price'];
				   $value        = preg_replace('/[\$,]/', '', $price);
				   $currentprice = (int)$value;
				   $price        = number_format($currentprice,2);
				   $energy_star  = $row['energy_star'];

				   if($energy_star == "Yes"){
					   $energy_star = "<li>Energy Efficient</li>";
					   $energy_pic  = "<img src='/images/icon_energy_star.jpg' width='39' height='39' class='energy' border='0' title='Energy Star Efficient' />";
				   } else {
					   $energy_star = "";
					   $energy_pic  = "";
				   }

				   echo "<tr>
				          <td width='135px'>
						   <div id='contentImg'>
						    <img src='/images/products/{$row['product_id']}{$row['_thumbnail']}'  class='displayed' style='max-width:225px;max-height:125px;padding-top:4px;' border='0' title='{$row['_title']}' /><br />
						   </div>
						  </td>
						  <td valign='top' width='500px'>
						   <div id='InvenTitlte'><a href='#'>{$row['_title']}</a></div>
						   <div id='ModelNumSm'>Model#: {$row['model_number']}</div>
						   <div id='contentProductFeatures'>
						    <div>
						     <ul>
							  <li>{$row['_color']}</li>
							  $energy_star
							 </ul>
							</div>
							<div>
						     <ul>
							  <li>Capacity: {$row['cubic_feet']}</li>
							  <li>Condition: {$row['_Pcondition']}</li>
							 </ul>
							</div>
							<div>
						     <ul>
							  <li>{$row['_height']} H x {$row['_width']} W x{$row['_depth']} D</li>
							  <li>Weight: {$row['_weight']}</li>
							 </ul>
							</div>
						   </div>
						   $energy_pic
						  </td>
						  <td valign='top' width='83px;' align='right'><div id='contentProductPrice'>$$price</div><div id='contentProductText'>price</div></td>
				         </tr>
						 <tr>
						  <td colspan='3'><hr /></td>
						 </tr>";
			   }
		   }
		   
		   include 'config/close.php';
		   ?>	    
		   </table>
		  </td>	    
	     </tr>
		 <tr>
	      <td colspan="2">
		   <?php
	        if($ErrMessage == "False"){
				echo "<table width='100%' cellspacing='0' cellpadding='0' border='0'>
	                   <tr>
		                <td align='right'>$paginate</td>
		                <td align='right' width='80px'>$total_pages Results</td>
		               </tr>
	                  </table>";
	        }
	       ?>
		  </td>
	     </tr>
	    </table> 
	   </td>
	  </tr>
	 </table>
	<!-- Main Body Table END -->
  <?php include 'config/footer.php'; ?>
 </body>
</html>
Edited by TiwstedNy
Link to comment
https://forums.phpfreaks.com/topic/287434-multiple-checkbox-filter-query-in-php/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.