Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 10th, 2004, 07:41 PM
Authorized User
 
Join Date: Aug 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Star007
Default multi Drop down list in PHP

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.




***************
__________________
***************
 
Old August 10th, 2004, 08:36 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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').select edIndex)
 {
  case 0: //change options accordingly for first option
  break;
  case 1: //change options for second option
  //etc.
 }
}

HTH,

Snib

<><
 
Old August 10th, 2004, 10:45 PM
Authorized User
 
Join Date: Aug 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Star007
Default

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.

***************
 
Old August 11th, 2004, 05:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

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">



    <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>



    <?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
 
Old August 12th, 2004, 11:53 AM
Authorized User
 
Join Date: Aug 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Star007
Default

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.

***************
 
Old August 12th, 2004, 01:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

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....

:D

have fun...
 
Old March 17th, 2012, 02:18 PM
Registered User
 
Join Date: Mar 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Please somebody help ...

I need a help.

I am making a upload page where i want to use three drop down as -

dropbox 1 -> dropbox 2 -> dropbox 3

I want to call the values from sql to all three dropdowns and also want to upload content based on choosen options. For example i want to upload a game of nokia mobile phone that’s model no is c200 then -

the device list (computer, mobile etc) list should come in dropdown 1 and device company name(Nokia, Samsung etc) should come in dropdown 2 and device model list(c200, c101 etc) should come in dropdown 3

then choose the file and hit upload.

please provide me this. your help will be heartfull for me as i am not able to make this... PLEASE HELP ME OUT

Last edited by prash; March 17th, 2012 at 02:24 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
multi column database source for drop down list mii2029 ASP.NET 3.5 Basics 3 August 4th, 2008 05:05 PM
multi-column list box values moved to 2nd list box sbmvr Access VBA 1 May 14th, 2007 01:58 PM
Multi Drop Down List epkon Pro PHP 0 December 1st, 2006 11:09 AM
List Tool Multi Column? Vendlus BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 April 28th, 2006 06:39 AM
drop down list values based on another drop down noor ASP.NET 1.0 and 1.1 Basics 3 July 5th, 2005 09:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.