Hi all,

I am trying to handle an sql query for multiple dropdowns where all won't neccessarily be active.


Dropdown1: CITY
Dropdown2: STATE
Dropdown3: COMPANY


Need the ability to choose from one, two or all to make a query.

Here is what I have now:

Above the doc type:

// get user from db
    $profile_id = $_GET['city'];
    $profile_id = $_SESSION['id'];

// get the profile from the database ready to display	 
  $select_profile = mysql_query("SELECT * FROM users WHERE id ='$profile_id'");
// get the array now
  $preview_profile = mysql_fetch_array($select_profile);
// get the profile details from the array
  $username = $preview_profile['username'];
  $email = $preview_profile['email'];
  $fname = $preview_profile['fname'];
  $lname = $preview_profile['lname'];
  $title = $preview_profile['title'];
  $office = $preview_profile['office'];
  $phone = $preview_profile['phone'];
  $mobile = $preview_profile['mobile'];
  $fax = $preview_profile['fax'];
  $website = $preview_profile['website'];
  $bio = $preview_profile['bio'];
  $image = $preview_profile['avatar'];

In the Body:

     	   <SELECT NAME="city">
             <OPTION VALUE='0'>Choose a city...</OPTION>
             $all_cities = mysql_query("SELECT id, city FROM users ORDER BY city");
             while($show_cities = mysql_fetch_array($all_cities)){
             $agent_id = $show_cities['id'];
             $city_name = $show_cities['city'];
             echo "<option value=\"$agent_id\">$city_name</option>";
	         }//end of while
     	   <SELECT NAME="state">
             <OPTION VALUE='0'>Choose a state...</OPTION>
             $all_states = mysql_query("SELECT id, state FROM users ORDER BY state");
             while($show_states = mysql_fetch_array($all_states)){
             $agent_id = $show_states['id'];
             $state_name = $show_states['state'];
             echo "<option value=\"$agent_id\">$state_name</option>";
	         }//end of while
     	   <SELECT NAME="company">
             <OPTION VALUE='0'>Choose a company...</OPTION>
             $all_offices = mysql_query("SELECT id, office FROM users ORDER BY office");
             while($show_offices = mysql_fetch_array($all_offices)){
             $agent_id = $show_offices['id'];
             $office_name = $show_offices['office'];
             echo "<option value=\"$agent_id\">$office_name</option>";
	         }//end of while
          <INPUT TYPE=SUBMIT class="button_login" VALUE="Go">

Totally appreciate any help on this...

if( empty($_GET['city']) )
    $city_id = "city_id = '" . $_GET['city'] . "' ";
} else if ( empty($_GET['state']) ) {
    $state = " AND state = ' " . $_GET['state'] . " ' ";
    $profile_id = $_SESSION['id'];

the build the query like so

"SELECT * FROM users WHERE $city_id $state";

I hope this helps you.
I am sure there is a smarter way to do this .. but it's 5 a clock here :D

Hi Ancyent, been at some other tasks on this site and will try out your suggestion in the next couple of days. Thank you for the reply, always much appreciated.