8 Replies Latest reply: Jul 22, 2014 11:25 PM by osgood_ RSS

    How to Create a Dropdown List with Information from Database Table

    Max Resnikoff Community Member

      Hi,

       

      I was wondering if someone could help me.

       

      I want to give a user the option to select a record from a different table.

       

      I have tried to look for tutorials online but have seemed to failed to do so.

       

      Any help would be very much appreciated.

       

      Thank you

        • 1. Re: How to Create a Dropdown List with Information from Database Table
          Rob Hecker2 CommunityMVP

          Here is an example:

           

          First, to start the dropdown: $reportchoice.="<p>Course: <select name='course_id'>";

           

          Now for the items returned from the database, perform something like the following on the result set:

          while($result = $sql->fetch(PDO::FETCH_ASSOC)){

          $course_names=$result['title'];

          $courseids=$result['course_id'];

          $reportchoice.= "<option value='$courseids'>$course_names</option>";

          $startdate=NULL;

          }

          Finally, close the select statement

          $reportchoice.='</select>';

           

          now you can echo the list where you need it:

          echo $reportchoice;

           

          Note that if you are not using PDO, the "while" statement will be different for you. Use the right syntax for your database connection method.

          Also note that "dot equal" allows you to build up a variable You could alternatively just echo each piece. I generally find building up the variable cleaner.

          • 2. Re: How to Create a Dropdown List with Information from Database Table
            osgood_ CommunityMVP

            Max Resnikoff wrote:

             

            I want to give a user the option to select a record from a different table.

             

             

            When you say 'different' table this infers you want to provide an option to the user to choose from the different tables in your database?

             

            If so you can use "show_tables".

             

            Below is an example using mysqli and 'show tables"

             

            Obviously you need to change the database connection details to your own in the $conn string below. The code will get all the tables in your database and insert them into a select list.

             

            You will then need to retrieve the select list 'value' ($table_name = $_POST['tableList'];) see below and use that to grab the information from the selected database table.

             

             

             

            <?php

            // connect to database

            $conn = new mysqli('localhost' , 'username' , 'password' , 'database_name');

            ?>

            <?php

            // get table names from database

            $listTables = $conn->query("show tables") or die($conn->error);

             

            // get table names from select list

            $table_name = $_POST['tableList'];

            ?>

             

            <!DOCTYPE HTML>

            <html>

            <head>

            <meta charset="UTF-8">

            <title>List Table</title>

            </head>

             

            <body>

            <h2>Table List</h2>

            <form name="listTables" method="post" action="">

             

            <select name="tableList" id="tableList">

            <option value="Choose Table">Choose Table</option>

            <?php

            while($printTables = mysqli_fetch_array($listTables)) {

            echo '<option value="'.$printTables[0].'">'.$printTables[0].'</option>';

            }

            ?>

            </select>

            <input name="submit" type="submit" value="Submit">

            </form>

            <?php

            echo $table_name;

             

            ?>

             

            </body>

            </html>

            • 3. Re: How to Create a Dropdown List with Information from Database Table
              osgood_ CommunityMVP

              Rob Hecker2 wrote:

               

              Also note that "dot equal" allows you to build up a variable You could alternatively just echo each piece. I generally find building up the variable cleaner.

               

              I like that idea, it never crossed my mind before (except when building an email message) - I'll see if I can weave that into some future work.

              • 4. Re: How to Create a Dropdown List with Information from Database Table
                Rob Hecker2 CommunityMVP

                I use the method often in order to keep the PHP heavy lifting away from the layout code.You can pack a lot into a single variable.

                • 5. Re: How to Create a Dropdown List with Information from Database Table
                  Max Resnikoff Community Member

                  Hi,

                   

                  Thank you for your suggestions.

                   

                  I found that this code was the best way to do it. (In My Opinion):

                   

                  <select name="category" class="formfield" id="category">        

                                   <option id="0">-- Select Category --</option>

                                  <?php

                                      require("Connections/connect_database.php");

                                      $getallequipcategories = mysql_query("SELECT * FROM equipcategories");                   

                                      while($viewallequipcategories = mysql_fetch_array($getallequipcategories)){

                                  ?>

                                  <option id="<?php echo $viewallequipcategories['id']; ?>"><?php echo $viewallequipcategories['Category'] ?></option>

                                  <?php } ?>

                            </select>

                  • 6. Re: How to Create a Dropdown List with Information from Database Table
                    Rob Hecker2 CommunityMVP

                    Yes, if that code does it for you Max, then good. It's a slightly different approach to the same method. But you want to use option value=, not option id.

                    • 7. Re: How to Create a Dropdown List with Information from Database Table
                      Max Resnikoff Community Member

                      Ok,

                      May I ask what the Difference is?

                      • 8. Re: How to Create a Dropdown List with Information from Database Table
                        osgood_ CommunityMVP

                        Max Resnikoff wrote:

                         

                        Ok,

                        May I ask what the Difference is?

                         

                        Without a 'value' the option tag is useless UNLESS you just want your end-user to select a 'category' and do nothing else. If you intend to develop this further you need to give the option tags a value because the 'value' is used to return other data from the database based on the value.

                         

                        You could go on and get the required details of the equipment (based on the option value selected by the user) :

                         

                        $category = $_POST['category'];

                        SELECT * FROM equipcategories WHERE category = $category