Php: Set default drop down value in a loop - php

A drop down list is printed in php in a while loop by taking value from database table. Here is the code:
<select name='supervisor' class='form-control' name='supervisor'>
<?php
$sql = "SELECT username FROM system_user where type='supervisor'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}?>
</select>
How can I set a default value for this? There is one 'username' value that i want to make as the default value. How can i do that?

<select name='supervisor' class='form-control' name='supervisor'>
<?php
$sql = "SELECT username FROM system_user where type='supervisor'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$sel = ''; if($row['username'] == 'usename'){ $sel = 'selected'; }
echo "<option $sel value='" . $row['username'] ."'>" . $row['username'] ."</option>";
} ?>
</select>

function dropDown(array $array, $default = null, $select_attrs = '')
{
$s = '<select $select_attr>';
foreach((array)$array as $k => &$v) {
$default = ($v === $default) ? 'selected' : null;
$s.='<option '.$default.' >'.$v.'</option>';
}
return $s;
}

this one worked for me...
<select name='supervisor' class='form-control' name='supervisor'>
<?php
$sql = "SELECT username FROM system_user where type='supervisor'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$sel = ''; if($row['username'] == 'usename'){ $sel = 'selected'; }
echo "<option $sel value='" . $row['username'] ."'>" . $row['username'] ."</option>";
} ?>

Related

how to get value from database and show in dropdown list

I already try many ways but the value didn't show in dropdown list
Here, this is my code. can you suggest me anything that i was wrong
<?php
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
$row = mysqli_fetch_assoc( $result );
$pos = 0;
echo "<select name=Pname >";
while($pos <= count ($row)){
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
$pos++;
}
echo "</select>";?>
And i write as .php file. Thanks for your help.
Try this out:
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output .= '<option value="' . $project_no . '">' . $project_name . '</option>";
}
}
Then inside of your HTML, print your $output variable inside of your <select> element:
<select>
<?php
print("$output");
?>
</select>
It should print all options for every row that you have requested from the database.
Hope this helps :)
Try this:
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
echo "<select name=Pname >";
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
}
echo "</select>";
}
This is the result code that i can run it. I put this code in a form code of html
$result = mysqli_query($con,"SELECT * FROM project"); ?>
<?php
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
echo " <select name = Pname>";
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output = "<option value=" . $project_no . "> ". $project_name ." </option>";
print("$output");
}
echo " </select>";
}
?>
Thank you every one for helping me ^^

Retrieve value from DB and display in a drop down on edit form in php

This script does not display the DB value in a drop down on the edit form.
<?php
echo "<select name='assign' value=''><option>Select name</option>";
while ($r = mysql_fetch_array($result)) {
$value = $r['name'];
echo "<option value=" . $r['emp_id'] . ">" . $r['name'] . " if ($name=='$value') echo 'selected = 'selected''></option>";
}
echo "</select>";
It does not show any error. How it can write in a correct way.
You can try this :
$echoSting = '<select name="assign"><option value="">Select name</option>'.PHP_EOL;
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
$echoSting .= '<option value="'.$r['emp_id'].'" '.($name==$value ? 'selected' : '').'>'.$r['name'].'</option>'.PHP_EOL;
}
$echoSting .= '</select>'.PHP_EOL;
echo $echoSting;
a side note, try looking into PDO for your database stuff : http://php.net/manual/en/book.pdo.php
Try this:
echo "<select name='assign' value=''><option>Select name</option>";
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
echo "<option value='.$r['emp_id'].'>'.$r['name'].' "; if ($name=='$value') echo "selected = 'selected'";echo">$value</option>";
}
echo "</select>";

While Loop entire select Tag

