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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Display Results in Drop Down Box

    Currently, I have a form that list a number of drop down boxes. And each time, I add a new option, I have to update the form again.

    I'd like to possibly create a table for all of the options, and then just have the form read the table and display them in the drop down box.

    The goal is to have code that I don't have to change each time, but rather just add/update the database as needed.

    I'm going to hopefully tonight try and work on it, but not sure exactly how hard it maybe. I think I just need help on how the code should look on the form.

    I may be able to post some of my progress as well tonight once I get this 'real' work done today.

    Thanks
    PT

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    PHP Code:

    //Query the DB
    $query "whatever";
    $result mysql_query($query) or die(mysql_error());
    while(
    $row mysql_fetch_array($result))
    {
    echo(
    "<option>{$row['option']}</option>")

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    New Coder
    Join Date
    Jul 2007
    Location
    malaysia
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    my example

    PHP Code:
    <? 
    $aProduct 
    db_query(" SELECT * FROM product_ms WHERE product_display = 'Y' order by product_code "); 

    if (!
    is_array($aProduct) ) 

    echo 
    "<option value=''>no Product available</option>"

    else 

    foreach (
    $aProduct as $rowProduct

    echo 
    "<OPTION VALUE=".($rowProduct["product_code"]).">".($rowProduct["product_desc"])."</OPTION>"


    ?>

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Hey guys,

    Thanks for the help. Was pretty hectic recently, but back at this after about two weeks away.

    I just gave it a shot, but haven't gotten it quite right yet. I've tried the following below, but the drop down only lists "No Games" when it should be listing the items from the database.

    PHP Code:
    $query "SELECT * FROM game_types"
            
    $game_types mysql_query($query) or die(mysql_error());
            
            if (!
    is_array($game_types)){ 
                echo 
    "<option value='none'>No Games</option>"
            } else {
                foreach (
    $game_types as $rowTypes){ 
                    echo 
    "<OPTION VALUE=".($rowTypes["game_style"]).">".($rowType["display_name"])."</OPTION>"
                } 
            } 

  • #5
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    PHP Code:
    $game_types mysql_query($query) or die(mysql_error()); 
    That's because this line sets $game_types to a resource id for the query, not an array of results.

    Take a look at the examples here

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Edit:

    Nevermind. I'm closer than I think on getting the second menu to populate correctly.
    Last edited by ptmuldoon; 08-25-2007 at 07:00 PM.

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Ok, I've made some progress.

    Following this tutorial, I have two drop down boxes, with the second drop down box being populated based on the result of the first drop box box.

    Tutorial Used: http://www.plus2net.com/php_tutorial..._down_list.php

    But now my issue......

    After you choose the first drop down menu, the page reloads to list the items for the second drop down menu.

    Can this be done without reloading the page? The issue is that the user is entering a number of fields prior to choosing the first drop down menu option. And then after they enter their menu choice, the page reloads, and all the information previously entered is lost, and has to be retyped into the form.
    Last edited by ptmuldoon; 08-26-2007 at 01:25 AM.

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    It can be done with AJAX. Try the JavaScript forum or searching google for "chained selects". You may have to add some other keywords..

  • #9
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Thanks for the help. I googled out Chain Selects, and found this tutorial here, which will create the second drop down without a page refresh.

    But the tutorial uses a static php content to create the drop down menus (getCities.php) file.

    Tutorial: http://www.ajax-tutorials.com/tutori...boxes/view.php

    Can someone maybe give a small example on how to change that from using a Switch statement to querying a database?

    I'm still learning, but my quess is possibly try to create a two loops within a switch statement? The first to loop through each case, and then a second loop to list the options available for each case?
    Last edited by ptmuldoon; 08-26-2007 at 05:42 PM.

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    PHP Code:
    $id = (int) $_GET['id'];
    $result mysql_query('SELECT * FROM `table` WHERE `id` = ' $id);
    while(
    $row mysql_fetch_assoc($result))
    {
        echo 
    "obj.options[obj.options.length] = new Option('" $row['name'] . "','" $row['id'] . "');\n";

    Add error checking, and customize, etc.
    Last edited by Inigoesdr; 08-26-2007 at 06:49 PM.

  • #11
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    I had to break from this earlier in the week, but am back looking to complete it. And I must admit the above sample has me confused.

    Basically, what I think I need to do now is to somehow convert a switch statement from using static data, to query a database:

    PHP Code:
    switch($_GET['countryCode']){
        
        case 
    "R1":
          echo 
    "obj.options[obj.options.length] = new Option('Bergen','1');\n";
          echo 
    "obj.options[obj.options.length] = new Option('Haugesund','2');\n";
          echo 
    "obj.options[obj.options.length] = new Option('Oslo','3');\n";
          echo 
    "obj.options[obj.options.length] = new Option('Stavanger','4');\n";
          
          break;
        case 
    "R2":
          
          echo 
    "obj.options[obj.options.length] = new Option('Aalborg','11');\n";
          echo 
    "obj.options[obj.options.length] = new Option('Copenhagen','12');\n";
          echo 
    "obj.options[obj.options.length] = new Option('Odense','13');\n";
          
          break; 
    So instead of having hard coded switch statements (R1, R2, etc), it should loop through the database, and list each each case, and what is available. Is that what the above code by Inigoesdr is doing? If so, than I just need to try and digest it more till I understand it better.

  • #12
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by ptmuldoon View Post
    Is that what the above code by Inigoesdr is doing? If so, than I just need to try and digest it more till I understand it better.
    Yes, and yes.

  • #13
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Well, I wish I knew where I was going wrong. I can use the below sample from the tutorial, which works fine:
    PHP Code:
    if(isset($_GET['countryCode'])){
      
      switch(
    $_GET['countryCode']){
        
        case 
    "R1":
          echo 
    "obj.options[obj.options.length] = new Option('Bergen','1');\n";
          echo 
    "obj.options[obj.options.length] = new Option('Haugesund','2');\n";
          echo 
    "obj.options[obj.options.length] = new Option('Oslo','3');\n";
          echo 
    "obj.options[obj.options.length] = new Option('Stavanger','4');\n";
          
          break; 
    But when I try to change that to use the query, it doesn't want to work
    PHP Code:
    if(isset($_GET['countryCode'])){
        
        
    $id =($_GET['countryCode']);
            
    $result mysql_query('SELECT * FROM `map_types` WHERE `game_style` = ' $id);
            while(
    $row mysql_fetch_assoc($result)){
                echo 
    "obj.options[obj.options.length] = new Option('" $row['css_file'] . "','" $row['map_types'] . "');\n";
            }
      

    Since this is using AJAX to get the script and return the result into the second drop down box, I believe the issue is within the AJAX script. In looking at the AJAX code (which I really don't comprehend), I do see the following code referencing the case. Perhaps I need to find a better sample of using AJAX and chained selects so that is will query a database?

    PHP Code:
    this.xmlhttp.onreadystatechange = function() {
                        switch (
    self.xmlhttp.readyState) {
                            case 
    1:
                                
    self.onLoading();
                                break;
                            case 
    2:
                                
    self.onLoaded();
                                break;
                            case 
    3:
                                
    self.onInteractive();
                                break;
                            case 
    4:
                                
    self.response self.xmlhttp.responseText;
                                
    self.responseXML self.xmlhttp.responseXML;
                                
    self.responseStatus[0] = self.xmlhttp.status;
                                
    self.responseStatus[1] = self.xmlhttp.statusText;

                                if (
    self.execute) {
                                    
    self.runResponse();
                                }

                                if (
    self.elementObj) {
                                    
    elemNodeName self.elementObj.nodeName;
                                    
    elemNodeName.toLowerCase();
                                    if (
    elemNodeName == "input"
                                    
    || elemNodeName == "select"
                                    
    || elemNodeName == "option"
                                    
    || elemNodeName == "textarea") {
                                        
    self.elementObj.value self.response;
                                    } else {
                                        
    self.elementObj.innerHTML self.response;
                                    }
                                }
                                if (
    self.responseStatus[0] == "200") {
                                    
    self.onCompletion();
                                } else {
                                    
    self.onError();
                                }

                                
    self.URLString "";
                                break;
                        }
                    }; 


  •  

    Posting Permissions

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