Dropdown list from mysql is empty - php

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.

Related

fetch data from db and display in dropdown

Would like to fetch the details from table and display in dropdown but below code displays only dropdown without any data.not sure whats worng.
<?php
include 'config.php';
$sql = "select name from finance";
$result = mysqli_query($con,$sql);
echo "<select name='name'>";
while ($row = mysqli_fetch_row($result)) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
echo "</select>";
?>
can some please advice.
mysqli_fetch_row does not return associated array.
Instead, Use mysqli_fetch_array
Also, check if your query is returning any record

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>";
}
?>

Javascript Dropdown List

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>";
}

mysql select distinct query in PHP

$sql = "SELECT DISTINCT Branch FROM student_main";
$result = mysql_query($sql);
$row_num = mysql_num_rows($result);
$rows = mysql_fetch_array($result);
echo "<select name='Branch'>";
for($i=0;$i<=$row_num-1;$i++){
echo "<option value='".$rows[$i]."'>".$rows[$i]."</option>";
}
echo "</select>";
echo "<input type='submit' Value='submit' />";
echo "</form>";
I am trying to create a dropdown using the above code for my form. But its not working. There are 3 distinct values in the Branch column but in the dropdown, it shows only one value(the first one) and the next two as blank values.
However when in echo $row_num, its shows 3.
Thats means its fetching the three rows, but then why its not showing in the dropdown list.
If I run the same query in phpmyadmin it shows the correct answer i.r it returns 3 distinct Branch values.
You should do something like this:
$sql = "SELECT DISTINCT Branch FROM student_main";
$result = mysql_query($sql);
echo "<select name='Branch'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
echo "</select>";
echo "<input type='submit' Value='submit' />";
echo "</form>";
you need to mysql_fetch_array() for each row. That function returns an associative array for one row only. just include it inside your for loop just above your echo statement.
edit: mysql_fetch_array() actually returns an array (by default) that has associative indices and numbered indices. You can continue using it the same way, though.
You need to loop through your query using the following:
$sql = "SELECT DISTINCT Branch FROM student_main";
$result = mysql_query($sql);
echo "<select name='Branch'>";
while($rows = mysql_fetch_array($result)){ // should probably use mysql_fetch_assoc()
echo "<option value='".$rows['Branch']."'>".$rows['Branch']."</option>";
}
echo "</select>";
echo "<input type='submit' Value='submit' />";
echo "</form>";
mysql_fetch_array only returns the current dataset as an array, and moves the internal pointer ahead. You need to repeatedly call mysql_fetch_array to get all results.
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row['Branch']."'>".$row['Branch']."</option>";
}
There is a problem in the loop using a while loop:
while($rows=mysql_fetch_array($result)){
echo "<option value='".$rows[$i]."'>".$rows[$i]."</option>";
}
Try this
What you really need is to learn how to use templates.
But it seems Stackoverflow is definitely not the place where one can learn professional ways of website developing.
get your data first
$select = $array();
$sql = "SELECT DISTINCT Branch FROM student_main";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
while($row = mysql_fetch_array($res)) $select = $row[];
And then use it in the template
<form>
<select name='Branch'>
<? foreach($select as $row): ?>
<option value="<?=htmlspecialchars($row['Branch'])?>">
<?=htmlspecialchars($row['Branch'])?>
</option>
<? endforeach ?>
</select>
<input type='submit' Value='submit' />
</form>
mysql_fetch_array will only return the first row...
see here for full details :)

Categories