Is it possible to have a search form, which only do a search in a category?
I explain a little bit, what I want to do:
One search form should be on the startpage and do a search in all categories.
And on all category pages the search form should only do a search for products in this category(correspondent category) only.
Regards,
Harry
yes I know it, but I want to do this without the need of the "q" value in the search form.
The search form value should be blank, and as I want to do a the search always in a category there must be a way to indicate the search form to do searches only in this category.
Regards,
Harry
It would be the best to have a dropdown (search in this category/search on all) next to the search form, which will be displayed on the category pages only.
Hi Harry,
Firstly, a new html/searchresults.php. This will display the category drop-down only for category search results:
html/searchform.php:
<div class='searchform'>
<form name='search' action='<?php print $config_baseHREF ?>search.php'>
<?php if ($parts[0]=="category"): ?>
<input type='text' name='q' size='35' value='<?php print (isset($parts[2])?$parts[2]:""); ?>' />
<?php else: ?>
<input type='text' name='q' size='35' value='<?php print (isset($q)?$q:""); ?>' />
<?php endif; ?>
<input type='submit' value='<?php print translate("Search"); ?>' />
<?php
if ($parts[0]=="category")
{
$sql = "SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` WHERE category <> ''";
if (database_querySelect($sql,$rows))
{
print " inside category ";
print "<select name='category'>";
foreach($rows as $row)
{
$selected = ($row["category"]==$parts[1]?"selected='selected'":"");
print "<option value='".$row["category"]."' ".$selected.">".$row["category"]."</option>";
}
print "</select>";
}
}
?>
</form>
</div>
...and then to handle the new search, in search.php look for the following code on line 4:
$q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\."):"");
...and REPLACE this with the following block of code:
$q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\."):"");
if ($_GET["category"])
{
$q = "category:".$_GET["category"].":".$q;
}
That should do the trick!
Cheers,
David.
very well and near at the solution, but with this mod all categories are shown in the dropdown.
I mean it would be better if the dropdown not displays all the category names, instead it should display only the text:
"search in this category" or "search in all categories"
(only two choices for the user)
Regards,
Harry
Hi Harry,
That can be done - try this html/searchform.php
<div class='searchform'>
<form name='search' action='<?php print $config_baseHREF ?>search.php'>
<?php if ($parts[0]=="category"): ?>
<input type='text' name='q' size='35' value='<?php print (isset($parts[2])?$parts[2]:""); ?>' />
<?php else: ?>
<input type='text' name='q' size='35' value='<?php print (isset($q)?$q:""); ?>' />
<?php endif; ?>
<input type='submit' value='<?php print translate("Search"); ?>' />
<?php
if ($parts[0]=="category")
{
print " inside ";
print "<select name='category'>";
print "<option value='".$parts[1]."'>this category</option>
print "<option value=''>all categories</option>
print "</select>";
}
?>
</form>
</div>
Cheers,
David.
I need the feature to search within a category, but it is not working with my installation.
So i checked the situation at your demo page http://www.webpricecheck.co.uk with
"category:Accessories:Apple"
but i´ve got the same result.
Do i have to set a parameter or modify the php-code?
Best regards
MrChilli
Hi,
I'll email you a modification to use to support this straight away...
Cheers,
David.
Dear David,
thanks a lot for the solution.
I changed the code a little bit to give the availabilty to search with match code...
Works perfekt :-)
best regards
MrChilli
Hello Harry,
Did you know that if you add terms to the search box on a category page it will only search in that category? For example, if you are on the Accessories category page, this should be in the search box:
category:Accessories:
...but then if you type in:
category:Accessories:Widget
...then only products in the Accessories category with "Widget" in the name will be shown. If this is not sufficient, let me know, and I'll help you modify search.php and html/searchform.php to make a search only in the current category. I guess you also want to indicate this to the user somehow, so let me know what you like to be displayed...
Cheers,
David.