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 8270361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:33:04+00:00 2026-06-08T06:33:04+00:00

The following code needs to make use of 3 variables given by the user.

  • 0

The following code needs to make use of 3 variables given by the user. By default all of these variables equal to 0.

  1. time (textbox)
  2. city (drop down list)
  3. type (drop down list)

If for example time and city is given by the user, but lets the type zero, it will not return any results.
My question is what is an effective and efficient way to modify my existing code so that if the user chooses not to select time, city or type or any combination of these, there will be results returned?

For example if time 21:00 is added with city number 3, it will show all the types that meet the 2 criteria should be listed.

$question= 'SELECT * FROM events WHERE ABS(TIMESTAMPDIFF( HOUR , `time`, :time )) < 2 AND city=:city AND type=:type';
$query = $db->prepare($question);
$query->bindValue(":time", $time, PDO::PARAM_INT);
$query->bindValue(":city", $city, PDO::PARAM_INT);
$query->bindValue(":type", $type, PDO::PARAM_INT);
$query->execute();
  • 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-08T06:33:05+00:00Added an answer on June 8, 2026 at 6:33 am
    <?php
    $question= 'SELECT * FROM events WHERE ';
    
    $hasTime = false;
    if(!empty($time)) { // @note better validation here
        $hasTime = true;
        $question .= 'ABS(TIMESTAMPDIFF( HOUR , `time`, :time )) < 2 ';
    }
    
    $hasCity = false;
    if(!empty($city)) { // @note better validation here
        $hasCity = true;
        $question .= 'AND city=:city ';
    }
    
    $hasType = false;
    if(!empty($type)) { // @note better validation here
        $hasType = true;
        $question .= 'AND type=:type';
    }
    
    $query = $db->prepare($question);
    
    if($hasTime)
        $query->bindValue(":time", $time, PDO::PARAM_INT);
    if($hasCity)
        $query->bindValue(":city", $city, PDO::PARAM_INT);
    if($hasType)
        $query->bindValue(":type", $type, PDO::PARAM_INT);
    
    $query->execute();
    $results = $query->fetchAll();
    
    if(empty($results))
        echo 'no results';
    else
        // $results is an array of arrays
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code needs to produce an XML file. string path = @c:\load\myFile.xml; SqlConnection
I dont understand what would be the problem with the following code. It needs
In the following code I need to print output from the variables $stout ,
I can use the following code for tiny little queries: DECLARE @sql VARCHAR(8000) SET
In the following code I want to reduce these 5 functions down to 3.
In the following code, where would I use preventDefault ? I have been testing
i have the following code ..i need to loop through end of the file
I need syntax help with the following code logic: I have a code block
I have the following FORTRAN code which I need to convert to C or
The following code generates a MS Word document in portrait format. But I need

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.