Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 770701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:32:21+00:00 2026-05-14T18:32:21+00:00

This is a bit urgent! I’m trying to make a simple filter search where-by

  • 0

This is a bit urgent!

I’m trying to make a simple filter search where-by you can choose from a series of 3 drop downs and then based upon this the results are then displayed, How would I go about adjusting the sql query for each and if you were to only choose to search from aone of the 3 rather than all 3 etc…
example there could be the url with input such as: url.com?location=gb&color=3&hair=4 and still form the correct sql query for something like this: url.com?location=gb&hair=1 and not encounter problems with WHERE and AND etc etc and empty variables in the statement

Would this not need to be a massive function to check using if to see how the data is set for all possibilities?

Thanks,
Stefan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:32:22+00:00Added an answer on May 14, 2026 at 6:32 pm

    I answered a question the other day that I think is pretty similar to yours:

    PHP: prepared statement, IF statement help needed

    The idea is that you use conditional logic in your code to collect terms as needed corresponding to your application inputs. Then you join them together in such a way that produces the right SQL expression.

    It does need some application function to build the SQL expression dynamically, and there are techniques to make it as concise as possible. If you really have many possible search terms, you might end up with a lengthy function. But guess what? If you have complex inputs, it should be no surprise that you need complex code to deal with them.


    Re your comment:

    url.com?location=gb&color=3&hair=4

    Okay, you have up to three inputs and you have to dynamically build up an SQL query from these. Let’s start from the end and work backwards. Ultimately you want an SQL expression like this:

    WHERE (location = 'gb') AND (color = 3) AND (hair = 4)
    

    If you have an array of three terms, you can join them together in PHP using the implode() function. But you may also have fewer than three. You can handle any number of terms by putting however many terms you have into an array and imploding them with AND between each term:

    $where_array = array(
            "(location = 'gb')",
            "(color = 3)",
            "(hair = 4)"
        );
    
    $where_expr = "WHERE " . implode(" AND ", $where_array);
    

    So how do you create the array with these terms? By writing code to append to the array conditionally for each input that is present in your app’s current request:

    $where_array = array();
    if (array_key_exists("location", $_GET)) {
        $location = mysql_real_escape_string($_GET["location"]);
        $where_array[] = "(location = '$location')";
    }
    if (array_key_exists("color", $_GET)) {
        $color = mysql_real_escape_string($_GET["color"]);
        $where_array[] = "(color = '$color')";
    }
    if (array_key_exists("hair" $_GET)) {
        $hair = mysql_real_escape_string($_GET["hair"]);
        $where_array[] = "(hair = '$hair')";
    }
    

    After all that’s done, your array has between zero and three elements. If it has one or more, you want to generate a WHERE clause as shown previously, otherwise skip it.

    $where_expr = '';
    if ($where_array) {
        $where_expr = "WHERE " . implode(" AND ", $where_array);
    }
    

    Then append the $where_expr to your baseline SQL query.

    $sql .= $where_expr
    

    The stuff about $params is for query parameters, which is an alternative method of including dynamic values into an SQL expression, instead of mysql_real_escape_string(). It’s not mandatory (and in fact PHP’s old mysql extension doesn’t support query parameters) but I recommend switching to PDO so you can use this feature. See example here: PDO::prepare().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I fetch images from a server? I've got this bit of code
I'm trying to understand this bit of code: in display.php: <html> ... <body> <table>
Ok, this is bit of an obscure question, but hopefully someone can help me
This is a bit of a long shot, but if anyone can figure it
I have this bit of javascript written with jQuery 1.2.5. It's contained inside the
I have this bit of script to widen a text box on mouseover and
I have this bit of code I found on the web at the end
Well, I have this bit of code that is slowing down the program hugely
This a bit of strange one.... We have an internal web app that runs
Using c#, VS2005, and .NET 2.0. (XP 32 bit) This is a Winforms app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.