$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 :)
Related
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>";
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
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>";
}
?>
So I have this drop down list in my form which pull "tags" from database as value for drop down options:
<select name="cartags">
<?php $result = mysql_query("SELECT * FROM Products WHERE ID > '0'");
while($row = mysql_fetch_array($result))
{
echo "<option value=\""; echo $row['Tag']; echo "\""; echo ">"; echo $row['Tag']; echo "</option>";
}
?>
</select>
What is my problem? My problem is that I am adding a lot of products into my databas and my code make dropdown list with tags for all this producst even if they have same tag. So what I need is solution how to prevent that same tag appear twice in my drop down.
I am pretty new to PHP and this is my first question here so I really hope that I explained my problem well.
Thanks in advance!
What is the purpose of WHERE ID > '0'? If ID is an auto-increment then it will always be positive. If not, it should be.
Why are you using mysql_fetch_array and then only using the associative keys? You should use mysql_fetch_assoc instead.
Why are you using a new echo every time you want to output a variable? Just concatenate.
Why are you setting the same string in value as the option's text? Without a value, it defaults to the text anyway.
Why are you not using backticks around your column and table names?
Try this instead:
<select name="cartags">
<?php
$result = mysql_query("SELECT DISTINCT `Tag` FROM `Products`");
while(list($tag) = mysql_fetch_row($result)) {
echo "<option>".$tag."</option>";
}
?>
</select>
Try this
<select name="cartags">
<?php $result = mysql_query("SELECT Tag, COUNT(Tag) tg Products WHERE ID > '0' GROUP BY Tag HAVING COUNT(Tag)>0 ORDER BY tg DESC");
while($row = mysql_fetch_array($result))
{
echo "<option value=\""; echo $row['tg']; echo "\""; echo ">"; echo $row['tg']; echo " </option>";
}
?>
</select>
It will also display the top tags that have the most first.
I have this array populating from mysql. But it is not showing me the first record. I can't figure out what the problem is. It should be simple. Here is the code.
$result=mysql_query("SELECT user_instance.instance_name, user_instance.host_name FROM
dba_account, user_instance WHERE dba_account.account_id = user_instance.account_id AND
dba_account.account_id = '1'");
echo mysql_error();
$nume = mysql_fetch_row($result);
while($note = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='instance_name' class='instance_name'
disabled='disabled' value='$note[instance_name]' size='25' /></td>";
echo "<td><input type='text' name='host_name' class='host_name' disabled='disabled'
value='$note[host_name]' size='25' /></td>";
echo "</tr>";
}
You're fetching the first row in $nume = mysql_fetch_row. That pulls the first record. Then you're iterating through the rest of the records in your while loop. Remove that first line.
The first time you call mysql_fetch_row(), it advances the result resource pointer to the second result. Don't do that. Especially considering you are not using the variable $nume in your loop in any way, it is unnecessary and harmful to your logic.
// Don't do this!
//$nume = mysql_fetch_row($result);
// Instead just fetch the loop
while($note = mysql_fetch_array($result)) {
// etc...
}