Javascript Dropdown List - php

I need to populate a drop down list from a Mysql table
my table will be like below
Symbol values
G.n0000 10
P.n0000 20
W.n0000 40
G.n0000 50
P.n0000 60
I need to appear the Symbol values in the drop down box, but only the distinct values
eg: drop down list should show only G.n0000,P.n0000,W.n0000
What I have tried is below but it is not working
<?php
$conn = new mysqli('localhost', 'root', '', 'offlinesurv')
or die ('Cannot connect to db');
$result = $conn->query("select distinct symbol from tab");
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = $result->fetch_assoc()) {
unset($id);
$id = $row['symbol'];
echo '<option value="'.$id.'"></option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
?>

Values do not show in select options.
It's been a while since I did any PHP so I can't recall the syntax, but with straight html you need to put:
<option>Text You Want To See</option>
Value is an optional identifier for each option
So you could have
<option value='id'><Text you want to see</option>

EDIT
while ($row = $result->fetch_assoc()) {
unset($id);
$id = $row['symbol'];
$value = $row['value'];
echo "<option value= '$value' >$id</option>";
}

Related

How to add an additional option to an option form which is filled with a db query

How can I add an extra <option> with a value of all to:
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());
$query = "SELECT name,id,path FROM categories ORDER BY ID DESC LIMIT 0,6";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="categories">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['path']."'>'".$row['name']."'</option>";
}
?>
</select>
I would be grateful for any suggestions
Edit
echo "<th>Tierart";
$query = $pdo->query("select sp_term from species");
//Abfrage der Tabelle Tierart
echo '<select name="sp_term">';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="all">all</option>';
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';
Since you're populating the values of option in a select tag section with a database query doing something as follows would do the trick.
echo '<select name="sp_term">';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';
However, when you want to add an additional item like All as an option, place an explicit option value before the while/after the while depending on your requirement and you'll have the select > option with the new All option added to the options from the database.
echo '<select name="sp_term">';
echo '<option value="all">all</option>';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';

Dropdown list from mysql is empty

I'm trying to create a dropdown list with items from the mysql database. The problem is that my list is empty. I looked around but I could not find a solution.
$q = $db->getQuery(true);
$q->select('#__peaks.peak_name');
$q->from($db->quoteName('#__peaks'));
$db->setQuery($q);
$result = $db->loadColumn();
echo "<select name='peak_name'>";
echo "<option size =30 ></option>";
while($row = mysql_fetch_array($result))
{
echo "<option value='".$row['peak_name']."'>".$row['peak_name']."</option>";
}
echo "</select>";
Where am I wrong? Please help.

Fetch data from MSSQL in dropdown using php

In this code i am trying to fetch city names into the html dropdown. Kindly corrct me if i am wrong anywhere, it give me an error
<?php
$query = Run("select city_name from City");
echo "<select name="city-name" style="width: 210px;">";
while ($row = mssql_num_rows($query))
{
echo "<option>$row->city_name</option>";
}
echo "</select>";
?>
use mysqli_fetch_array($query) insted of mssql_num_rows($query)
try this one
echo "<select>";
while ($row =mysqli_fetch_array($query))
{
echo "<option value='".$row['city_name']."'>".$row['city_name']."</option>";
}
echo "</select>";
mssql_num_rows returns the number of rows in the result set, it doesn't iterate and return individual rows. Try using mssql_fetch_object instead.
If you start a string with " then you have to escape all occurrences of " inside your string or use '.
For example:
echo "<select name=\"city-name\" style=\"width: 210px;\">";
or
echo '<select name="city-name" style="width: 210px;">';
so you don't accidently close the string.
Also like the others pointed out, you have to use
mysqli_fetch_array($query).
Use this
$query = Run("select city_name from City");
echo "<select name='city-name' style='width: 210px;'>";
while ($row = mssql_fetch_object($query))
{
echo "<option>$row->city_name</option>";
}
echo "</select>";
Use This
$query = Run("select `city_name` from City");
echo "<select name='city-name' style='width: 210px;'>";
while ($row = mssql_num_rows($query))
{
echo "<option value='.$row->city_name.'>".$row->city_name."</option>";
}
echo "</select>";

How to get selected value from dropdown list after submitting form in php

I would like to remain my drop down value which I select for submitting after posting the form. My form posts to the same page.
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
echo "Country: <select name='country' value=''>";
while($r = mysql_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id.">".$cname."</option>";
}
echo "</select>"; ?>
Remove your current echo inside the loop and replace it with the following:
if($_POST["country"]==$id)
echo "<option value='".$id."' selected='selected'>".$cname."</option>";
else
echo "<option value='".$id."' >".$cname."</option>";
This will check if the current option being displayed is the one that was submitted and it will select it in that case.
If I understand what you are looking for correctly you need to use the $_POST value of your select to set the selected item...
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
$country = '';
echo "Country: <select name='country'>";
while($r = mysqli_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id;
echo ($_POST["country"]==$id) ? ' selected="SELECTED"' : '';
echo ">".$cname."</option>";
}
echo "</select>"; ?>
Setting selected="SELECTED" for the $id that matches $_POST['country'] will make it the selected item in your dropdown.
And, get rid of mysql* functions and use mysqli* functions instead...

Populate multiple drop-down lists with a single query

I have four drop-down lists that I would like to populate with values from an MSSQL table. All four lists should contain the same values. The query looks like this:
$data = $con->prepare("SELECT ID, Code FROM Table WHERE Code = :value ORDER BY Code");
$input = array('value'=>'value'); //'value' is hardcoded, not a variable
$data->execute($input);
And here is the code for my drop-downs:
<?php
echo "<select name=\"proj1[]\">";
while($row = $data->fetch(PDO::FETCH_BOTH))
{
echo "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
echo "</select>";
?>
This works fine for one drop-down. If I try to create another one (proj2[], proj3[], proj4[]) and apply the same query, however, the PHP page stops loading at that point and the second drop-down does not populate. The only way I've found around it is to copy the query and change the variables ($data becomes $data2 for proj2[], and so on). I'd really rather not have to write the same query four times. Is there a way around it?
$select = '';
while($row = $data->fetch(PDO::FETCH_BOTH))
{
$select .= "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
echo "<select name=\"proj1[]\">";
echo $select;
echo "</select>";
echo "<select name=\"proj2[]\">";
echo $select;
echo "</select>";
//etc...
Why not just put all of it in a veriable and then using it 4 times?
Somthing like this:
<?php
while($row = $data->fetch(PDO::FETCH_BOTH))
{
$options .= "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
for($i = 0; $i <= 4; $i++){
echo "<select name=\"proj1[]\">";
echo $options;
echo "</select>";
}
?>

Categories