Hi,
What I'm after is a sidebar with some search functionality such as dropdowns to specify a merchant, brand or category as well as being able to input a price range. I'm thinking dynamically populate the dropdowns and some where statements that get tagged on the end of a select would do the trick but wondered if you already had a solution or could help with the implementation?
I remember reading a similar topic here a little while back but can't seem to find it. Would also be good to be able to have static links that use GET variables to display preset filters. For instance, a link that displays products from x category for under £100.
thanks,
Jay
Hi David,
Thanks, can't wait for this. Kindly consider its compatibility with the External scripts you've done for us using Joomla/WP etc.
Ta
Ivor
Hows this coming along? Did you start a new thread?
regards,
Jay
Hi Jay,
I'm hoping to have a go at it over this weekend - will start a thread with the changes first thing next week (if not before...)
Cheers,
David.
Hello Jay,
I know you've been making some modifications yourself so if you could email me your search.php and html/searchresults.php i'll merge the changes in for you...
Cheers,
David.
Just to add to this - it would be great if you could consider the ability to add custom filters such as "Colour" or "Shoe Size".
Thanks!
Hi David, was wondering if theres a way to only have these filters tagged on to the URL when they have a value. Currently all variables will be included in the URL even if only one has been changed.
regards,
Jay
Hello Jay,
Sure - in the last version of search.php, you'll find this code starting at line 362:
$sortForm .= '<input type="hidden" name="merchantFilter" value="'.htmlentities($merchantFilter,ENT_QUOTES,$config_charset).'" />';
$sortForm .= '<input type="hidden" name="categoryFilter" value="'.htmlentities($categoryFilter,ENT_QUOTES,$config_charset).'" />';
$sortForm .= '<input type="hidden" name="brandFilter" value="'.htmlentities($brandFilter,ENT_QUOTES,$config_charset).'" />';
...so to only include the values if set, REPLACE the above with:
if ($merchantFilter) $sortForm .= '<input type="hidden" name="merchantFilter" value="'.htmlentities($merchantFilter,ENT_QUOTES,$config_charset).'" />';
if ($categoryFilter) $sortForm .= '<input type="hidden" name="categoryFilter" value="'.htmlentities($categoryFilter,ENT_QUOTES,$config_charset).'" />';
if ($brandFilter) $sortForm .= '<input type="hidden" name="brandFilter" value="'.htmlentities($brandFilter,ENT_QUOTES,$config_charset).'" />';
Cheers,
David.
thanks, couldn't see the wood through the trees, worked a treat. You're missing the { } though.
regards,
Jay
Think I was a little premature here. It's fine when changing the sort form but shows all the blank values when applying a filter. Is there a way to null this or does it have to pass these variables to the URL?
regards,
Jay
Hi Jay,
As the values are part of a form rather than a constructed URL, the only method I can think of is to use JavaScript to remove them from the form if empty when Submit is clicked. In html/searchresults.php, for each of the drop-down boxes you'll find this line (or equivalent for bf/brandFilter and cf/categoryFilter):
print "<select style='width:150px;' id='mf' name='merchantFilter'>";
In each instance, replace that line with:
print "<div id='dmf'><select style='width:150px;' id='mf' name='merchantFilter'>";
(replacing dmf with cmf for the category drop-down and bmf for the brand drop-down)
And for each one, replace the closing
print "</select>";
with:
print "</select></div>";
Then replace the following line which creates the submit button:
print "<input type='submit' value='Apply' />";
...with:
print "<input type='submit' value='Apply' onclick='JavaScript:
if (document.getElementById(\"mf\").value==\"\") { document.getElementById(\"dmf\").innerHTML=\"\"; }
if (document.getElementById(\"cf\").value==\"\") { document.getElementById(\"dcf\").innerHTML=\"\"; }
if (document.getElementById(\"bf\").value==\"\") { document.getElementById(\"dbf\").innerHTML=\"\"; }
' />";
Hope this helps!
Cheers,
David.
Just tried this and it works quite nicely and keeps functionality when js is off. thanks
regards,
Jay
sorry, me again.
Got a few loose ends now that the filters are working nicely. Listing them will probably be easier.
1. Can the mechant, brand and category links now act like the filters rather than using the $q input. If I browse by a certain brand, the brand filter doesn't have any other brands in them. What I'd like is clicking on browse mechant x would be the same as browsing all and having the merchant filter set.
2. I'm getting SQL syntax errors after hitting a blank search. I'm guessing it should be going to a noresults page?
3. As I understand it, the filters apply to whatever $q returns. Can there be a 'browse all' option $q=all ?
regards,
Jay
Hi Jay,
Re:1/ Yes - assuming that you're using clean URLs (created by .htaccess), the merchant / category / brand rewrites can be changed to use an all products search (see answer to your question 3 below) together with the appropriate filter. In your .htaccess, you should currently have:
RewriteRule ^merchant/$ merchants.php
RewriteRule ^merchant/(.*)/$ search.php?q=merchant:$1:&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^merchant/(.*)/(.*).html$ search.php?q=merchant:$1:&page=$2&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^category/$ categories.php
RewriteRule ^category/(.*)/$ search.php?q=category:$1:&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^category/(.*)/(.*).html$ search.php?q=category:$1:&page=$2&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^brand/$ brands.php
RewriteRule ^brand/(.*)/$ search.php?q=brand:$1:&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^brand/(.*)/(.*).html$ search.php?q=brand:$1:&page=$2&rewrite=1&%{QUERY_STRING} [L]
REPLACE all the above with
RewriteRule ^merchant/$ merchants.php
RewriteRule ^merchant/(.*)/$ search.php?q=bw:&merchantFilter=$1&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^merchant/(.*)/(.*).html$ search.php?q=bw:&merchantFilter=$1&page=$2&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^category/$ categories.php
RewriteRule ^category/(.*)/$ search.php?q=bw:&categoryFilter=$1&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^category/(.*)/(.*).html$ search.php?q=bw:&categoryFilter=$1&page=$2&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^brand/$ brands.php
RewriteRule ^brand/(.*)/$ search.php?q=bw:&brandFilter=$1&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^brand/(.*)/(.*).html$ search.php?q=bw:&brandFilter=$1&page=$2&rewrite=1&%{QUERY_STRING} [L]
Re:2/ Can you post the syntax error you are getting - I presume this means that you have MySQL debug mode enabled (this can be disabled on line 6 of config.advanced.php) but if you could post the error I should be able to figure out the cause...
Re:3/ The query bw: is equivalent to searching all products.
Cheers,
David.
this is what im getting where the sidebar filter should be...
An error occurred: error number 8
Script: '[FILE PATH] \sidebar3.inc.php'
Line: 18
message: Undefined variable: where
[SELECT DISTINCT(merchant) FROM `products` WHERE ][You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1]
An error occurred: error number 2
then this where the products should be...
Script: '[FILE PATH] \db_mysqli.inc.php'
Line: 23
message: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
regards,
Jay
Hi Jay,
Ah, that makes sense - the filter code, if it exists in a file that is displayed on all pages, should only take effect if it is both called only from search.php, and there is a value of $q. This can be done by enclosing the filter code within the following IF condition:
if (strpos($_SERVER["PHP_SELF"],"search.php")!==FALSE && $q)
{
// filter code
}
Hope this helps!
Cheers,
David.
I'm not actually using any of the rewrites. Can't remember why exactly but I think it was something to do with issues integrating with the rest of the site or SEO or something along those lines.
Instead of the rewrites then I could do something like search.php?q=bw&merchantFilter=[merchant] rather than search.php?q=merchant:[merchant]:
regards,
Jay
Hi Jay,
That's exactly right - in each of merchants.php, categories.php and brands.php you'll find the following code where the non-rewrite link is generated:
$item["href"] = "search.php?q=merchant:".urlencode($product["merchant"]).":";
...so in place of that, you could use (adjusted as required in each case):
$item["href"] = "search.php?q=bw:&merchantFilter=".urlencode($product["merchant"]);
(make sure you don't miss my previous reply regarding the SQL syntax error - I think we were posting at the same time!)
Cheers,
David.
Thats done the trick, thanks. What do I need to change so a blank search brings up the noresults page?
regards,
Jay
Hi Jay,
That should be the case as it stands, in search.php you should have
require("html/noresults.php");
(quite near the end, line 297 in the distribution)
Check that you have content in that file to be displayed if there are no results; and it should appear in conjunction with the "(no results found)" displayed in the banner (where the number of results would normally be displayed)...
Cheers,
David.
The 'no results' shows up if I type something in that has no matches but shows nothing when a $q is empty. Not even the h2 banner.
regards,
Jay
Hello Jay,
I understand - sorry about that; in search.php you will find this code around about line 289:
if (isset($searchresults))
{
if ($searchresults["numRows"])
{
require("html/searchresults.php");
}
else
{
require("html/noresults.php");
}
}
...REPLACE that with:
if (isset($searchresults) && $searchresults["numRows"])
{
require("html/searchresults.php");
}
else
{
require("html/noresults.php");
}
That should do the trick!
Cheers,
David.
Hello Jay,
This is a common request so I'll put together a mod based on search.php and html/searchresults.php and publish the details in a new thread...
Cheers,
David.