Jump to content

I Need Some Help Filtering Search Results Using Dropdowns!


designer76

Recommended Posts

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!

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

I know what a syntax error is  :P  And we can't help you with that. Start digging through your code  8)

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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");
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.