Hi,
I'm on the final stretch of getting my site ready before launch, but have a couple more amendments I would like to make. I think I may have asked you this by e-mail but cannot find it for the life of me, so I may have dreamt it.
The first one is that I would really like to be able to filter search results by category. A user may type 'Fifa' into the search and be met with pages of results. To get them to what they want quicker, I would like to be able to filter search results by category, much like the big websites (kelkoo etc) do.
In my above example, Fifa would match products from the following categories: Nintendo DS Games, Nintendo Wii Games, Playstation 3 Games etc. If the user wants to find Fifa on the Nintendo Wii, they should be able to click on the category and it shows only products from that category that match the searh query.
Additionally the list of categories created should only be categories that the query.
Many thanks,
Craig.
Hi David,
I've been trying this out but with not much luck so far.
The dropdown appears on the search results page but it has all the categories regardless of the search terms entered, also no matter which category I choose from the filter, the results don't change!
Regards
Hayden
Hi Hayden,
What you could do is add the WHERE clause from the search to the SQL to restrict the category list to those with products returned by the query. Try using:
<?php
if ($parts[0] <> "category")
{
$sql = "SELECT DISTINCT category FROM `".$config_databaseTablePrefix."products` WHERE ".$where;
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$categories[$row["category"]] = $row["category"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("categoryFilter",$categories,$_GET["categoryFilter"],"");
print "<input type='hidden' name='q' value='".urlencode($q)."' />";
print "<input type='submit' value='Filter By Category' />";
print "</form>";
}
?>
If selecting a category is making no difference, check that the categoryFilter has been implemented correctly as described in the following thread (the above code won't have any effect on its own)
http://www.pricetapestry.com/node/212
If it looks OK, if you could email me your modified search.php i'll check it out for you!
Cheers,
David.
Hi David,
I've tried the above code and i'm getting the following error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/namedcom/public_html/xxmysitexx.co.uk/includes/database.php on line 32
Any ideas?
Regards
Hayden
Hi David,
Sorry I missed a bit of the warning:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/namedcom/public_html/xxmysitexx.co.uk/includes/database.php on line 27
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/namedcom/public_html/xxmysitexx.co.uk/includes/database.php on line 32
Regards
Hayden
Hi Hayden,
Could you email me your search.php and I'll check it out for you - i'll make sure that $where is in scope at the point at which you are creating the drop-down...
Cheers,
David.
Hi David,
I gave it a shot, the drop down box appears, but it doesn't actually filter anything.
Additionally the box contained ALL categories not just ones that matched search terms. (e.g. a search for 'mario' showed up all categories, but it should only have been Wii and DS).
I hope you don't mind, but I've e-mailed my search.php to you to have a look at.
Kind Regards,
Craig
Hi,
Is it possible to use this modification combined with the brand filter dropdown? So you would have the option to filter on brand and category? I'm already using a brand filter.
Thanks,
Pieter.
Hi Pieter,
It should be, if you could email me your search.php so I can see how you are doing the brand filtering I'll take a look...
Cheers,
David.
Hi David,
Works fine now. Just one more question. Is it also possible to fill the dropdown with all categories and brands within the complete search results? Now it only fills the dropdown with the search results from the current page.
If the search results cover 4 pages for instance, I would like to see the categories and brands for the other pages as well.
Here is an example (BETA)
I would like to display the categories from the next 12 pages in the dropdown as well.
Kind regards,
Pieter.
Hello Pieter,
I spotted a mistake in the last version of search.php that I sent to you (it was using the wrong variable name - $serchWhere instead of $searchWhere but I also want to check that it is not using the LIMIT clause..
Could you perhaps email me again your very latest version of search.php is running on the site in the link and I'll check it out for you...
Cheers,
David.
Hi David,
Hope you dont mind me adding to this thread, but is it possible to sort the categories within the drop down box so they are in Alphabetical order.
At the moment it appears to be totally random and the dropdown box could look like this:
Sony PSP games
PC Games
Nintendo Wii Games
Sony PS3 Games
Nintendo Wii Accessories.
I feel if they were in alphabetical order it would be easier for the user to find what they were after.
Many Thanks,
Craig.
Hello Craig,
Very small change - in place of:
$sql = "SELECT DISTINCT category FROM `".$config_databaseTablePrefix."products` WHERE ".$where;
...use:
$sql = "SELECT DISTINCT category FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY category";
Hope this helps!
Cheers,
David.
Hi David,
I'm struggling to get this mod working and wondered if you could shed any light on where I've gone wrong?
When I try to filter by category the url it tries to send me to is all back to front eg:
search.php?categoryFilter=blah&q=brand%253Ablahblah%253A
Cheers,
Chris.
Hi Craig,
Sure - you could implement a categoryFilter in exactly the same way as the brandFilter described in the following thread:
http://www.pricetapestry.com/node/212
However, in the section describing the changes to html/banner.php, in place of the described code use the following modification to show the category filter drop-down on all pages except category search results:
<?php
if ($parts[0] <> "category")
{
$sql = "SELECT DISTINCT category FROM `".$config_databaseTablePrefix."products`";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$categories[$row["category"]] = $row["category"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("categoryFilter",$categories,$_GET["categoryFilter"],"");
print "<input type='hidden' name='q' value='".htmlentities($q,ENT_QUOTES,$config_charset)."' />";
print "<input type='submit' value='Filter By Category' />";
print "</form>";
}
?>
Cheers,
David.