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

  • Home
  • SEARCH
  • 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 9027387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:44:06+00:00 2026-06-16T06:44:06+00:00

I am creating a search filter feature and will be sending the filter information

  • 0

I am creating a search filter feature and will be sending the filter information via the URL. The issue I am facing is that since it’s a filter, some of the information will be there at some times, and some not. This creates the undefined variable problem.

The maximum amount of filters will be like so: search.php? cat=Computers&credits1=1&credits2=2&rev=5&stars=5. I will also be pulling data from multiple SQL tables based on the GET variables set.

How can I account for some variables being unset at certain times in order to run my query? I think I could do this via a bunch of if else statements accounting for each case, but that seems like “dirty code.”

  • 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-06-16T06:44:07+00:00Added an answer on June 16, 2026 at 6:44 am

    You should just build your SQL statement dynamically based on the filter values that are passed. This will likely mean conditionals, but this is really the best method in order to optimize your queries on the database.

    It might look something like this (example uses mysqli, but this should be similar for any sort of DB library using prepared statements):

    $query = 'SELECT * FROM table'; // or specifically specify the fields you need in result set.
    if (empty($_GET['cat']) && empty($_GET['credits1']) && empty($_GET['credits2']) && empty($_GET['rev']) && empty($_GET['stars'])) {
        // there are no search filters set so maybe you alert some sort of error
    } else {
        $query .= ' WHERE ';
        $parameters = array();
        $at_least_one_where_set = false;
        if (!empty($_GET['cat'])) {
            if($at_least_one_where_set === true) {
                $query .= ' AND cat=?';
            } else {
                $query .= 'cat=?';
            }
            $at_least_one_where_set = true;
            $parameters[]['value'] = $_GET['cat'];
            $parameters[]['type'] = 's';
        }
    
        // do similar conditionals for each $_GET value you are interested in.
    
        // query the database    
        $mysqli = new mysqli('host', 'user', 'password', 'database');
        $stmt = $mysqli->stmt_init();
        $stmt->prepare($query);
        foreach ($parameters as $parameter) {
            $stmt->bind_param($parameter['type'], $parameter['value']);
        }
        $stmt->execute();
    
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {
            // do something with your result row
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems simple enough. I'm creating a search engine that returns results to the user
I'm creating eBay style search panes that allow users to narrow their results set
I'm creating a set of search panes that allow users to tweak their results
The code below is creating a search for many strings. Initially there are 5
I'm creating a search engine for my asp page.There are 15 pieces filtering options
I currently have a search that allows me to filter products down, for a
I'm creating a stored procedure to return search results where some of the parameters
I am creating a search list in java. If I enter the beginning letters,
I am creating a search page where we can find the product by entering
I'm creating video music search in android WebView and I wanted users can launch

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.