Filter by brand
Firstly I'd like to say this is an excellent script.
What I'd like to do is add an additional filter to the category pages. What changes would need to be made for a drop down list to appear at the top of the category pages so when the user is browsing a category they can select a particular brand from a drop down menu so the product list is filtered to only those products in the category from that brand.
Ideally the drop down would only contain a list of brands which had products in that category.
For instance if you were browsing a category say Mens T-shirts - the drop down would contain brands such as Ben Sherman, Hugo Boss, Levis. Selecting a brand such as Ben Sherman would list Ben Sherman T-shirts.
Thanks for your quick response.
That worked well, except for when the results span more than one page. When clicking on page 2 the brandFilter= is lost, so I guess there needs to be a change there?
Yes - the brandFilter parameter must be persisted on the navigation panel; and also (if you wish) on the links to sort by relevance, rating or price.
For the navigation panel; you basically need to copy the way in which the "sort" parameter is added to the construction of the various HREF variables in html/navigation.php
For example, you would change:
$prevHREF = "?q=".urlencode($q)."&page=".$prevPage."&sort=".$sort;to:
$prevHREF = "?q=".urlencode($q)."&page=".$prevPage."&sort=".$sort."&brandFilter=".urlencode($_GET["brandFilter"]);There are 4 instances where this modification is required; on lines 16,62,75,99.
In search.php, you just need to make one change, from:
$sortHREF = $config_baseHREF."search.php?q=".urlencode($q)."&page=1&sort=";to:
$sortHREF = $config_baseHREF."search.php?q=".urlencode($q)."&page=1&brandFilter=".urlencode($_GET["brandFilter"])."&sort=";Let me know if that works as expected...
That worked well, except there was one small problem with $sortHREF
Changed it to
$sortHREF = $config_baseHREF."search.php?q=".urlencode($q)."&page=1&brandFilter=".urlencode($_GET["brandFilter"])."&sort=";I just spotted that! I've fixed it in the above post - thanks!
I'll roll some code for the Brand drop-down on the category page now... check back in a bit for that...
Ok, first thing to do is bring in the widget library (which is only used in the admin area) as it has a nice function for creating a drop-down menu from an array, so add the following line at the top of search.php
require("includes/widget.php");Next step is to get a distinct array of brands matching the current category query. You will know that you are in a category query if $parts[0] == "category", so you can put all the code for this inside an if statement with that test; and the drop down will only be displayed on the appropriate pages.
I would position this to start off with after the banner, and before the search results; so you will want to add this code just after the following line:
require("html/banner.php");Here's the PHP code that should create the drop down:
<?php
if ($parts[0] == "category")
{
$sql = "SELECT DISTINCT brand FROM `".$config_databaseTablePrefix."products` WHERE category = '".database_safe($parts[1])."'";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$brands[$row["brand"]] = $row["brand"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("brandFilter",$brands,$_GET["brandFilter"],"");
print "<input type='hidden' name='q' value='".urlencode($q)."' />";
print "<input type='submit' value='Filter By Brand' />";
print "</form>";
}
?>
Fingers crossed...!
Cheers,
David.
David, thanks very much for the code.
It would not work with the line of code as below
print "<input type='hidden' name='q' value='".urlencode($q)."' />";but when changed to
print "<input type='hidden' name='q' value='".($q)."' />";it did work.
Taking this one small step further. How about an alternative where you would have the brands as text links across the top of the results page such as
Browse by Brand: Brand 1 | Brand 2 | Brand 3 | Brand 4
Great!
> How about an alternative where you would have the brands as text links
In some respects that is easier - just create links to the appropriate search.php URL rather than using a FORM:
<?php
if ($parts[0] == "category")
{
print "Filter by Brand: ";
$sql = "SELECT DISTINCT brand FROM `".$config_databaseTablePrefix."products` WHERE category = '".database_safe($parts[1])."'";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$brandHREF = $config_baseHREF."search.php?q=".urlencode($q)."&brandFilter=".urlencode($row["brand"]);
print "<a href='".$brandHREF."'>".$row["brand"]."</a> |";
}
}
}
?>
That should do the trick; although i'm not sure why urlencode didn't work in the first example - you may have the same problem here...!
Hope this helps,
David.
Excellent. Thanks very much for all your help David.
hi
I have just tried to implement this mod, every thing worked until i tried the paging, it just sent me to the front page. I noticed it was said there was an error,has this been rectifed.
I am trying to get a list of brands on the category page when searched.
Thanks
Brent
Hi Brent,
If you have not already made any modifications to html/navigation.php to include the new parameter on the page URLs then you will need to make the changes shown in the 4th message on the following thread:
http://www.pricetapestry.com/node/636
This will add the brandFilter parameter to the URL in each of the page links, although i'm not sure why you would be getting redirected to the front page. Have you already made changes to navigation.php - and is the navigation still working for a normal search?
Cheers,
David.
Hi
Thanks, just tried it again from the link you gave and works OK.
Brent
Hi david
Used the php above to create a list of brands at the top of the category search pages(works great), is it possible to have them in a-z order. Also how hard would it be to rewrite navigation urls with mod rewirte after using code in
http://www.pricetapestry.com/node/636
Brent
Hi Brent,
To make the list in alphabetical order change the SQL as follows:
$sql = "SELECT DISTINCT brand FROM `".$config_databaseTablePrefix."products` WHERE category = '".database_safe($parts[1])."' ORDER BY brand";With regards rewriting the navigation URLs, do you mean that you want to keep the re-written URL after selecting a brand filter; so you would have a URL like:
http://www.example.com/category/Widgets/3.html?brandFilter=Foo
Cheers,
David.
hi
Yes, its just to make it a bit more search engine friendly.
Brent
Firstly, you can detect if the source URL is rewritten by looking at the $rewrite variable; so in the code that generates the brand links you need to modify the part in the foreach loop something like this:
<?php
foreach($rows as $row)
{
if ($rewrite)
{
$brandHREF = "?brandFilter=".urlencode($row["brand"]);
}
else
{
$brandHREF = $config_baseHREF."search.php?q=".urlencode($q)."&brandFilter=".urlencode($row["brand"]);
}
print "<a href='".$brandHREF."'>".$row["brand"]."</a> |";
}
?>
Secondly, you need to modify the links generated in html/navigation.php to look to see if brandFilter is set, and add the parameter, for example:
<?php
if ($rewrite)
{
$prevHREF = $prevPage.".html";
// next line is the new code
if ($_GET["brandFilter"]) $prevHREF .= "?brandFilter=".urlencode($_GET["brandFilter"]);
}
?>
There are 3 more instances where a similar modification needs to be made; search for $pageOneHREF, $pageHREF and $nextHREF. The mod is almost the same, just use the appropriate variable in place of $prevHREF as used in the example...
Hope this helps!
Cheers,
David.
hi david
I have been using this mod for some time and it works great, however as the number of items have grown so as the list of brand names,and it can look a little over powering for customers, I tried to use the form mod above and it did not work, at present the mod is in user_search_results_before.php and the form one was under html/banner.php ,
I tried both, both gave errors. Would this mod work for the latest version and would there be any other solution that may look less in your face to customers. An example http://www.4utoday.co.uk/category/Womens/, this is not the largest.
I hope this makes sense.
thanks
Brent
Hi Brent,
Can you post the code you've added to user_search_results_before.php and i'll take a look. If you could also post the error messages you get that will help too...
Cheers,
David.
hi
The code was from above
<?php
if ($parts[0] == "category")
{
$sql = "SELECT DISTINCT brand FROM `".$config_databaseTablePrefix."products` WHERE category = '".database_safe($parts[1])."'";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$brands[$row["brand"]] = $row["brand"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("brandFilter",$brands,$_GET["brandFilter"],"");
print "<input type='hidden' name='q' value='".urlencode($q)."' />";
print "<input type='submit' value='Filter By Brand' />";
print "</form>";
}
?>
the error was
Fatal error: Call to undefined function: widget_selectarray() in /home/utodayno/public_html/html/user_searchresults_before.php on line 15
Hi Brent,
I think the only issue that it requires the widget.php include file which is not included by default.
To fix this, at the top of the file you have modified add:
require("includes/widget.php");That should do the trick!
Cheers,
David.
hi david
Done that also put in code to A-Z form comes up but it seems to have a glitch, please see http://www.4utoday.co.uk/category/Cleaning/
The code used is
<?php
require("includes/widget.php");
if ($parts[0] == "category")
{
$sql = "SELECT DISTINCT brand FROM `".$config_databaseTablePrefix."products` WHERE category = '".database_safe($parts[1])."' ORDER BY brand";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$brands[$row["brand"]] = $row["brand"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("brandFilter",$brands,$_GET["brandFilter"],"");
print "<input type='hidden' name='q' value='".urlencode($q)."' />";
print "<input type='submit' value='Filter By Brand' />";
print "</form>";
}
?>
When you try to go to another brand it shows blabk page, it seems to have inserted an extra character.
Thanks
Brent
Hi Brent,
I'm getting "Server not found..." on:
http://www.4utoday.co.uk/category/Cleaning/
Is that the correct URL?
Cheers,
David.
hi
Yes, just tried it at this end and it works OK
Brent
hi david
Its OK found the glitch, it was the same problem someone else had above.
Thanks for the help
Brent
It would not work with the line of code as below
print "<input type='hidden' name='q' value='".urlencode($q)."' />";but when changed to
print "<input type='hidden' name='q' value='".($q)."' />";it did work.
Hi David,
I have just implemented this mod and have added an extra bit of code to correct the navigation when there is more than one page when the filter has been selected.
The change is in html/navigation.php.
Change this...
$pageHREF = "?q=".urlencode($q)."&page=".$i."&sort=".$sort;to this...
if ($_GET["brandFilter"]){
$pageHREF = "?q=".urlencode($q)."&page=".$i."&sort=".$sort."&brandFilter=".urlencode($_GET["brandFilter"]);
} else {
$pageHREF = "?q=".urlencode($q)."&page=".$i."&sort=".$sort;
}Thanks, Simon
Is it also possible to filter by price?
And behind the filter a total count of the products?
THx
HEnk
Hello Henk,
Do you remember implementing the min / max price search mods? That should enable you to filter by price if the changes are incorporated into this site.
I'm not sure what you mean by a count of the total products behind the filter - can you post an example of what you want to display?
I know you're using a heavily modified search.php, so it might help if you could email me your latest version then I can refer to that in my replies...!
Cheers,
David.
i am starting a new one again, so its not very modded.
what i want is a menu like this one:
< € 50 (1)
€ 50 - € 100 (6)
€ 100 - € 200 (109)
€ 200 - € 300 (284)
€ 300 - € 400 (95)
€ 400 - € 500 (65)
€ 500 - € 750 (59)
€ 750 - € 1000 (36)
€ 1000 - € 2000 (60)
€ 2000 - € 3000 (17)
€ 3000 - € 4000 (6)
€ 4000 - € 5000 (5)
€ 5000 > (5)Cheers HEnk
Hi Henk,
Thanks for that. I'm not sure about the practicalities of doing this on a per-search basis. This sort of range + number drop down is generally only done on a static basis against the entire data set - i.e. the ranges are calculated whenever the database is updated.
In it's simplest form it requires as many queries as there are ranges you want to support, and each query has to scan the entire table; but it should be possible to get someway towards the effect you want with a summary query. I'll look into it for you.
Cheers,
David.
Hi i have now a brandfilter and a model ( sub ) filter when you follow the link you see it in action.
is it possible to filter the sub when you click on the brand. (So its only showing the models thats links to the brand)
HEnk
Hello Henk,
Sure - without seeing your code i'm assuming that your model filter is simply based on all products, using a query something like this:
$sql = "SELECT model FROM products ORDER BY model";All you should need to do is test for $_GET["brandFilter"], and if it is set, use this value in the SQL to restrict the model selection to the selected brand, for example:
if ($_GET["brandFilter"])
{
$sql = "SELECT model FROM products WHERE brand='".database_safe($_GET["brandFilter"])."' ORDER BY model";
}
else
{
$sql = "SELECT model FROM products ORDER BY model";
}That should do the trick!
Cheers,
David.
Sry here is my code:
$sql = "SELECT DISTINCT sub FROM `".$config_databaseTablePrefix."products` WHERE category = '".database_safe($parts[1])."' ORDER BY sub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subs[$row["sub"]] = $row["sub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("subFilter",$subs,$_GET["subFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter model' />";
print "</form>";Her is my code for the filter model.
Hi Henk,
The mods should be something like this to filter the model (sub) list based on the brand filter:
if ($_GET["brandFilter"])
{
$sql = "SELECT DISTINCT sub FROM `".$config_databaseTablePrefix."products` WHERE category = '".database_safe($parts[1])."' AND brand='".database_safe($_GET["brandFilter"])."' ORDER BY sub";
}
else
{
$sql = "SELECT DISTINCT sub FROM `".$config_databaseTablePrefix."products` WHERE category = '".database_safe($parts[1])."' ORDER BY sub";
}
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subs[$row["sub"]] = $row["sub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("subFilter",$subs,$_GET["subFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter model' />";
print "</form>";Hope this helps!
Cheers,
David.
Hi Henk,
Do you mean your model (sub) select box?
If so, I can see why it is empty on the merchant page. The query is currently SELECTing based on category; whereas on the merchant page $parts[1] is the merchant name. It should work by changing the code as follows; and this will then work on the category, merchant AND brand pages:
switch($parts[1])
{
case "merchant":
$where = "merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where = "category = '".database_safe($parts[1])."'";
break;
case "brand":
$where = "brand = '".database_safe($parts[1])."'";
break;
}
if (($parts[1] != "brand") && $_GET["brandFilter"])
{
$where .= " AND brand='".database_safe($_GET["brandFilter"])."' ";
}
$sql = "SELECT DISTINCT sub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY sub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subs[$row["sub"]] = $row["sub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("subFilter",$subs,$_GET["subFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter model' />";
print "</form>";Cheers,
David.
Hi i have now an error on this page:
Link this is because its more then 3 letter word
And an error on this page:
it doesnt link forward to the selected filter.
Thx HEnk
Hi Henk,
Sorry - it has to be modified for the pages that aren't merchant, category or brand, and also I used $parts[1] in the switch() it should be $parts[0]...
Also, it looks like you have 2 versions of the code now - can you remove the old one and just have this:
switch($parts[0])
{
case "merchant":
$where = "merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where = "category = '".database_safe($parts[1])."'";
break;
case "brand":
$where = "brand = '".database_safe($parts[1])."'";
break;
default:
$where = "1";
break;
}
if (($parts[1] != "brand") && $_GET["brandFilter"])
{
$where .= " AND brand='".database_safe($_GET["brandFilter"])."' ";
}
$sql = "SELECT DISTINCT sub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY sub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subs[$row["sub"]] = $row["sub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("subFilter",$subs,$_GET["subFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter model' />";
print "</form>";Cheers,
David.
Hello Henk,
I see one little problem - it was selecting empty models (subs), so I have fixed that with the modified version below.
On the search page, it is doing what the code below will do, but because there is no merchant, category or brand selected it is selecting EVERY model in the database, and is therefore quite slow.
The safest thing to do is probably to not do this for normal searches, so use the following code altogether:
if ($parts[1])
{
$where = "sub <> ''";
switch($parts[0])
{
case "merchant":
$where = " AND merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where = " AND category = '".database_safe($parts[1])."'";
break;
case "brand":
$where = " AND brand = '".database_safe($parts[1])."'";
break;
}
if (($parts[1] != "brand") && $_GET["brandFilter"])
{
$where .= " AND brand='".database_safe($_GET["brandFilter"])."' ";
}
$sql = "SELECT DISTINCT sub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY sub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subs[$row["sub"]] = $row["sub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("subFilter",$subs,$_GET["subFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter model' />";
print "</form>";
}That should work better.
Cheers,
David.
Sorry Henk,
I forgot to change the "=" to ".=" after adding the last change.. Try:
if ($parts[1])
{
$where = "sub <> ''";
switch($parts[0])
{
case "merchant":
$where .= " AND merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where .= " AND category = '".database_safe($parts[1])."'";
break;
case "brand":
$where .= " AND brand = '".database_safe($parts[1])."'";
break;
}
if (($parts[1] != "brand") && $_GET["brandFilter"])
{
$where .= " AND brand='".database_safe($_GET["brandFilter"])."' ";
}
$sql = "SELECT DISTINCT sub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY sub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subs[$row["sub"]] = $row["sub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("subFilter",$subs,$_GET["subFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter model' />";
print "</form>";
}Fingers crossed!
Cheers,
David.
Hi Henk,
I'm not quite sure what you mean - do you want another drop-down with categories?
Yes Sry, only now not the whole list of categories whats in the database but filtered.
Thx
HEnk
Hi, almost done the categories.
Only one thing, when click on the link there are three options, filter sub, filter brand, filter brand.
what i want is when you filter sub, the others must change, you have already don it with the brand filter.
link
Here is the code:
<? if ($parts[1])
{
$where = "catsub <> ''";
switch($parts[0])
{
case "merchant":
$where .= " AND merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where .= " AND category = '".database_safe($parts[1])."'";
break;
case "brand":
$where .= " AND brand = '".database_safe($parts[1])."'";
break;
}
if (($parts[1] != "brand") && $_GET["brandFilter"])
{
$where .= " AND brand='".database_safe($_GET["brandFilter"])."' ";
}
$sql = "SELECT DISTINCT catsub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY catsub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subcats[$row["catsub"]] = $row["catsub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("catsubFilter",$subcats,$_GET["catsubFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter Subcategorie' />";
print "</form>";
}
?>
</p>
<p>
<?php
if ($parts[0] == "category")
{
$sql = "SELECT DISTINCT brand FROM `".$config_databaseTablePrefix."products` WHERE category = '".database_safe($parts[1])."'";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$brands[$row["brand"]] = $row["brand"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("brandFilter",$brands,$_GET["brandFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter By Brand' />";
print "</form>";
}
?>
</p>
<p>
<? if ($parts[1])
{
$where = "sub <> ''";
switch($parts[0])
{
case "merchant":
$where .= " AND merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where .= " AND category = '".database_safe($parts[1])."'";
break;
case "brand":
$where .= " AND brand = '".database_safe($parts[1])."'";
break;
}
if (($parts[1] != "brand") && $_GET["brandFilter"])
{
$where .= " AND brand='".database_safe($_GET["brandFilter"])."' ";
}
$sql = "SELECT DISTINCT sub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY sub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subs[$row["sub"]] = $row["sub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("subFilter",$subs,$_GET["subFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter model' />";
print "</form>";
}
?>
Thx HEnk
Hello Henk,
I think i'm with you here. Try something like this - it makes it consistent for all 3 filters:
<p>
<?
if ($parts[1])
{
$where = "";
switch($parts[0])
{
case "merchant":
$where .= " merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where .= " category = '".database_safe($parts[1])."'";
break;
case "brand":
$where .= " brand = '".database_safe($parts[1])."'";
break;
}
if ($_GET["brandFilter"])
{
$where .= " AND brand='".database_safe($_GET["brandFilter"])."' ";
}
if ($_GET["subFilter"])
{
$where .= " AND sub='".database_safe($_GET["subFilter"])."' ";
}
$sql = "SELECT DISTINCT catsub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY catsub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subcats[$row["catsub"]] = $row["catsub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("catsubFilter",$subcats,$_GET["catsubFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter Subcategorie' />";
print "</form>";
}
?>
</p>
<p>
<?php
if ($parts[1])
{
$where = "";
switch($parts[0])
{
case "merchant":
$where .= " merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where .= " category = '".database_safe($parts[1])."'";
break;
case "brand":
$where .= " brand = '".database_safe($parts[1])."'";
break;
}
if ($_GET["catsubFilter"])
{
$where .= " AND catsub='".database_safe($_GET["catsubFilter"])."' ";
}
if ($_GET["subFilter"])
{
$where .= " AND sub='".database_safe($_GET["subFilter"])."' ";
}
$sql = "SELECT DISTINCT brand FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY brand";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$brands[$row["brand"]] = $row["brand"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("brandFilter",$brands,$_GET["brandFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter By Brand' />";
print "</form>";
}
?>
</p>
<p>
<?php
if ($parts[1])
{
$where = "";
switch($parts[0])
{
case "merchant":
$where .= " merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where .= " category = '".database_safe($parts[1])."'";
break;
case "brand":
$where .= " brand = '".database_safe($parts[1])."'";
break;
}
if ($_GET["catsubFilter"])
{
$where .= " AND catsub='".database_safe($_GET["catsubFilter"])."' ";
}
if ($_GET["brandFilter"])
{
$where .= " AND brand='".database_safe($_GET["brandFilter"])."' ";
}
$sql = "SELECT DISTINCT sub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY sub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subs[$row["sub"]] = $row["sub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("subFilter",$subs,$_GET["subFilter"],"");
print "<input type='hidden' name='q' value='".($q)."' />";
print "<input type='submit' value='Filter model' />";
print "</form>";
}
?>Hope this helps!
Cheers,
David.
Almost, thx.
But :) there is a problem with brands who are in differents categories so the filter doesn't work.
Hope you have a sollution for this .
THX
HENK
Hello Henk,
Can you give an example? What should I select in the boxes and then what is wrong on the next page...
Thanks,
David.
Hi, 'differents categories' must be sub
ok select
haardroger -( hairdryer ) filter
then select brand braun filter.
its shows all braun brands products.
Thx HEnk
Hello Henk,
I understand... I think this can be done by adding the other 2 filter values as hidden fields in each form... try this:
<p>
<?
if ($parts[1])
{
$where = "";
switch($parts[0])
{
case "merchant":
$where .= " merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where .= " category = '".database_safe($parts[1])."'";
break;
case "brand":
$where .= " brand = '".database_safe($parts[1])."'";
break;
}
if ($_GET["brandFilter"])
{
$where .= " AND brand='".database_safe($_GET["brandFilter"])."' ";
}
if ($_GET["subFilter"])
{
$where .= " AND sub='".database_safe($_GET["subFilter"])."' ";
}
$sql = "SELECT DISTINCT catsub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY catsub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subcats[$row["catsub"]] = $row["catsub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("catsubFilter",$subcats,$_GET["catsubFilter"],"");
print "<input type='hidden' name='brandFilter' value='".htmlentities($_GET["brandFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='subFilter' value='".htmlentities($_GET["subFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='q' value='".htmlentities($q,ENT_QUOTES,$config_charset)."' />";
print "<input type='submit' value='Filter Subcategorie' />";
print "</form>";
}
?>
</p>
<p>
<?php
if ($parts[1])
{
$where = "";
switch($parts[0])
{
case "merchant":
$where .= " merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where .= " category = '".database_safe($parts[1])."'";
break;
case "brand":
$where .= " brand = '".database_safe($parts[1])."'";
break;
}
if ($_GET["catsubFilter"])
{
$where .= " AND catsub='".database_safe($_GET["catsubFilter"])."' ";
}
if ($_GET["subFilter"])
{
$where .= " AND sub='".database_safe($_GET["subFilter"])."' ";
}
$sql = "SELECT DISTINCT brand FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY brand";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$brands[$row["brand"]] = $row["brand"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("brandFilter",$brands,$_GET["brandFilter"],"");
print "<input type='hidden' name='catsubFilter' value='".htmlentities($_GET["catsubFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='subFilter' value='".htmlentities($_GET["subFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='q' value='".htmlentities($q,ENT_QUOTES,$config_charset)."' />";
print "<input type='submit' value='Filter By Brand' />";
print "</form>";
}
?>
</p>
<p>
<?php
if ($parts[1])
{
$where = "";
switch($parts[0])
{
case "merchant":
$where .= " merchant = '".database_safe($parts[1])."'";
break;
case "category":
$where .= " category = '".database_safe($parts[1])."'";
break;
case "brand":
$where .= " brand = '".database_safe($parts[1])."'";
break;
}
if ($_GET["catsubFilter"])
{
$where .= " AND catsub='".database_safe($_GET["catsubFilter"])."' ";
}
if ($_GET["brandFilter"])
{
$where .= " AND brand='".database_safe($_GET["brandFilter"])."' ";
}
$sql = "SELECT DISTINCT sub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY sub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subs[$row["sub"]] = $row["sub"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("subFilter",$subs,$_GET["subFilter"],"");
print "<input type='hidden' name='catsubFilter' value='".htmlentities($_GET["catsubFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='brandFilter' value='".htmlentities($_GET["brandFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='q' value='".htmlentities($q,ENT_QUOTES,$config_charset)."' />";
print "<input type='submit' value='Filter model' />";
print "</form>";
}
?>Cheers!
David.
SUPER !!!
Thx
its real fun now to see that some big i mean big companies doesnt have there product sorted :)
Thx
Henk
hi again,
a bit stupid question, how can i make it 5 rows high and link on select.
Thx HEnk
Hello Henk,
Should be straight forward to do; but both these involve changing the widget_selectArray() function. What I would do is edit includes/widget.php and create a new function called widgetSelectArray2() as follows:
function widget_selectArray2($name,$array,$default,$null_option)
{
// here are the changes to make if 5 rows high and link on select:
print "<select size='5' onchange='JavaScript:this.form.submit();' name='".$name."'>";
if (!$array[$default] && $null_option)
{
echo("<option value=''>".$null_option."</option>");
}
foreach($array as $k => $v)
{
if ($default == $k)
{
$selected = "selected='selected'";
}
else
{
$selected = "";
}
print "<option value='".htmlentities($k)."' ".$selected.">".htmlentities($v)."</option>";
}
print "</select>";
}Then, in your code above, simply change each of:
widget_selectArrayto
widget_selectArray2Cheers,
David.
--
Developer, Price Tapestry
For unrelated PHP, MySQL or Affiliate Marketing tech help please post your questions on my personal forum
Cool, and if there is no value is it possible to let it vanish.
Thx HEnk
Hi Henk,
Sure - all you need to do is bring the form HTML code inside the if (database_querySelect()) control structure, for example where you have:
$sql = "SELECT DISTINCT brand FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY brand";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$brands[$row["brand"]] = $row["brand"];
}
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("brandFilter",$brands,$_GET["brandFilter"],"");
print "<input type='hidden' name='catsubFilter' value='".htmlentities($_GET["catsubFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='subFilter' value='".htmlentities($_GET["subFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='q' value='".htmlentities($q,ENT_QUOTES,$config_charset)."' />";
print "<input type='submit' value='Filter By Brand' />";
print "</form>";...change this to:
$sql = "SELECT DISTINCT brand FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY brand";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$brands[$row["brand"]] = $row["brand"];
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray("brandFilter",$brands,$_GET["brandFilter"],"");
print "<input type='hidden' name='catsubFilter' value='".htmlentities($_GET["catsubFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='subFilter' value='".htmlentities($_GET["subFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='q' value='".htmlentities($q,ENT_QUOTES,$config_charset)."' />";
print "<input type='submit' value='Filter By Brand' />";
print "</form>";
}That will do the trick!
Cheers,
David.
--
Developer, Price Tapestry
For unrelated PHP, MySQL or Affiliate Marketing tech help please post your questions on my personal forum
uhm it doesnt vanish the the table.
$sql = "SELECT DISTINCT catsub FROM `".$config_databaseTablePrefix."products` WHERE ".$where." ORDER BY catsub";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$subcats[$row["catsub"]] = $row["catsub"];
}
print "<form method='GET' action='".$config_baseHREF."search.php'>";
widget_selectArray2("catsubFilter",$subcats,$_GET["catsubFilter"],"");
print "<input type='hidden' name='brandFilter' value='".htmlentities($_GET["brandFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='subFilter' value='".htmlentities($_GET["subFilter"],ENT_QUOTES,$config_charset)."' />";
print "<input type='hidden' name='q' value='".htmlentities($q,ENT_QUOTES,$config_charset)."' />";
print "</form>";
}
}
?>i changed it to this.
HEnk
Hi Henk,
With the code above, it should only show the catsubFilter form if there are items in the table, because database_querySelect($sql,$rows) should return 0 if there is nothing to display.
Is that not happening? Can you send me a link to page if not and your script - i'll take a look...
Cheers,
David.
--
Developer, Price Tapestry
For unrelated PHP, MySQL or Affiliate Marketing tech help please post your questions on my personal forum
Hi,
Is it possible to count the products in the filter and show it.
Thx Henk
Hi Henk,
It certainly is in theory; but I wouldn't recommend this in practice because of the number of queries required. - one count(*) query requiring a full table scan (only one index can be used at a time in SQL) per item per filter...
Cheers,
David.
--
Developer, Price Tapestry
For unrelated PHP, MySQL or Affiliate Marketing tech help please post your questions on my personal forum
Hi,
Thanks for your comments!
It would certainly be possible to do this - it's basically the addition of an SQL "WHERE" clause when selecting from the category index.
Rather than post a load of code in one go; it will be best to tackle the implementation in stages, so that you can get each part working before it all comes together.
The first stage would be to add a new parameter to search.php that adds the appropriate WHERE clause if a brandFilter has been supplied in the URL. To do this, you can just make the following addition to search.php
case "merchant":// pass through to category
case "category":
// pass through to brand
case "brand":
$where = " ".$parts[0]."='".database_safe($parts[1])."' ";
//******************//
// NEW CODE (BEGIN) //
if ($_GET["brandFilter"])
{
$where .= "AND brand='".database_safe($_GET["brandFilter"])."' ";
}
// NEW CODE (END) //
//****************//
After making that change; test the new function by constructing an appropriate search.php URL; for example:
http://www.example.com/search.php?q=category:Jeans&brandFilter=Levis
Let me know if this works as you're expecting, then i'll work through the code required to create a drop down...