0

I tried to create a simple select dropdown menu from MySQL database. However, it does not work on my code.

Here is my code:

            <?php
              mysql_select_db($database_conn, $conn);
              $query_RsCourse = "SELECT * FROM tbl_course ORDER BY courseid DESC";
              $RsCourse = mysql_query($query_RsCourse, $conn) or die(mysql_error());
              $totalRows_RsCourse = mysql_num_rows($RsCourse);
              $count=0;
              while ( $row = mysql_fetch_array($RsCourse, MYSQL_ASSOC)) {
              $courseid=$row["courseid"];
         $count++;  
              }
             ?>

            <tr>
            <td bgcolor="#CCCCCC">&nbsp;Course Name</td>
            <td bgcolor="#dfdfdf">&nbsp;<select name="courseid">
            <option value="" SELECTED>Selected Course</option>      
            <option value="<?php echo $courseid; ?>"><?php echo $row_RsCourse['$courseid']; ?></option>
            </select>
            </td>
            </tr>

Any advice will be appreciated!

3
  • You are overwriting $courseid in your while loop before building your select options, so you will only get the last value of $courseid Commented Nov 29, 2013 at 2:25
  • So What I can do on my code? Thanks. Commented Nov 29, 2013 at 2:37
  • Where are you getting $row_RsCourse['$courseid']; from? Commented Nov 29, 2013 at 2:43

2 Answers 2

0
 <?php

 echo '<tr>
        <td bgcolor="#CCCCCC">&nbsp;Course Name</td>
        <td bgcolor="#dfdfdf">&nbsp;';

        mysql_select_db($database_conn, $conn);
        $query_RsCourse = "SELECT * FROM tbl_course ORDER BY courseid DESC";
        $RsCourse = mysql_query($query_RsCourse, $conn) or die(mysql_error());
        $totalRows_RsCourse = mysql_num_rows($RsCourse);
        if($totalRows_RsCourse)
        {
        echo '<select name="courseid"><option value="" SELECTED>Selected Course</option>';
            $count=0;

            while ($row = mysql_fetch_array($RsCourse, MYSQL_ASSOC))
            {
            $count++;
            echo '<option value="'.$row['courseid'].'">'.$row['courseid'].'</option>';
            }
            echo '</select>';
        }
        else
        {
        echo 'No courses to show yet.'; // no rows in tbl_course
        }
        echo '</td>
            </tr>';

?>

That was a mess, but hope you can go on from these new codes. Enjoy.

PS: this part >'.$row['courseid'].'</option> u can change to new one according to your table structure which one is not shown here.

<?php

 echo '<tr>
        <td bgcolor="#CCCCCC">&nbsp;Course Name</td>
        <td bgcolor="#dfdfdf">&nbsp;';

        mysql_select_db($database_conn, $conn);
        $query_RsCourse = "SELECT * FROM tbl_course ORDER BY courseid DESC";
        $RsCourse = mysql_query($query_RsCourse, $conn) or die(mysql_error());
        $totalRows_RsCourse = mysql_num_rows($RsCourse);


        echo '<select name="courseid"><option value="" SELECTED>Selected Course</option>';
            $count=0;

            while ($row = mysql_fetch_array($RsCourse, MYSQL_ASSOC))
            {
            $count++;
            echo '<option value="'.$row['courseid'].'">'.$row['courseid'].'</option>';
            }
            echo '</select></td>
            </tr>';

?>
Sign up to request clarification or add additional context in comments.

6 Comments

Yep. I tried to echo "<option>$courseid</option>"; it is able to show up on screen. However, it is unable to come up on select drop down menu.
Yeah I expected that, that is why I gave you that code. Well, do you have ANY rows/data in your tbl_course????? Can you see any data via phpmyadmin in there???
Is it able to do the code seperately with while command? I have form to post in the other php pager. Thanks.
This code is operational Mahou. If you see "No courses to show yet" that means: a) you dont have any data in "tbl_course", or b) it is mysql query error. But here is code bellow with clean "while".:P
Thanks for your reply. I copied your code and pasta to create new one. It is working fine. However, I tried to work on seperate code from your code. It does not work. The table is including data. I am trying to fix up this issue now.
|
0

You can store everything in a buffer and print at once in the select below:

    <?php
      mysql_select_db($database_conn, $conn);
      $query_RsCourse = "SELECT * FROM tbl_course ORDER BY courseid DESC";
      $RsCourse = mysql_query($query_RsCourse, $conn) or die(mysql_error());
      $coursesHtml = "";
      while ( $row = mysql_fetch_array($RsCourse, MYSQL_ASSOC)) {
        $coursesHtml .= "<option value='{$row["courseid"]}'>{$row["coursename"]}</option>";
      }
     ?>

    <tr>
    <td bgcolor="#CCCCCC">Course Name</td>
    <td bgcolor="#dfdfdf">
    <select name="courseid">
    <option value="" SELECTED>Selected Course</option>      
    <?= $coursesHtml  ?>
    </select>
    </td>
    </tr>

PS: Avoid use &nbsp; , style your html well with css using padding-left: 5px; or other features;

PS2: You should not show your page/form with tables structure, use divs with flexbox.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.