designer76 Posted May 23, 2011 Share Posted May 23, 2011 I need some serious help filtering search results using dropdown boxes. Right now my search is working perfectly fine. It searches by the keywords that people enter in and returns the results. My only problem is that after getting those results, I want people to be able to filter those results using dropdown boxes. So if someone searches by the keyword artwork, it will pull up all of my portfolio images that have artwork as a keyword. Now if they want to filter those results by: Color: Green, Type: Painting and Size: Large then they can use the dropdown boxes that will be populated with that information based off of their search term. I have no idea on how to even get started on something like this, so I would really appreciate all of the help I can get. Thanks in advance! Quote Link to comment Share on other sites More sharing options...
dougjohnson Posted May 23, 2011 Share Posted May 23, 2011 Are Color: Green, Type: Painting and Size: Large Keywords also? Quote Link to comment Share on other sites More sharing options...
dougjohnson Posted May 23, 2011 Share Posted May 23, 2011 Could you create a temp table containing all of the results from your first search results and then "search" the temp table for the other keywords? Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 23, 2011 Author Share Posted May 23, 2011 Are Color: Green, Type: Painting and Size: Large Keywords also? Yes they are. Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 23, 2011 Author Share Posted May 23, 2011 Could you create a temp table containing all of the results from your first search results and then "search" the temp table for the other keywords? I am not sure how I would go about doing that. Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 23, 2011 Author Share Posted May 23, 2011 I also forgot to add that I would like the dropdowns to instantly refresh and filter the results just how sites like eBay do it. Quote Link to comment Share on other sites More sharing options...
TOA Posted May 23, 2011 Share Posted May 23, 2011 Could you create a temp table containing all of the results from your first search results and then "search" the temp table for the other keywords? Yes you could I also forgot to add that I would like the dropdowns to instantly refresh and filter the results just how sites like eBay do it. That will be ajax Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 23, 2011 Author Share Posted May 23, 2011 Could you create a temp table containing all of the results from your first search results and then "search" the temp table for the other keywords? Yes you could I also forgot to add that I would like the dropdowns to instantly refresh and filter the results just how sites like eBay do it. That will be ajax How would I go about doing this? I am still learning this stuff and I am not sure how to go about achieving this? Are there any tutorials or anything you can point me to or possibly assist yourself? Any help given is very much appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 23, 2011 Share Posted May 23, 2011 Something like this works nice (untested): <?php $arr = new array(); // color if(!empty($_POST['color']) $arr[] = "color = '".$_POST['color']."'"; // type if(!empty($_POST['type']) $arr[] = "type = '".$_POST['type']."'"; // size if(!empty($_POST['size']) $arr[] = "size = '".$_POST['size']."'"; $str = implode(" and ", $arr); if(!empty($str)) $str = "and ".$str; mysql_query("select * from table where 1 $str"); ?> Quote Link to comment Share on other sites More sharing options...
TOA Posted May 23, 2011 Share Posted May 23, 2011 Beat me to it I'm not much on ajax so I'm afraid I can't help there, sorry Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 23, 2011 Author Share Posted May 23, 2011 Thanks The Little Guy, Now with this code do I put it in the php code that contains my search script? If so, then what am I going to put on my dropdown menus? Are they basically going to "post" those variables once the ajax does it's thing? Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 23, 2011 Author Share Posted May 23, 2011 ...I'm not much on ajax so I'm afraid I can't help there, sorry No problem man, I appreciate your input anyway! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 23, 2011 Share Posted May 23, 2011 Now with this code do I put it in the php code that contains my search script? Yes If so, then what am I going to put on my dropdown menus? You put the value of the drop down the same as the value that is in the database. Are they basically going to "post" those variables once the ajax does it's thing? If you want ajax I suggest doing it via jquery (http://jquery.com) you would then do something like this: <script type="text/javascript" src="/my/js/jquery.js"></script> <script type="text/javascript"> var itmcolor = $("#color").val(); var itmsize = $("#size").val(); var itmtype = $("#type").val(); $.ajax({ type: "POST", url: "/my/php/script.php", data: "color="+itmcolor+"&size="+itmsize+"&type="+itmtype, success: function(data){ $("#myOutput").html(data); } }); </script> hope that helps Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 23, 2011 Author Share Posted May 23, 2011 Thanks, I am going to try working with this now. BTW, I am getting syntax errors on these lines below in Dreamweaver. Do you know what could be wrong? $arr[] = "color = '".$_POST['color']."'"; Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 23, 2011 Share Posted May 23, 2011 What is the error? Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 23, 2011 Author Share Posted May 23, 2011 What is the error? It is just giving me a syntax error in the coding part of Dreamweaver. That's all it says is syntax error, and it is on each of the lines similar to one I posted above. When I try to view the page in the web browser it is blank, but I haven't been able to figure out what the syntax error is. Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 24, 2011 Author Share Posted May 24, 2011 Is anyone else able to help me with this? I am really stuck and have no idea how to proceed. Quote Link to comment Share on other sites More sharing options...
TOA Posted May 24, 2011 Share Posted May 24, 2011 Add this to the top of your script and see if anything changes. error_reporting('E_ALL'); Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 24, 2011 Author Share Posted May 24, 2011 Add this to the top of your script and see if anything changes. error_reporting('E_ALL'); Thanks, but I already have that at the top of my code. The syntax error inside of Dreamweaver usually means that something is typed incorrectly, like a " should be a ' or a : should be a ; Something along those lines. Quote Link to comment Share on other sites More sharing options...
TOA Posted May 24, 2011 Share Posted May 24, 2011 I know what a syntax error is And we can't help you with that. Start digging through your code Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 24, 2011 Author Share Posted May 24, 2011 I know what a syntax error is And we can't help you with that. Start digging through your code The syntax error only happens when I put the new code in that was posted in this thread. Even when I put the new code in a PHP file by itself, there is still a syntax error. That's why I don't think my search script is causing the syntax error. Quote Link to comment Share on other sites More sharing options...
TOA Posted May 24, 2011 Share Posted May 24, 2011 The syntax error only happens when I put the new code in that was posted in this thread. Even when I put the new code in a PHP file by itself, there is still a syntax error. That's why I don't think my search script is causing the syntax error. Of the code I saw posted, I didn't see any syntax problems. Does it give you a line number or any other info? Can you post the part of the sript you think is the culprit? Quote Link to comment Share on other sites More sharing options...
designer76 Posted May 24, 2011 Author Share Posted May 24, 2011 The syntax error only happens when I put the new code in that was posted in this thread. Even when I put the new code in a PHP file by itself, there is still a syntax error. That's why I don't think my search script is causing the syntax error. Of the code I saw posted, I didn't see any syntax problems. Does it give you a line number or any other info? Can you post the part of the sript you think is the culprit? Sure. This code below is giving me syntax errors in Dreamweaver on every line that has the $arr in it; <?php $arr = new array(); // color if(!empty($_POST['color']) $arr[] = "color = '".$_POST['color']."'"; // type if(!empty($_POST['type']) $arr[] = "type = '".$_POST['type']."'"; // size if(!empty($_POST['size']) $arr[] = "size = '".$_POST['size']."'"; $str = implode(" and ", $arr); if(!empty($str)) $str = "and ".$str; mysql_query("select * from table where 1 $str"); ?> Quote Link to comment Share on other sites More sharing options...
TOA Posted May 24, 2011 Share Posted May 24, 2011 Weird, that all looks fine at a quick glance. You could try putting braces in the ifs, like: if (!empty($_POST['color'])) { do your stuff here } You could also try removing the concatenations and do it like this: $arr[] = "color = '$_POST[color]'"; but to be honest, it should have worked either way. Dreamweaver can be touchy though so you never know Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 24, 2011 Share Posted May 24, 2011 Shouldn't those if statements be bracketed? $arr = new array(); // color if(!empty($_POST['color']){ $arr[] = "color = '".$_POST['color']."'"; } // type if(!empty($_POST['type']){ $arr[] = "type = '".$_POST['type']."'"; } // size if(!empty($_POST['size']){ $arr[] = "size = '".$_POST['size']."'"; } $str = implode(" and ", $arr); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.