Jump to content

Recommended Posts

Hello,

I'm creating an inventory search page and after the primary search a list of categories with a count is displayed which the user can click to refine the search. For this I use a query like this:

 

$refineBrandSQL = mysql_query("SELECT a.*, COUNT(*) AS numbers
                                    FROM inventory a
                                    WHERE item_name LIKE '%$searchTermDB%' OR item_desc LIKE '%$searchTermDB%' OR a.sub_category_b 
                                    LIKE  '%$searchTermDB%'
                                    GROUP BY brand");		
	$refineBrand = $refineBrandSQL or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$refineSQL}") ;
                if (mysql_num_rows($searchResult) > 0) {
                 echo "<div class='headerClass3'>Brand</div>";
	 while ($make = mysql_fetch_array($refineBrand)){
	$brand = "";
	$brand .= ''. $make['brand'] .' ('. $make['numbers'] .')<br />'; 
	echo "<div class='left_pad'>" .$brand. '</div>';
	}
                }

 

This outputs results such as:

Nike(12)

Addidas(7)

 

What I'm looking for now is to create a link perhaps out of these results that will select the counted results. so if the user clicks Nike(12) all the 12 results for Nike will be displayed. are there any ideas on how to accomplish this? Thank you

Link to comment
https://forums.phpfreaks.com/topic/233422-using-data-to-refine-search/
Share on other sites

this should probably be in the php section,

you'll need something like

 

search.php

if (isset($_GET['brand']) {
$brand = mysql_real_escape_string($_GET['brand']);
$whereClauseXtra= "AND brand='$brand'";
} else {
$whereClauseXtra = '';
}

$refineBrandSQL = mysql_query("SELECT a.*, COUNT(*) AS numbers
                                    FROM inventory a
                                    WHERE item_name LIKE '%$searchTermDB%' OR item_desc LIKE '%$searchTermDB%' OR a.sub_category_b $whereClauseXtra
                                    LIKE  '%$searchTermDB%'
                                    GROUP BY brand");		
	$refineBrand = $refineBrandSQL or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$refineSQL}") ;
                if (mysql_num_rows($searchResult) > 0) {
                 echo "<div class='headerClass3'>Brand</div>";
	 while ($make = mysql_fetch_array($refineBrand)){
	$brand = "";
	$brand .= "<a href='search.php?brand={$make['brand']}'>". $make['brand'] .' ('. $make['numbers'] .')</a><br />'; 
	echo "<div class='left_pad'>" .$brand. '</div>';
	}
                }

 

Sorry, I left in your original where condition, and I should've enclosed the OR terms in brackets.

have a play around with it - you should get the general idea now

and echo the search query so you can have a good look at whats executing.

if (isset($_GET['brand']) {
$brand = mysql_real_escape_string($_GET['brand']);
$whereClauseXtra= "AND brand='$brand'";
} else {
$whereClauseXtra = '';
}

$refineBrandSQL = mysql_query("SELECT a.*, COUNT(*) AS numbers
                                    FROM inventory a
                                    WHERE (item_name LIKE '%$searchTermDB%' OR item_desc LIKE '%$searchTermDB%' OR a.sub_category_b LIKE  '%$searchTermDB%')
$whereClauseXtra                         
                                    GROUP BY brand");


//echo query for debug
echo "SELECT a.*, COUNT(*) AS numbers
                                    FROM inventory a
                                    WHERE (item_name LIKE '%$searchTermDB%' OR item_desc LIKE '%$searchTermDB%' OR a.sub_category_b LIKE  '%$searchTermDB%')
$whereClauseXtra                         
                                    GROUP BY brand";









$refineBrand = $refineBrandSQL or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$refineSQL}") ;
                if (mysql_num_rows($searchResult) > 0) {
                 echo "<div class='headerClass3'>Brand</div>";





while ($make = mysql_fetch_array($refineBrand)){





$brand = "";





$brand .= "<a href='search.php?brand={$make['brand']}'>". $make['brand'] .' ('. $make['numbers'] .')</a><br />'; 





echo "<div class='left_pad'>" .$brand. '</div>';





}
                }

Sorry, I left in your original where condition, and I should've enclosed the OR terms in brackets.

have a play around with it - you should get the general idea now

and echo the search query so you can have a good look at whats executing.

 

Thanks Joel, useful info there. I'm one step further along with it now.

Cheers!

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.