Using PHP to populate a select /select dropdown?
What about something like this :
echo '<select name="select">';
while($row=mysql_fetch_array($result))
{
echo '<option value="' . htmlspecialchars($row['column_for_value']) . '">'
. htmlspecialchars($row['column_for_label'])
. '</option>';
}
echo '</select>';
Of course, up to you to decide which items from $row
should be used for the value and the text of each <option>
Just make sure you are escaping the data that comes from your DB -- especially if it can contain HTML ; as you are outputting HTML, this can be done with htmlspecialchars
or htmlentities
.
Note that those might take a couple of additionnal parameters that I didn't use in my example -- setting those can be useful, depending on the charset you're using.
How to populate old select option values?
you can do like this
@if(count($category_list)>0)
<option value="null">Pick a Category</option>
@foreach($category_list as $cl)
<option value="{{$cl->id}}" @if(old('category_id') == $cl->id) selected="selected" @endif>{{$cl->name}}</option>
@endforeach
@else
<option>No categories found</option>
@endif
How to use PHP to populate a select Dropdown with json data
you can use '
and "
together:
<select id="printer" name="printer">
<option value="none">Select Printer</option>
<?php
foreach($test as $t) {
echo '<option value="'. $t['Name']. '">'. $t['Name'].'</option>';
}
?>
</select>
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 dropdown - PHP Ajax MySQL
If you want a more dynamic solution (that will accommodate changes to the background DB) you can do something like this on your page:
<script>
function changeSecond(first){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var res=xmlhttp.responseText;
document.getElementById("second").innerHTML=res;
}
}
xmlhttp.open("GET","second_script.php?first="+first,true);
xmlhttp.send();
}
</script>
...
<select onChange="changeSecond(this.value)">
<option value="Degree">Degree</option>
<option value="City">City</option>
</select>
<div id="second"><select><option value=""></option></select></div>
and then a script similar to:
<?php
//database connection
$first=mysql_real_escape_string($_REQUEST["first"]);
$query="SELECT ".$first." FROM tablename GROUP BY ".$first;
$data=mysql_query($query);
echo "<select>";
while($row=mysql_fetch_row($data)){
echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
}
echo "</select>";
?>
I guess for real flexibility you'd also want to dynamically populate that first one using mysql_field_name in another script similar to above
Laravel-5 how to populate select box from database with id value and name value
Laravel provides a Query Builder with lists() function
In your case, you can replace your code
$items = Items::all(['id', 'name']);
with
$items = Items::lists('name', 'id');
Also, you can chain it with other Query Builder as well.
$items = Items::where('active', true)->orderBy('name')->lists('name', 'id');
source: http://laravel.com/docs/5.0/queries#selects
Update for Laravel 5.2
Thank you very much @jarry. As you mentioned, the function for Laravel 5.2 should be
$items = Items::pluck('name', 'id');
or
$items = Items::where('active', true)->orderBy('name')->pluck('name', 'id');
ref: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0 -- look at Deprecations lists
Populate data from database in Select tag
This should do the trick:
<?php
$q1 = "select * from bloodbank_facility";
$options=[];
$q = $conn->query($q1);
while($row1 = $q->fetch_array($q1)){
$options[] = "<option>{$row1[1]}</option>";
}
?>
<select name="facility_name" required>
<?php echo implode($options) ?>
</select>
The main thing you didn't do was have the curly braces around $row1[1] which meant PHP thought you just wanted $row1 rather than the array value.
Notice: I already fixed it and thank you for your help. This is the solution that I came up with.
<select name="facility_name" required style="width:260px;">
<?php
$q1 = $conn->query("SELECT bloodbank_name FROM bloodbank_facility");
?>
<?php
while($rows = $q1->fetch_assoc()){
$facility_name= $rows['bloodbank_name'];
echo "<option value='$facility_name'>$facility_name</option>";
}
?>
</select>
Related Topics
Ternary Operator Left Associativity
Create a Dynamic MySQL Query Using PHP Variables
Fatal Error: Using $This When Not in Object Context
Laravel Recursive Relationships
PHP Replacing Special Characters Like à->A, è->E
Dynamic Static Method Call in PHP
Extract Text from Doc and Docx
Token Must Be a Short-Lived Token and in a Reasonable Timeframe
Joomla 3.2.1 Password Encryption
Why Doesn't File_Get_Contents Work
How to Use Spl_Autoload() Instead of _Autoload()
Install Imagick for PHP and Apache on Windows
How to Set Default Value for Form Field in Symfony2
PHP - Remove <Img> Tag from String