Is it possible to while loop an entire select tag to have multiple dropdown menus?
What I am trying to achieve is to have a column full of dropdown menus in a table.
This is what I have tried so far
<?php
$DNS_FROM = $DNS."_port-%";
$select = "SELECT * FROM `uplink_port_mapping` WHERE DNS_From LIKE '$DNS_FROM'";
$select1 = mysqli_query($conn, $select);
$select2 = "SELECT DNS_From FROM `uplink_port_mapping` WHERE DNS_From NOT LIKE '$DNS_FROM' AND DNS_To = ''";
$select3 = mysqli_query($conn, $select2);
while($uplink_from = mysqli_fetch_assoc($select1)){
echo "<tr>";
echo "<td>".$uplink_from['DNS_From']."</td>";
echo "<td>"."<select name = 'uplink_to' multiple='multiple'>
<option value = '".$uplink_from['DNS_To']."' selected='selected'>". $uplink_from['DNS_To']."</option>";
while ($uplink_to = mysqli_fetch_assoc($select3)){
echo "<option value='".$uplink_to['DNS_From']."'>".$uplink_to['DNS_From']."</option>";
}
echo"</select>";
echo"</td>";
echo"</tr>";
}
?>
How it is right now.
The reason for the second while loop only working once is, that mysqli_fetch_assoc($result) will leave the pointer of the $result recource at it's end.
So when you try to loop the second time, mysqli_fetch_assoc($result) will not return anything (cause it's at the end of the $result recource.
Two possibilites:
Reset the pointer to the beginning:
<?php
....
while($uplink_from = mysqli_fetch_assoc($select1)){
echo "<tr>";
echo "<td>".$uplink_from['DNS_From']."</td>";
echo "<td>"."<select name = 'uplink_to' multiple='multiple'>
<option value = '".$uplink_from['DNS_To']."' selected='selected'>". $uplink_from['DNS_To']."</option>";
// here's the change:
mysql_data_seek($select3, 0);
while ($uplink_to = mysqli_fetch_assoc($select3)){
echo "<option value='".$uplink_to['DNS_From']."'>".$uplink_to['DNS_From']."</option>";
}
echo"</select>";
echo"</td>";
echo"</tr>";
}
....
?>
Or - which I think is the better solution - store that data into an array first, and then walk through that array:
<?php
...
$select2 = "SELECT DNS_From FROM `uplink_port_mapping` WHERE DNS_From NOT LIKE '$DNS_FROM' AND DNS_To = ''";
$result2 = mysqli_query($conn, $select2);
$dns_from = Array();
while ($uplink_to = mysqli_fetch_assoc($select3)){
$dns_from[] = $uplink_to;
}
....
// inside your first while loop:
foreach($dns_from as $dns) {
echo "<option value='".$dns['DNS_From']."'>".$dns['DNS_From']."</option>";
}
....
// note that I left out a bunch of your code, that doesn't change.
?>
Multiple select dropdown basic examples
$array = [
'Apple' => 1,
'Orange' => 0,
'Banana' => 1,
];
echo '<select multiple>';
foreach ($array as $fruit => $sel) {
$selected = 0;
if ($sel == 1) {
$selected = 'selected';
}
echo '<option value="' . $fruit . ' " ' . $selected . '>' . $fruit . '</option>';
}
echo '</select>';

Find previously selected option in while loop

I'm trying to get a drop down menu to keep its selected value when the user hits submit, but it fails due to errors on the form.
I have a while loop returning values from a database to build the options for the drop down, but how do I echo "selected" on the right option?
I have tried if($district == $row["name"]) { echo "selected";} as you see below, but it doesn't work.
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="{$row["name"]}"'; if($district == $row["name"]) { echo "selected";} ; echo '>' . $row["name"] . "</option>";
}
?>
Sorry for the delay. None of the suggested answers worked for me. Any other ideas?
Can you try this,
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
$district = $_REQUEST['name']; // You need pass the value you have been submitted
while ($row = mysql_fetch_array($result)) {
$selected ="";
if(trim($district) == trim($row["name"])) { $selected = "selected";}
echo '<option value="{$row["name"]}" '.$selected.' >' . $row["name"] . "</option>";
}
?>
Try this..
if($district == $row["name"])
{
echo "<option value='$district' selected>$district</option>";
}
I just found the answer. This is what I did:
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row["name"] . '"';
if($row["name"] == $district) { echo 'selected';} ;
echo '>' . $row["name"] . '</option>';
}
It seems to have been this line
echo '<option value="{$row["name"]}"';
that was causing the problem.

Selected value in a list

I'm trying to show the selected value in the list if it's found matching. It's successfully populated but the selected value code does not run.
Code:
$StaffName = 'Jimmy Chan';
<select name="Staff" id="Staff"><?php
$data = array();
$data[0] = '';
echo "<option value='" . $data[0] . "'>" . $data[0] . "</option>";
$result= $DB->query('select No, FirstName, LastName from Staff');
foreach ($result as $data)
{
$SNo = $data['No'];
$SFN = $data['FirstName'];
$SLN = $data['LastName'];
$SName = $SFN.' '.$SLN;
if($SName == $StaffName)
{
echo "<option value='".$Sno."' selected = \"selected\">".$Sname."</option>\n";
}
else
{
echo "<option value='" .$SNo. "'>" . $SName . " </option>";
}
}
?>
</select>
The second else statement do run but not the if statement. I have already put the "selected" inside. Kindly advise.
Try this:
foreach ($result as $data)
{
$SNo = $data['No'];
$SFN = $data['FirstName'];
$SLN = $data['LastName'];
$SName = trim($SFN).' '.trim($SLN);
if(strtolower($SName) == strtolower($StaffName))
{
echo "<option value='".$Sno."' selected = 'selected'>".$Sname."</option>\n";
}
else
{
echo "<option value='" .$SNo. "'>".$SName."</option>\n";
}
}
Also, you have $Sname instead of $SName in first "if" statement. I am not sure if PHP can make a difference on it, but just keep in mind.
The same for $Sno and $SNo variables.
It seems the problem is with the value in variable $SName. Before comparison trim and convert both variables to uppercase or lowercase.
Lower case: http://php.net/manual/en/function.strtolower.php
Uppercase: http://php.net/manual/en/function.strtoupper.php
Trim: http://php.net/manual/en/function.trim.php
Also try,
echo "<option value='".$Sno."' selected="selected">".$Sname."</option>"; //remove \n
You are using
echo "<option value='".$Sno."' selected = \"selected\">".$Sname."</option>\n";
But Variable names are $SNo and $SName and you are using $Sno and $Sname. So Please replace line with line given below.
echo "<option value='".$SNo."' selected = \"selected\">".$SName."</option>\n";
I hope i will be work for you,
thanks

Categories