'Select option from dropdown menu with PHP and mySql
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"> Course Name</td>
<td bgcolor="#dfdfdf"> <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!
Solution 1:[1]
<?php
echo '<tr>
<td bgcolor="#CCCCCC"> Course Name</td>
<td bgcolor="#dfdfdf"> ';
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"> Course Name</td>
<td bgcolor="#dfdfdf"> ';
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>';
?>
Solution 2:[2]
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
, 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | capcj |