Set HTML dropdown options from PHP variable - php

I want to set the on a HTML dropdown menu from a php variable. I give you my code so you can see I want to do:
<?php
$html_table = '
<table border="0" cellspacing="0" cellpadding="0"><tr>';
while($arr = pg_fetch_array($result1))
{
$html_table .= "<tr><td> $arr[0] </td></tr>";
}
$html_table .='</tr>';
?>
<p>
<select name="db" size="1">
<option> $html_table </option> #### <- that is my question, how to get that working
</select>
</p>
I hope you understand what I want to do. If you know about nicer ways, let me know.
Cheers

<?php
$options = '';
while($arr = pg_fetch_array($result1)) {
$options .= '<option>'.$arr[0].'</option>';
}
?>
<p>
<select name="db" size="1">
<?php echo $options; ?>
</select>
</p>

Just replace $html_table with <?php echo $html_table; ?> and you're good to go - from the PHP point of view that is. HTML is of course invalid, as pointed out by others.

First: you didn't close your <table> tag.
Second: you can't paste<table> tag into <select>. You should put a series of <option> elements, each with one element of the array.
Third: To print PHP variable you should do like this:
<option><?php echo $html_table; ?></option>
But again: it will break your HTML, because table can't be inside select option.
The best solution is:
<select name="db" size="1">
<?php while ($arr = pg_fetch_array($result1)): ?>
<option><?php echo $arr[0]; ?></option>
<?php endwhile; ?>
</select>

so set your option elements like this
<option value="<?php echo $html_table; ?>"><?php echo $html_table; ?></option>
just know, an option can't be a table.

Related

multiple option echo php & mysql

how can i output more when 1 array. when i delete , ["nr"] evrything works. but i need the second one too...
<div>
<select name="name[]">
<option value = ""> Select the person with phone nbr. </option>
<?php foreach ($results as $output) {?>
<option> <?php echo $output ["fname"],["nr"]; ?> </option>
<?php } ?>
</select>
</div>
does "nr" is an index of $output array? if so, then you have to write it like this:
<?php echo $output["fname"] . "," . $output["nr"]; ?>

Passing PHP variable to HTML code

I am trying to pass the variable from my database to an HTML code but it isn't working. If I enter in a numeric value for my Select option it will work but it doesn't work with me trying to reference my class_id. The code I'm basing mine off of is found here. I am writing this code on wordpress also.
global $wpdb;
$result = $wpdb->get_results("SELECT course FROM class;");
echo "
<form>
<select name='department' onchange ='showUser(this.value)'>";
foreach($result as $results){
echo "<option selected value='$results->class_id'>$results->course</option>";
}
echo" </select>
</form>";
Try not to mix your HTML and PHP together, I've fixed a few things for you below. You need to select class_id in your query to use it as the option value, you can't use selected on all options as well. You may want to check if there are actually any results before looping over them too.
<?php
global $wpdb;
$results = $wpdb->get_results("SELECT class_id, course FROM class;");
?>
<form>
<select name="department" onchange="showUser(this.value)">
<?php foreach( $results as $result ): ?>
<option value="<?= $results->class_id ?>"><?= $results->course ?></option>
<?php endforeach ?>
</select>
</form>
global $wpdb;
$result = $wpdb->get_results("SELECT course FROM class;");
echo '<form><select name="department" onchange="showUser(this.value)">';
foreach($result as $results){
echo '<option selected value="'.$results->class_id.'" >'.$results->course.'</option>';
}
echo '</select></form>';
it is easy and simple for understanding use this code it's working prefect
<?php global $wpdb; $result = $wpdb->get_results("SELECT course FROM class;"); ?>
<form>
<select name='department' onchange ='showUser(this.value)'>
<?php foreach($result as $results){ ?>
<option selected value='<?php echo $results->class_id; ?>'><?php echo $results->course; ?> </option>
<?php } ?>
</select>
</form>

Repitition of selected item in dropdownlist in PHP

