Populating three list boxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stephene
    New Member
    • Aug 2006
    • 1

    Populating three list boxes

    New to the world of web design/php/mysql and need help please.

    What I'm trying to do:[U]

    I would like a web page with three drop down menus each populated by a query

    The first represents locations ie Room1, Room2 etc

    The second represents locations within the rooms i.e Rack1, Rack2 etc

    The third represents further sub locations ie Shelf1, Shelf2 etc

    so as you make a selection, say Room2, dropdown box 2 lists all of the sub locations in room 2, ie Rack1 etc and so on.

    I've setup a table wih locations in mysql which I can query.

    I've got some Javacode to do the three drop down list boxes.(hopefull y I can modify this when I have the results of my queries stored in the arrays)

    Problem:

    I'm having a problem filling arrays from my querys. The fist query works fine, but the second and third do not.

    I'm having trouble finding examples of filling multidimension arrays from queries

    Any help would be appriciated...t hanks

    heres my code:

    <?php include("/var/www/cgi-bin/dbconnect.php") ;

    #--------------------List of Main Locations------------------------------------------
    $mainlocations = array();
    $query = "select distinct location1 from locations";
    $result= mysql_query($qu ery) or die (mysql_error()) ;
    while ($row = mysql_fetch_arr ay($result))
    {
    $mainlocations[]=$row["location1"];
    }

    #--------------------List of Main locations and Sub locations------------------------
    $sublocations = array();
    foreach ($mainlocations as $loc )
    {
    $query = "select distinct location2 from locations where location1 = '$loc'";
    $result= mysql_query($qu ery) or die (mysql_error()) ;
    while ($row = mysql_fetch_arr ay($result))
    {
    $sublocations[$loc][]=$row["location2"];
    }
    }

    #--------------------List of Main locations, Sub locations and Subsublocations------
    $subsublocation s = array();
    foreach ($mainlocations as $loc1)
    {
    foreach ($sublocations as $loc)
    {
    $query = "select distinct location3 from locations where location1='$loc 1' and location2='$loc '";
    $result= mysql_query($qu ery) or die (mysql_error()) ;
    while ($row = mysql_fetch_arr ay($result))
    {
    $subsublocation s[$loc1][$loc][]=$row["location3"];
    }

    }
    }
    #------------------------------------------------------

    ?>
    <html>
    <FORM name="isc">

    <select name="example" size="1" onChange="redir ect(this.option s.selectedIndex )">
    <option selected>---Select1-------------</option>
    <?php
    foreach ($mainlocations as $loc)
    {
    echo "<option>$l oc</option>";
    }
    ?>
    </select>

    <select name="stage2" size="1" onChange="redir ect1(this.optio ns.selectedInde x)">
    <option value=" " selected> </option>
    <option value=" " selected>---Select2--------------</option>
    </select>

    <select name="stage3" size="1" onChange="redir ect2(this.optio ns.selectedInde x)">
    <option value=" " selected> </option>
    <option value=" " selected>---Select3----------------</option>
    </select>
    <script>
    <!--
    var groups=document .isc.example.op tions.length
    var group=new Array(groups)
    for (i=0; i<groups; i++)
    group[i]=new Array()

    group[0][0]=new Option("---Select2---"," ");

    group[1][0]=new Option("Now Select This One"," ");
    group[1][1]=new Option("Black", " ");
    group[1][2]=new Option("Blue"," ");
    group[1][3]=new Option("Red"," ");

    group[2][0]=new Option("Now Select This One"," ");
    group[2][1]=new Option("Pink"," ");
    group[2][2]=new Option("Yellow" ," ");

    var temp=document.i sc.stage2

    function redirect(x){
    for (m=temp.options .length-1;m>0;m--)
    temp.options[m]=null
    for (i=0;i<group[x].length;i++){
    temp.options[i]=new Option(group[x][i].text,group[x][i].value)
    }
    temp.options[0].selected=true
    redirect1(0)
    }

    var secondGroups=do cument.isc.stag e2.options.leng th
    var secondGroup=new Array(groups)
    for (i=0; i<groups; i++) {
    secondGroup[i]=new Array(group[i].length)
    for (j=0; j<group[i].length; j++) {
    secondGroup[i][j]=new Array() }}

    secondGroup[0][0][0]=new Option("---Select 3---"," ");
    secondGroup[1][0][0]=new Option("---Select 3---"," ");
    secondGroup[1][1][0]=new Option("Now Select This One"," ");
    secondGroup[1][1][1]=new Option("Large", "");
    secondGroup[1][1][2]=new Option("X-Large","");

    secondGroup[1][2][0]=new Option("Now Select This One"," ");
    secondGroup[1][2][1]=new Option("Large", "");
    secondGroup[1][2][2]=new Option("X-Large","");

    secondGroup[1][3][0]=new Option("Now Select This One"," ");
    secondGroup[1][3][1]=new Option("Large", "");
    secondGroup[1][3][2]=new Option("X-Large","");

    secondGroup[2][0][0]=new Option("---Select 3---"," ");
    secondGroup[2][1][0]=new Option("Now Select This One"," ");
    secondGroup[2][1][1]=new Option("Small", "");
    secondGroup[2][1][2]=new Option("Medium" ,"");

    secondGroup[2][2][0]=new Option("Now Select This One"," ");
    secondGroup[2][2][1]=new Option("Small", "");
    secondGroup[2][2][2]=new Option("Medium" , "" );

    var temp1=document. isc.stage3
    function redirect1(y){
    for (m=temp1.option s.length-1;m>0;m--)
    temp1.options[m]=null
    for (i=0;i<secondGr oup[document.isc.ex ample.options.s electedIndex][y].length;i++){
    temp1.options[i]=new Option(secondGr oup[document.isc.ex ample.options.s electedIndex][y][i].text,secondGro up[document.isc.ex ample.options.s electedIndex][y][i].value)
    }
    temp1.options[0].selected=true
    }

    function redirect2(z){
    window.location =temp1[z].value
    }

    //-->
    </script>

    </FORM>
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    I believe you are making this code way more difficult then it should be.

    Why not join the tables togethor and get all columns you need from one sql statement and then loop through them to create your select box.

    Comment

    Working...