Populate Select Drop Down from a Database Table

Populate select drop down from a database table

$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";

$res = mysql_query($query);
echo "<select name = 'venue'>";
while (($row = mysql_fetch_row($res)) != null)
{
echo "<option value = '{$row['venue_id']}'";
if ($selected_venue_id == $row['venue_id'])
echo "selected = 'selected'";
echo ">{$row['venue_name']}</option>";
}
echo "</select>";

Populating table with select dropdown (from database)

I think first of all you should change your foreach() statement into:

<?php foreach($users as $user) { ?>
<option value="<? echo $user['id']; ?>"><? echo $user['pref']; ?> - <?echo $user['nome']; ?></option>
<?php }; ?>

I find this a bit more clean and readable :)

Then you will need to enable the page to "filter" the rows by select - this can be done either by posting the select contents each time you change the select value (so basically you create a form that will submit itself every time you change what is in select - not very modern solution) or use AJAX to send the contents and retrieve the results.

If you decide to go second option (which I would highly recommend) you should take a look at some tutorials on changing page content basing on AJAX response. I would recommend to go to jQuery for that - since you should find quite some functions there that would help you out...

Populate dropdown from Another MySQL table - php

This is assuming you have an object $student which is the row from students corresponding to the current user.

$dropdown_query = "SELECT * FROM course";
$courses = mysqli_query($con, $dropdown_query);

echo '<select name="course">';
while ($course = mysqli_fetch_array($courses)) {
echo "<option value='{$course['id']}'".($student['course']==$course['id'] ? ' selected="selected"' : '').">{$course['course_name']}</option>";
} // while
echo '</select>';

How to populate dropdown menu from sql table in PHP

Try saving the result of mysqli_query:

$result = mysqli_query($db, $sql);

And then using it in the while condition:

while ($row = mysqli_fetch_array($result)) {

The select should be also "Select unitid, unitname ..." to return also the unitname used in the options:

$sql = "SELECT unitid, unitname FROM unit WHERE unitname = '$unitname'";

And you should use prepared statements if you want to prevent it from SQL injection attacks.

If you want all the units to be shown on the combo change the select to:

$sql = "SELECT unitid, unitname FROM unit";

So, the code should be now:

 <?php
$db = mysqli_connect('localhost', 'root', '', 'registration');
$sql = "Select unitid, unitname from unit";
$result = mysqli_query($db, $sql);

echo "<select name='unitid'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" .$row['unitid']."'> ".$row['unitname'] . "</option>";
}
echo "</select>";
?>

Populating a Dropdown Selection from MySql & Php

Heres the code thats working

//placed in beginning of code
<?php
include("database.class.php");
$database = new Database;

$result = $database->mysqlQuery("SELECT * FROM spec_tables");

?>
//Placed inside html form
<?php
echo '<select name="list">';
echo '<option>Select List</option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['name'] .'">' . $row['name'] .'</option>';
}
echo '</select>';

?>

Populate dropdown from database and set default value

You can do that like given below:

<?php

$result = $mysqli->query("select * from listoption");
$id = ($_GET['id'])? $_GET['id'] : '';

echo "<select id='list' name='list'>";

while ($row = $result->fetch_assoc()) {

$listoption_item = $row['listoption_item'];
$sel = ($id == $row['id'])? 'selected="selected"':'';

echo '<option value="'.$listoption_item.'" '.$sel.'>'.$listoption_item.'</option>'; // $sel will deside when to set `selected`

}

echo "</select>";

?>

Populating Second Dropdown Based on first dropdown from the database table without using javascript

You cant do this only with PHP. You have to use jquery OR Ajax to do this.
Please check this example page . This may help you

https://www.tutorialrepublic.com/faq/populate-state-dropdown-based-on-selection-in-country-dropdown-using-jquery.php

OR

https://www.codexworld.com/dynamic-dependent-select-box-using-jquery-ajax-php/



Related Topics



Leave a reply



Submit