Hi all,

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

EX:

Dropdown1: CITY
Dropdown2: STATE
Dropdown3: COMPANY

BUTTON: GO

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
if($_GET['city'])
{
    $profile_id = $_GET['city'];
}
else
{
    $profile_id = $_SESSION['id'];
}

$agent=$_GET['city'];
// 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'];
  $userlevel=$preview_profile['userlevel'];
  $email_notify=$preview_profile['email_notify'];

In the Body:

<FORM ACTION="" METHOD=GET >
     	   <SELECT NAME="city">
             <OPTION VALUE='0'>Choose a city...</OPTION>
             <?php
             $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>
           <br/>
           <br/>
     	   <SELECT NAME="state">
             <OPTION VALUE='0'>Choose a state...</OPTION>
             <?php
             $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>
           <br/>
           <br/>
     	   <SELECT NAME="company">
             <OPTION VALUE='0'>Choose a company...</OPTION>
             <?php
             $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
             ?>
           </SELECT>
           <br/>
           <br/>
          <INPUT TYPE=SUBMIT class="button_login" VALUE="Go">
         </FORM>

Totally appreciate any help on this...

Recommended Answers

All 2 Replies

if( empty($_GET['city']) )
{
    $city_id = "city_id = '" . $_GET['city'] . "' ";
} else if ( empty($_GET['state']) ) {
    $state = " AND state = ' " . $_GET['state'] . " ' ";
else
{
    $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.

Cheers...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.