Announcement

Collapse
No announcement yet.

Dynamic DropDown Menus?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic DropDown Menus?

    What is the proper code to build dynamic dropdown menus using PHP/MySQL?

    I am trying to build a search page with dropdown menus dynamically build from the options in the table.

    For example, I have over 800 records in a table with a field that is populated by 4 different colors. The field is obviously called 'color' and each record will have only one of 4 colors associated with it:

    red, blue, green, yellow

    I want to build a dynamic dropdown menu on the search page that will have only 4 options in the dropdown menu for color: red, blue, green, yellow.

    No matter what I do, I get 800 colors listed in the dropdown menu.
    Rob

  • #2
    I'm not quite sure what you are trying to do. Can you post the code you are using?

    Comment


    • #3
      Originally posted by DoE View Post
      I'm not quite sure what you are trying to do. Can you post the code you are using?
      Ok....if you have a table of widgets and they are all either red, blue green or yellow, and a user wants to run a search on all the red ones, so they select red from the dropdown menu - that's what I'm trying to do. I only want the options in the select statement to be red, blue, green, yellow:

      PHP Code:
      <select value="color">
      <
      option value="red">red</option>
      <
      option value="green">green</option>
      <
      option value="blue">blue</option>
      <
      option value="yellow">yellow</option>
      </
      select
      I want the options in bold to be dynamically generated based upon the different colors in the table.

      So if down the road I decided to add widgets that are gold, silver, bronze, fuschia, etc....I don't have to go back to the search page to manually add those colors into the dropdown menu.
      Rob

      Comment


      • #4
        your sql command can limit the search to these fields with the selected color (i.e. where color = red)
        while(true){
        if(
        $someone->needsHelp() && $i->canHelp()) $post->help();
        if(
        $i->findBug()) $post->bug();
        }

        Comment


        • #5
          Originally posted by Dody View Post
          your sql command can limit the search to these fields with the selected color (i.e. where color = red)
          Here's the SQL command that I'm using:

          PHP Code:
          $sql="SELECT * FROM `".$tablename."`"
          and here's where I'm building the menu:

          PHP Code:
          <?
                  
          while($row mysql_fetch_assoc($result)){
                     
          $color trim($row["color"]);
                     echo 
          "<option value=\""$color ."\">" $color "</option>\n";
                     }
                  
          ?>
          So do I have to have a separate sql command for each color? I'm going to have about 10 other dropdown menus on this search page, so I'm just wondering if I'm going to have a separate sql command for each one?
          Rob

          Comment


          • #6
            To return those 4 unique colors instead of 800 duplicates, use this query:
            PHP Code:
            $sql "SELECT DISTINCT `color` FROM `$tablename`"
            The joy of an early release lasts but a short time. The bitterness of an unusable system lasts for years.
            GeekPoint Forum | Articles & Reference

            Comment


            • #7
              Originally posted by Shadab View Post
              To return those 4 unique colors instead of 800 duplicates, use this query:
              PHP Code:
              $sql "SELECT DISTINCT `color` FROM `$tablename`"
              This doesn't work. It returns "color" in the dropdown menu.
              Rob

              Comment


              • #8
                you can use DISTINCT or GROUP BY, both will do the magic.
                while(true){
                if(
                $someone->needsHelp() && $i->canHelp()) $post->help();
                if(
                $i->findBug()) $post->bug();
                }

                Comment

                widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                Working...
                X