I have this problem that the selected value in my dropdownlist is showing twice. What should i do so that it will show only once.
<td>
<select class="span8" style="width:100%" name="from_time[]" id="from_time<?php echo $count;?>" value="start_from" onchange="calculate_by_ajax(this.id);">
<option value="<?php
$start_from=$jobs_result['start_from'];
//$date_arr_to= explode(" ", $start_from);
//$date_to= $date_arr_to[0];
//$time_to= $date_arr_to[1];
$sec="SELECT DATE_FORMAT('$start_from', '%H:%i') as tp";
$sec_exe=mysql_query($sec);
$sec_res=mysql_fetch_array($sec_exe);
?>" selected="selected">
<?php echo $sec_res['tp']; ?></option>
<?php include("list.php"); ?>
</select>
</td>
Got it, instead of modifying one of the available options and marking it as selected, you actually add another option with your code.
You could change list.php to look like this (i.e. an array of all the times)
<?php
$drop_down_values = array('11:35', '11:36', '11:27');
?>
Then your code should look like this:
<?php
include("list.php");
$start_from=$jobs_result['start_from'];
$sec="SELECT DATE_FORMAT('$start_from', '%H:%i') as tp";
$sec_exe=mysql_query($sec);
$sec_res=mysql_fetch_array($sec_exe);
?>
<td>
<select class="span8" style="width:100%" name="from_time[]" id="from_time<?php echo $count;?>" value="start_from" onchange="calculate_by_ajax(this.id);">
<?php foreach ($drop_down_values as $value) {
if ($value == $sec_rec) {
echo "<option selected=\"selected\">$value</option>";
} else {
echo "<option>$value</option>";
}
}
?>
</select>
</td>

In PHP i'm not getting desire id it's returning me as 0

here is my piece of code
i'm encountering a problem,,, below are my subject and test table in database. when i select a subject from dropdown menu on the" ADDTEST.php" page and want to add in Test table it couldn't get sub_id from "Subject Table " and it take "Sub_id" as 0 ...
'ADDTEST.PHP'
<td>Select Subject</td>
<td><select name="sub_id">
<?php
include ("database.php");
$rs=mysql_query("Select * from mst_subject");
while($row=mysql_fetch_array($rs))
{
$sub_id=$row['sub_id'];
?>
<option> <? echo $row['sub_name']; ?></option>
<?
}
?> </select>
<?php
include ("database.php");
if($_POST[submit]=='Save' || strlen($_POST['sub_id'])>0 )
{
$testname=$_POST['test_name'];
$totque=$_POST['totque'];
$sub_id=$_POST['sub_id'];
mysql_query("insert into mst_test(sub_id,test_name,total_que) values
('$sub_id','$testname','$totque')") or die(mysql_error());
echo "<p align=center>Test <b>\"$testname\"</b> Added Successfully.</p>";
unset($_POST);
}
}
?>
Please replace
<option> <? echo $row['sub_name']; ?></option>
with
<option value="<? echo $sub_id; ?>"> <? echo $row['sub_name']; ?></option>
In $_POST['sub_id'] you get the value attribute(s) of the select -> option tags.
Please consider also moving from mysql_ php functions to mysqli or pdo, if possible since mysql_ is marked in php as deprecated and, even from my own experience, may occur problematic and too limited.
Try this
<option value="<?=$sub_id?>"><?=$row['sub_name']?></option>
Instead of
<option> <? echo $row['sub_name']; ?></option>

Remember selected option within select menu

I've googled a lot and found alternative solutions but in my case things are a bit different.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<?php endforeach; ?>
</select>
I need to display the selected option after a POST request in between the option tags but since I already have a value for that I cannot find a way to do so. The idea is that I have one form with a couple of select menus. From the first one I select a database. The second one is for selecting a table from the already chosen database and another select menu for the columns. The problem is that I'm sending a new request for both the database and table and the chosen database cannot be remembered (it just 'resets' the select menu and starts from the first value).
Here's the whole code
Right now I need to reselect the database which I've previously chosen in order to display the columns from a table.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?= $row; ?>"
<?php if ($row == $_POST['database']){echo " selected";}?>>
<?= $row; ?>
</option>
<?php endforeach; ?>
</select>
Wouldn't this work?
$dbms=$_POST['database'];
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"
<?php if ($row == $dbms) echo " selected"; ?>
> <?php echo $row; ?></option>
<?php endforeach; ?>
</select>

Categories