Populating drop down list in the PHP variable - php

while ($row = mysqli_fetch_array($result)){
$move = '
<select type = text name="mover">
<option>select something</option>
'".while ($dbrow = mysqli_fetch_array($result)){
echo "<option value='".$dbrow['deptname']."'>".$dbrow['deptname']."</option>";}."'
</select>';
echo '<tr><td>'.$row['name'].'</td><td>'.$move.'</td></tr>';}
I am trying to display dropdown list inside the table. However whenever I apply while(...) codes, it displays an error.
shouldn't HTML + '".PHP."' + HTML be the correct way?

Generate the select menu before entering the main loop that builds the table and then rewind the recordset so that the main loop can begin
<?php
$html='<select name="mover">
<option selected disabled hidden>Please select something';
while( $row = mysqli_fetch_array( $result ) ){
$html.=sprintf('<option>%s',$row['deptname'] );
}
$html.='</select>';
$result->data_seek(0);
?>
Then build the table.
<table>
<?php
while( $row = mysqli_fetch_array( $result ) ){
printf('<tr><td>%1$s</td><td>%2$s</td></tr>',$row['deptname'],$html);
}
?>
</table>

Don't concatenate. End your string, start your second while loop.
while ($row = mysqli_fetch_array($result)){
$move = '<select name="mover">
<option>select something</option>';
while ($dbrow = mysqli_fetch_array($result)){
$move .= "<option value='".$dbrow['deptname']."'>".$dbrow['deptname']."</option>";
}
$move .= '</select>';
echo '<tr><td>'.$row['name'].'</td><td>'.$move.'</td></tr>';
}
Removed type=text from your <select>.
Replaced your echos with $move .= ... to add to the $move variable.
Be aware that, depending on the number of items in $row this would leave your HTML with multiple <select>s with the same name.

When you use " the php is resolved so you just need to escape the html "s
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>$row['name']</td><td><select name=\"mover\"><option value=\"\">select something</option>";
while ($dbrow = mysqli_fetch_array($result))
echo "<option value=\"$dbrow['deptname']\">$dbrow['deptname']</option>";
echo "</select></td></tr>";
}

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 ^^

Populate more than one drop down using the same mysql query

