Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post

    Dynamic dropdowns [solved]

    Hello, I need to make a dynamic 2 level dropdown for auto trading site. It will receive model lists from MySQL. And it will be like on cars.com (makes and models).
    I have this piece of JS:
    Code:
    <script type="text/javascript">
    
    var makelist=document.classic.make
    var modellist=document.classic.model
    
    var model=new Array()
    model[0]=""
    model[1]=["A4|A4", "A6|A6", "A8|A8", "TT|TT", "Q7|Q7"]
    model[2]=["M3|M3", "M5|M5", "M6|M6", "325|325"]
    model[3]=["Enzo|Enzo", "F30|F30", "F50|F50"]
    
    function updatemodel(selectedcitygroup){
    modellist.options.length=0
    if (selectedcitygroup>0){
    for (i=0; i<model[selectedcitygroup].length; i++)
    modellist.options[modellist.options.length]=new Option(model[selectedcitygroup][i].split("|")[0], model[selectedcitygroup][i].split("|")[1])
    }
    }
    
    </script>
    So basically, I will need to update this:
    Code:
    model[0]=""
    model[1]=["A4|A4", "A6|A6", "A8|A8", "TT|TT", "Q7|Q7"]
    model[2]=["M3|M3", "M5|M5", "M6|M6", "325|325"]
    model[3]=["Enzo|Enzo", "F30|F30", "F50|F50"]
    So, any ideas?
    Last edited by M.W.S.; 08-02-2008 at 05:46 PM.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Well, your in the php forum, and asking about javascript, but........

    I was just asking a similar question myself. See my post here. I found a nice php script that may help you out.

    http://www.codingforums.com/showthread.php?t=145548

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    This is a PHP code I use to give you an idea where to start.
    PHP Code:
    <select>
    <?php
    $sql 
    mysql_query("SELECT id, project FROM project_supplies ORDER BY id ASC");
    while(
    $item mysql_fetch_array($sql)){
    echo 
    "<option value=\"$item[1].php#$item[0]\">$item[0]</option>\n";
    }
    ?>
    </select>

    -----------------
    Last edited by Len Whistler; 08-01-2008 at 06:53 PM.
    Leonard Whistler

  • #4
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post
    Thanks for reply.
    But, I think you don't get what I wanted.
    You see, I want to make a menu, sort of this:



    (see cars.com)

    I could do this manually, but It will be veeeery tiring. So I wanted to just add a menu item and subitem from DB.

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    What Len Whistler showed you will do the trick. Just google around for dynamic selects.

  • #6
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    look into multiselects

    http://www.infimum.dk/HTML/multiselect.html

    just an example

  • #7
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by M.W.S. View Post
    Thanks for reply.
    But, I think you don't get what I wanted.
    You see, I want to make a menu, sort of this:



    (see cars.com)

    I could do this manually, but It will be veeeery tiring. So I wanted to just add a menu item and subitem from DB.
    You would have to add a while loop within a while loop of the PHP code I posted to populate the Javascript code from the MySQL database.

    OR

    Maybe an Ajax solution might be better. I don't know anything about Ajax but I do believe that Ajax is for this sort of thing.





    -----------
    Leonard Whistler

  • #8
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post
    Thanks for replies

    Finally, I did it using this:
    http://www.yxscripts.com/cs/chainedselects.html and PHP file manipulation.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •