Subject: multi Drop down list in PHP
Posted By: Star007 Post Date: 8/10/2004 7:41:06 PM
I am working on a script which require  advance search

I have 4 tables and I want to connect each table in the search.

First drop down list would be region

second depending on the region : the country
third depending on the country: the state
4th depending on the state: the city.


REGION TABLE
RE_ID RE_NAME
1     EAST
2     WEST

COUNTRY TABLE

COUN_ID  RE_ID   COUN_NAME
1         1      CHINA
2         1      JAPAN
3         2      CANADA
4         2      USA


STATE TABLE
STAT_ID   COUN_ID   STAT_NAME
1           1        SOMENAME
2           1        SOMENAME2
3           3        ONTARIO
4           3        MANITOBA
5           4        NEW YORK
6           4        BUFFALO

CITY TABLE
CIT_ID  STAT_ID   CITY_NAME
1        3          TORONTO
2        3          AJAX
3        5          NEW YORK CITY
4        5          SOME CITY


IN THE FIRST DROP DOWN YOU HAVE OPTION TO SELECT EAST OR WEST
IF YOU SELECT WEST THEN IN THE SECOND DROP LIST SHOULD HAVE
CANADA AND USA AND IF YOU SELECT CANADA THEN IN THE 3RD DROP DOWN
YOU SHOULD HAVE ONTARIO AND MANITOBA AND IF YOU SELECT ONTARIO THE FORTH DROP DOWN SHOULD HAVE TORONTO AND AJAX

AND I SHOULD BE ABLE TO GET THE ABOVE INFO AND DO A SEARCH IN THE CITY FOR STORES


SPEACIAL THANKS TO WHO EVER CAN HELP ME ON THIS....
i REALLY APPRECIATE YOUR HELP
THANK YOU.




***************
Reply By: Snib Reply Date: 8/10/2004 8:36:44 PM
This question is more appropriate in the JavaScript section, since that is what you'll likely end up using to solve your problem.

If you have no knowledge of JS, then you should probably look for something pre-made.

I suggest looking at http://javascript.internet.com and http://dynamicdrive.com .

If you do know a bit of JS, here's the trick. Add an onchange attribute to the <select> attribute and have it call a function that checks the selectedIndex of the dropdown box and changes the options of the next dropdown box depending on it.

For instance:

function changeOptions()
{
 switch(document.getElementById('selectbox').selectedIndex)
 {
  case 0: //change options accordingly for first option
  break;
  case 1: //change options for second option
  //etc.
 }
}

HTH,

Snib

<><
Reply By: Star007 Reply Date: 8/10/2004 10:45:09 PM
well I think javascript will be a long way to do it...

I like to have a short script

here is a example for 2 drop down menu

I want 4 drop down..can you plz take a look at this script and modify to 4 drop down menu

//Perhaps something along these lines:

//The database fields and some variable names are in danish, but you should be able to figure it out anyway :)
//quick translation:
//grupper = groups
//varer = products
//navn = name

<html>
<body>
<?php

$server = "localhost";
$brugernavn = "name";
$password = "pass";

mysql_connect($server, $brugernavn, $password)
or die( "Unable to connect\n". mysql_error() );

mysql_select_db("vech")
or die("Unable to select db ".mysql_error()."\n");

$id = $_GET['id'];
print"<table border=1><tr><td>";
echo'<form name="testform">';
$q = mysql_query("SELECT * FROM cat");
echo"<select name=\"cat\" onChange=\"Load_id()\">";

 

while($row = mysql_fetch_array($q)) {
$selected = ($row["cat_id"] == $id)? "SELECTED":"";
echo"<option value=\"".$row['cat_id']."\"". $selected." >".$row['cat_name']."</option>";
}
echo"</select>";
print"</td></tr><tr><td>";
print "<p>";
$q2 = mysql_query("SELECT * FROM type WHERE cat_id = $id");
echo"<select  name=\"type\">";
while($row = mysql_fetch_array($q2)) {
echo"<option value=\"".$row['type_id']."\">".$row['type_name']."</option>";
}
echo"</select></form>";
print"</td></tr></table>";
?>

<script type="text/javascript">
function Load_id()
{
var id = document.testform.cat.options[document.testform.cat.selectedIndex].value
var id_txt = "?id="
location = id_txt + id
}
</script>
</BODY>
</HTML>



thank you.

***************
Reply By: Moharo Reply Date: 8/11/2004 5:18:10 AM
hey there

i didn't not go thru the code you have but i did something new... the concept is just like yours... notice that i
didn't not use a table for regions, since there's only two values (EAST and WEST) i don't think doing the table is a good idea....
so instead i used select with these two values... if you don't like that just see how other 3 drop down menus are made...



<?php

$region = $country = $state = $city = null; //declare vars

$conn = mysql_connect('localhost');
$db = mysql_select_db('testing',$conn);