I have form with a number of drop boxes which have the numbers 1-5 in them. I can use this code to populate a drop down but was wondering if I can somehow only make the call to the db once but populate all the drop downs that use the same numbers?
<?php
$sql = "SELECT * FROM riskNumDrop";
$result = $conn->query($sql);
if (!$conn->query($sql)) {
echo "query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
echo '<select class="assess" name="precontcons" style="width:4em">' ;
while($row = $result->fetch_assoc()){echo '<option value='. $row['riskNumDrop'] .'>'.$row['riskNumDrop'].'</option>';}
?> </select>
So Ideally, I generate the output once and reuse it multiple times. Im guessing an array (which $result already is) but how do I populate a drop down from it? TIA
Saving as a string would save you the processing of having to loop through the same data generating the same output multiple times. If this is what you want, you could do the following.
Replace:
echo '<select class="assess" name="precontcons" style="width:4em">' ;
while($row = $result->fetch_assoc()){echo '<option value='. $row['riskNumDrop'] .'>'.$row['riskNumDrop'].'</option>';}
?> </select>
with:
$drop = '<select class="assess" name="precontcons" style="width:4em">' ;
while($row = $result->fetch_assoc()){
drop .= '<option value='. $row['riskNumDrop'].'>'.$row['riskNumDrop'].'</option>';
}
$drop .= '</select>';
Then you can echo $drop several times if you want.
If for whatever reason you want different select attributes, you could just save the options list and print the select around that, like this:
$dropOptions = "";
while($row = $result->fetch_assoc()){
$dropOptions .= '<option value='. $row['riskNumDrop'].'>'.$row['riskNumDrop'].'</option>';
}
Then just echo '<select class="foo" name="bar">'.$dropOptions.'</select'>
You could store the data into an array, and then print the data by enumerating the array.
$risks = array();
// Load values into array.
while ($row = $result->fetch_assoc()) {
array_push($row['riskNumDrop']);
}
// Drop down #1
echo '<select>';
foreach ($risks as $risk) {
echo '<option value='. $risk .'>'. $risk .'</option>';
}
echo '</select>';
// Drop down #2
echo '<select>';
foreach ($risks as $risk) {
echo '<option value='. $risk .'>'. $risk .'</option>';
}
echo '</select>';
Simply get the results into an array using the fetch_all method. It returns all rows from the result set into an associative or numeric array. The you can do whatever you want with such array:
$result = $conn->query('SELECT * FROM riskNumDrop');
if (!$result) {
echo "query failed: (" . $mysqli->errno . ") " . $mysqli->error;
exit; // fetch_* functions cannot be called on error (sice $result is false)
}
// get all rows from the result
$rows = $result->fetch_all(MYSQLI_ASSOC);
// output first dropdown...
echo '<select name="dropdown_1">';
foreach ($rows as $row) {
// options for the first dropdown (same as you did before)
}
echo '</select>';
// output second dropdown...
echo '<select name="dropdown_2">';
foreach ($rows as $row) {
// options for the second dropdown
}
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>';

How to create select option from database?

I'm working on a form that has 4 different select elements from 2 tables of a database. I haven't done anything like this and I don't really know how to do it.
I have a table called "students" from I need "name" and "class" and a table called "books" from I need "writer" "title" ... all is one select element and all has more than 2 option values.
I've tried with only one sql query and one select but it shows only one option on the site, wether it has about 6 values in the database.
My code:
$sql = "SELECT class
FROM students";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$select_class = "<option value={$row['class']}>{$row['class']}</option>";
}
<select id="class" name="class">
<?php print $select_class; ?>
</select>
How would it be correct?
try this
$sql = "SELECT class FROM students";
$result = mysql_query($sql);
echo '<select id="class" name="class">';
while ($row = mysql_fetch_assoc($result)) {
echo "<option value={$row['class']}>{$row['class']}</option>";
}
echo '</select>';
You are overwriting $select_class on each while() loop. You need to concatenate $select_class . Change to $select_class .=
$select_class = "";
while ($row = mysql_fetch_assoc($result)) {
$select_class .= "<option value={$row['class']}>{$row['class']}</option>";
}
Changing this:
$select_class = "<option value={$row['class']}>{$row['class']}</option>";
to this:
$select_class .= "<option value={$row['class']}>{$row['class']}</option>";
might solve your problem.
Right now you are constantly resetting the value of $select_class, instead of adding to it. The .= assignment should help you get around this.
As always, be sure to up-vote any StackOverflow answers you find useful.
Try this
<?php
$dbhandle = mysql_connect($hostname, $username, $password) or die("can't connect");
$table = "students";
$sql = "SELECT * FROM students";
$result = mysql_query($sql, $dbhandle);
mysql_data_seek($result, 0);
?>
<form>
<select class="dropdown" name="dropdown">
<?php
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['class'] . '">' . $row['class'] . '</option>';
}
}
?>
</select>
</form>

Edit drop down but not showing selected value from mysql data base in php

I am new to php, i created drop down which calling data from mysql data base, user selects option and its save to data base.
Problem Arises in edit form in which its do not showing selected value.
Drop Down code is below:
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
echo "<option value='$heading'>$heading\n";
}
echo "</select>"
Please advise solution for the edit form.
Thanks in Advance
you must close <option> tag:
echo "<option value='$heading'>$heading</option>";
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
?>
<option <?php if($heading=="SOMETHING") { echo "selected='selected'"; } ?> value="SOMETHING">SOMETHING</option>
<option <?php if($heading=="SOMETHING2") { echo "selected='selected'"; } ?> value="SOMETHING2">SOMETHING2</option>
<option <?php if($heading=="SOMETHING3") { echo "selected='selected'"; } ?> value="SOMETHING3">SOMETHING3</option>
<?php
}
echo "</select>"
I'd do it this way.
$numrows = mysql_num_rows($result);
if ($numrows != 0){
echo "<select name='owner'>\name";
while ($x = mysql_fetch_assoc($result)){
echo "<option value='".$x['heading']."'>".$x['heading']."</option>";
}
echo "</select>";
}
$x['heading'] is using the value of the row 'heading' in the database
It's much more efficient and simply looks more sophisticated.

Categories