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>';
Related
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>";
}
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 ^^
The following worked on a local environment. Now that it's pushed
live everything but these seems to work. Displays completely empty select boxes now no checks or blank labels just empty space in the "select options" drop down.
select display
<?php
$selected = array();
$selected = explode(",",$fill['markets']);
$condb = mysql_query("SELECT * FROM `countries`");
$count = mysql_num_rows($condb);
$countries = array();
$str;
while ($countries = mysql_fetch_array($condb))
{
$str = "option{$countries['id']}";
echo "<option value='{$str}' ";
if(in_array($str,$selected)) {
echo "selected>";
echo $countries['country'];
echo "</option>";
} else {
echo ">";
echo $countries['country'];
echo "</option>";
}
}
?>
You should use while loop instead. You can declare $i outside the loop if you need it, Try the following code:
$condb = mysql_query("SELECT * FROM `countries`");
$i = 0;
while($countries = mysql_fetch_array($condb)) {
$str = 'option' . $i;
echo "<option value='{$str}' ";
if(in_array($str,$selected)) {
echo "selected>";
echo $countries['country'];
echo "</option>";
} else {
echo ">";
echo $countries['country'];
echo "</option>";
}
$i++;
}
?>
</select>
Note:
mysql_* is deprecated as of php-5.5. So instead use mysqli_* or PDO.
Why shouldn't I use mysql_* functions in PHP?
I am populating a drop down menu from mysql database. It works well, But I want it not to repeat values. (i.e if some value is N times in database it comes only once in the drop down list)
Here is my code:
<?php
mysql_connect('host', 'user', 'pass');
mysql_select_db ("database");
$sql = "SELECT year FROM data";
$result = mysql_query($sql);
echo "<select name='year'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['year'] . "'>" . $row['year'] . "</option>";
}
echo "</select>";
?>
Use DISTINCT in your query.
"SELECT DISTINCT year FROM data";
just change Your query. is better
$sql = "SELECT distinct year FROM data";
Another technique:
select year from table group by year
in PHP side you have to do this
$all_data = array();
echo "<select name='year'>";
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
array_push($all_data,$row["column"]);
}
$all_data = array_unique($all_data);
foreach($all_data as $columns => $values){
print("<option value='$value'>$value</option>");
}
echo "</select>";
Here is a simple trick. Take a boolean array. Which value has not come in list print it in list and which value has come in list already once, set it as true through indexing the boolean array.
Set a condition, if boolean_array[ value ] is not true, then show value in list. Otherwise, don't.
mysql_connect('host', 'user', 'pass');
mysql_select_db ("database");
$sql = "SELECT year FROM data";
$result = mysql_query($sql);
echo "<select name='year'>";
while ($row = mysql_fetch_array($result)) {
if($bul[$row['year']] != true){
echo "<option value='" . $row['year'] . "'>" . $row['year'] . " </option>";
$bul[$row['year']] = true;
}
}
echo "</select>";
?>
If in the browser I have parameters like:
http://localhost/specials.php?year=2009&make=honda&model=civic
and the dropdown looks something like this:
<select name="year">
<?php
$query = mysql_query("select distinct year from tbl_content where year = '$year'");
while($row = mysql_fetch_assoc($query)) {
echo "<option value=\"{$row['year']}\">{$row['year']}</option>";
}
?>
</select>
Now what I'm trying to do is show select when the dropdown options value is equal to the parameter year in the browser URL.
I tried this:
<select name="year">
<?php
$query = mysql_query("select * from tbl_year
while($row = mysql_fetch_assoc($query)) {
#=============================
if(isset($_GET['year'])) {
$year = (int)$_GET['year'];
if($year == $row['year'] { $selected = "selected"; }
else { $selected = "";
}
echo "<option value=\"{$row['year']}\" {$selected}>{$row['year']}</option>";
}
?>
</select>
Maybe try "selected='selected'" to make it valid xml.
<select name="year">
<?php
$selectedYear = NULL;
if(isset($_GET['year']))
$selectedYear = (int)$_GET['year'];
$query = mysql_query('SELECT year FROM tbl_year GROUP BY year ORDER BY year ASC');
while($row = mysql_fetch_assoc($query)) {
echo '<option value="' . htmlspecialchars($row['year']) . '"';
if($selectedYear === (int)$row['year']) {
echo ' selected="selected"';
}
echo '>' . htmlspecialchars($row['year']) . '</option>";
}
?>
</select>
Fell free to separate functionality!
<?php
function selectList($name,$values,$labels=null,$selected=null){
if($labels==null) $labels=&$values;
$data="<select name='$name'>";
foreach($values as $k=>$v){
$selected=($v==$selected)?'selected="selected"':false;
$data.="<option value='$v' $selected>".htmlspecialchars($labels[$k])."</option>";
}
$data.="</select>";
return $data;
}
$select=isset($_REQUEST['year'])?(int) $_REQUEST['year']:null;
$query=mysql_query("SELECT DISTINCT `year` FROM `tbl_year` ORDER BY `year`");
while(($row=mysql_fetch_assoc($query))!==false){
$values[]=$row['year'];
}
echo selectList("year",$values,null,$select);
?>