if(isset($_GET["region"]) && is_numeric($_GET["region"]))
{
    $region = $_GET["region"];
}

if(isset($_GET["country"]) && is_numeric($_GET["country"]))
{
    $country = $_GET["country"];
}

if(isset($_GET["state"]) && is_numeric($_GET["state"]))
{
    $state = $_GET["state"];
}

if(isset($_GET["city"]) && is_numeric($_GET["city"]))
{
    $city = $_GET["city"];
}

?>

<script language="JavaScript">

function autoSubmit()
{
    var formObject = document.forms['theForm'];
    formObject.submit();
}

</script>

<form name="theForm" method="get">

    <!-- REGION SELECTION -->
    
    <select name="region" onChange="autoSubmit();">
        <option value="null"></option>
        <option value="1" <?php if($region == 1) echo " selected"; ?>>East</option>
        <option value="2" <?php if($region == 2) echo " selected"; ?>>West</option>
    </select>

    <br><br>

    <!-- COUNTRY SELECTION BASED ON REGION VALUE -->
        
    <?php
    
    if($region != null && is_numeric($region))
    {
    
    ?>
    
    <select name="country" onChange="autoSubmit();">        
        <option value="null"></option>
        
        <?php
        
        //POPULATE DROP DOWN MENU WITH COUNTRIES FROM A GIVEN REGION
        
        $sql = "SELECT COUN_ID, COUN_NAME FROM COUNTRY WHERE RE_ID = $region";
        $countries = mysql_query($sql,$conn);
        
        while($row = mysql_fetch_array($countries))
        {        
            echo ("<option value=\"$row[COUN_ID]\" " . ($country == $row["COUN_ID"] ? " selected" : "") . ">$row[COUN_NAME]</option>");        
        }
        
        ?>
        
    </select>
    
    <?php
    
    }
    
    ?>
    
    <br><br>
    
    <?php
    
    if($country != null && is_numeric($country) && $region != null)
    {
    
    ?>
    
    <select name="state" onChange="autoSubmit();">
        <option value="null"></option>
        
        <?php
        
        //POPULATE DROP DOWN MENU WITH STATES FROM A GIVEN REGION, COUNTRY
        
        $sql = "SELECT STAT_ID, STAT_NAME FROM states WHERE COUN_ID = $country ";
        $states = mysql_query($sql,$conn);
        
        while($row = mysql_fetch_array($states))
        {
            echo ("<option value=\"$row[STAT_ID]\" " . ($state == $row["STAT_ID"] ? " selected" : "") . ">$row[STAT_NAME]</option>");        
        }
        
        ?>    
        
    </select>
    
    <?php
    
    }
    
    ?>
    
    <br><br>
    
    <?php
    
    if($state != null && is_numeric($state) && $region != null && $country != null)
    {
    
    ?>
    
    <select name="city" onChange="autoSubmit();">
        <option value="null"></option>
        
        <?php
        
        //POPULATE DROP DOWN MENU WITH CITIES FROM A GIVEN REGION, COUNTRY, STATE
        
        $sql = "SELECT CIT_ID, CITY_NAME FROM CITY WHERE STAT_ID = $state ";
        $cities = mysql_query($sql,$conn);
        
        while($row = mysql_fetch_array($cities))
        {
            echo ("<option value=\"$row[CIT_ID]\" " . ($city == $row["CIT_ID"] ? " selected" : "") . ">$row[CITY_NAME]</option>");        
        }
        
        ?>    
        
    </select>
    
    <?php
    
    }
    
    ?>

</form>

www.campusgrind.com the college portal
Reply By: Star007 Reply Date: 8/12/2004 11:53:25 AM
Hi there,
Million thanx for da help.

Can you plz help me on one more thing

ok the above code is just fine, but the thing is when i run it I only get one drop down menu and then after I choose the 1 from the region the second drop down show up and then choose one from second the third drop down show up on screen.

I would like to have all 4 drop down at the frist time and as I choose the options the valuses should change.

just like this http://www.trader.ca/WeilandFord/default.asp?trader=1

if they try to choose from the second or third drop down before choosing one from the first one it should pop a message please choose one from the first drop down. somethign like that.

I know you can help me on this..
I really appreciate your help.
Thanx a lot.

***************
Reply By: Moharo Reply Date: 8/12/2004 1:44:31 PM
hello

if you want to have all 4 drop downs visible, just remove "if()" conditions that are responsible for displaying a given select each time. however this might create some problems because you either have to have default values for $region, $country and the rest (that's because the next select values are populated based on previous selection, and the default values for these variables is NULL) or you should populate an array which would contain all values from the database and use javascript to alter select's values based on the selection from previous select, so forth and so on....



have fun...


Go to topic 17496

Return to index page 807
Return to index page 806
Return to index page 805
Return to index page 804
Return to index page 803
Return to index page 802
Return to index page 801
Return to index page 800
Return to index page 799
Return to index page 798