I'm trying to populate drop down option with the data from table :
<?
$sqloption="SELECT name FROM user";
$resultoption=mysql_query($sqloption);
$options="";
while ($row=mysql_fetch_array($resultoption))
{
$nameoption[]=$row['name'];
$options.="<OPTION>".$nameoption</option>";
}
?>
<SELECT>
<OPTION>Choose user<?=$options?>
</SELECT>
The button is getting displayed but it has no options. How do I correct this?
Why are you storing them in an another array? Fix the errors. Just do -
PHP
while ($row=mysql_fetch_array($resultoption))
{
$options.= "<OPTION>". $row['name'] ."</OPTION>";
}
HTML
<SELECT>
<OPTION>Choose user</OPTION>
<?=$options?>
</SELECT>
You can populate the dropdown menu using many method:
mysql_fetch_array() fetches a result row as an associative array, a numeric array, or both. It returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how $result_type is defined.
<?php
$sql="SELECT name FROM user";
$result=mysql_query($sql);
?>
<select name="user">
<?php while ($row=mysql_fetch_array($result)){ ?>
<option value="some_value"><?php echo $row['name'];?></option>
<?php } ?>
<select>
mysql_fetch_assoc() fetches a result row as an associative array. (column names as key)
<?php while ($row=mysql_fetch_assoc($result)){ ?>
<option value="some_value"><?php echo $row['name'];?></option>
<?php } ?>
mysql_fetch_object() fetches the result row as an object.
<?php while ($row=mysql_fetch_object($result)){ ?>
<option value="some_value"><?php echo $row->name;?></option>
<?php } ?>
<?
$sqloption="SELECT name FROM user";
$resultoption=mysql_query($sqloption);
$options="";
while ($row=mysqli_fetch_array($resultoption)) {
$nameoption[]=$row['name'];
$options.="<OPTION>".$row['name']."</option>";
}
?>
<SELECT>
<OPTION>Choose user<?=$options?>
</SELECT>
You've forgotten a "." after $nameoption.
And by the way, you cannot do $options.="<OPTION>".$nameoption</option>";
because it's an array. PHP can't print an array() directly.
I've kept your $nameoption if you need it. Also, I suggest switching from mysql to mysqli because mysql_ is deprecated.
Related
I have two drops down and I am able to display the records in one dropdown from a database. I am getting the second drop down list bank.I have to display same records in my second drop down but it is not displaying. If I delete first drop down then records are displaying in second drop down.I haven't change any think just copy paste the code.Would you help me in this?
<?php
$sql_emails="SELECT * FROM request";
$result_emails = $conn->query($sql_emails);
//first drop down
?>
<select class="selectpicker" name="first" data-live-search="true">
<option style="color:#bbb;" value="" disabled selected>Select emails</option>
<?php while($row = mysqli_fetch_array($result_emails)) { ?>
<option value="<?php echo $row['Email'];?>"><?php echo $row['Email'];?></option>
<?php } ?>
</select>
<?php mysqli_data_seek( $result_emails, 0 ); ?>
<select class="selectpicker" name="second" data-live-search="true" id="learner-emails">
<option style="color:#bbb;" value="" disabled selected>Select emails</option>
<?php while($row = mysqli_fetch_array($result_emails)){ ?>
<option value="<?php echo $row['Email'];?>"><?php echo $row['Email'];?></option>
<?php } ?>
</select>
mysqli_data_seek( $result_emails, 0 ); use this before second dropdown
Put the rows into an array at the beginning of your script.
$rows = [];
while($row = mysqli_fetch_array($result_emails))
$rows[] = $row;
Then, on the html sections, loop over the array. You can loop over it as many times as you want. You can't do that with the query results.
<?php foreach($rows as $row):;?>
<option value="<?php echo $row['Email'];?>"><?php echo $row['Email'];?></option>
<?php endfor;?>
mysqli_fetch_array() moves the pointer forward each time you call it. You need mysqli_data_seek() to set the pointer back to the start and then call mysqli_fetch_array() again.
I am new to PHP and trying to make dynamic list box using SQL
<label> Select Sport</label>
<select name = "Sport">
<option value = "">Select Sport</option>
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"></option>
<?php
}
?>
</select>
But its printing BLANK space
Try this:
<select name="">
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"><?php echo $Sport['Sport']; ?></option> // The value between <option> value </option> is the value which is to be shown in the dropdown, without this it is showing blank
<?php
}
</select>
You probably shouldn't use SELECT *, but since you're learning, I digress. Also, I personally have a hard time reading a lot of opening and closing php tags scattered throughout plus they often give weird results than what you're expecting, so this is how I'd write it.
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $sport)
{
echo "<option value =" . $sport['Sport'] . ">" . $sport['Sport'] . "</option>";
}
?>
The blank spaces indicates that you are actually hitting the loop and iterating, but the values for that array item are null. Are you sure the field name isn't 'sport' instead of 'Sport'?
The problem I have is that there is no results in the "category" option. I tried to use two whiles but that doesn't work either.
This is my php:
try{
$sel=$pdo->prepare("Select * from
actors inner join cat
on actors.id=cat.id");
$sel->bindColumn(1,$actors_id);
$sel->bindColumn(2,$actors);
$sel->bindColumn(3,$cat_id);
$sel->bindColumn(4,$cat);
$sel->execute();
}catch(PDOException $e){
die("error: " . $e->getMessage());
}
And This is my html:
<form action="" method="post">
<label for="author">Author</label>
<Select id="author">
//Show data from php file:
<?php while($sel->fetch()):?>
<?php echo "<option value='$actors_id'>$actors</option>";?>
<?php endwhile; ?>
</Select>
<label for="category">Category</label>
<Select id="category">
//Show data again from php file:
<?php while($sel->fetch()):?>
<?php echo "<option value='$cat_id'>$cat</option>";?>
<?php endwhile; ?>
</Select>
</form>
And this is how it looks like:
As you can see, there is no info display in the "category" option.
If I change the html to something like this, it doesn't work either:
<Select id="author">
<?php while($sel->fetch()):?>
<?php echo "<option value='$actors_id'>$actors</option>";?>
</Select>
<label for="category">Category</label>
<Select id="category">
<?php echo "<option value='$cat_id'>$cat</option>";?>
<?php endwhile; ?>
</Select>
Now it looks like this:
Do you have any suggestion?
Your first while loop fetches all rows from the PDOStatement object, so when you call while ($sel->fetch()) a second time, it immediately returns false. If you want to use the results in essentially two separate loops, fetch all rows first into an array of rows, then loop over that array. Do so with fetchAll() instead of binding to variables via bindColumn().
In fact, I would almost always recommend fetching first rather than during the display logic, except for very large rowsets where the memory consumed by fetching it all at once would be taxing on your system.
// Get all the rows as an array
$rows = $sel->fetchAll(PDO::FETCH_ASOC);
// Later loop for the items you need as many times as you need
// Each new foreach will iterate the entire $rows array from start to end
<?php foreach ($rows as $row): ?>
<?php echo "<option value='{$row['actors_id']}'>" . htmlspecialchars($row['actors']) . "</option>";?>
<?php endforeach; ?>
<?php foreach ($rows as $row): ?>
<?php echo "<option value='{$row['cat_id']}'>" . htmlspecialchars($row['cat']) . "</option>";?>
<?php endforeach; ?>
Don't forget to call htmlspecialchars() on the output, to ensure it is properly encoded for HTML. I haven't done so on cat_id,actors_id on the assumption those are known to be integers, but if not, then you should use htmlspecialchars($row['cat_id'], ENT_QUOTES) to ensure the HTML attributes are correctly quoted.
You need to be cleaner in your return -
<?php
$result = $sel->fetchAll(PDO::FETCH_OBJ); //return an object
foreach($result as $actor) {
echo '<option value=' . $actors->actor_id .'>'. $actor->actors .'</option>'; // note the quotes
}
?>
You can loop through the result as many times as you need to, without re-fetching the data.
I have a material table where the all materials are being inserted in the desc column. desc values can be as many as possible.
sample:
desc
poly canvass
metal
washer
knot
Here's the code i did.
<?php
$resource=mysql_query("Select * from material",$con);
while($result2=mysql_fetch_array($resource))
{
?>
<select>
<option value="<?php echo $result2[desc] ?>"><?php echo $result2[desc] ?></option>
</select>
</tr>
<?php };?>
But it generates four dropdown that corresponds each of the four values in my db
like:
dropdown1 dropdown2 dropdown3 dropdown4
polycanvass metal washer knot
How can I make all of the values in the desc column to appear in one dropdown?
<?php
$resource=mysql_query("Select * from material",$con);
?>
<select>
<?php
while($result2=mysql_fetch_array($resource))
{
?>
<option value="<?php echo $result2[desc] ?>"><?php echo $result2[desc] ?></option>
<?php
}
</select>
?>
select should be out of while loop because for each value it also gets repeated and you will end up having as many drop downs as the number of total records.
Take out the select tag from the while loop and put it outside.
I would recommend you to use mysqli instead of mysql
PHP MySQLi = PHP MySQL Improved!
Note: The MySQLi extension is designed to work with MySQL version 4.1.13 or newer
<?php
$con=mysqli_connect("host","username","password","dbname");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$resource= mysqli_query($con,"SELECT * FROM material");
echo "<select>";
while($result = mysqli_fetch_array($resource)){
echo '<option value="'.$result[desc].'">'.$result[desc].'</option>';
}
echo "</select>";
mysqli_close($con);
?>
<?php
$resource=mysql_query("Select * from material",$con);
?>
<select>
<?php
while($result2=mysql_fetch_array($resource))
{
echo '<option value="'.$result2[desc].'">'.$result2[desc].'</option>';
}
?>
</select>
Select out of loop and shorter way to output <option>
I have result that I want to display as a drop down menu. The query selects id and name from a table.
$usersQuery = "SELECT id, name
FROM users";
$usersResult = mysqli_query ($dbc, $usersQuery);
I want to use this result as a list in a drop down menu. This is what i have so far.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
foreach ($usersRow as $value){
echo "<option value=\"$value\"";
echo ">$value</option>\n";
}
}
?>
</select>
this would work fine if I just wanted to display name as both the value and the display to the user. But what I want to do is use the selected id as "value" for the select option and I want to show the name selected to the user. I have tried this but it does not work.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
foreach ($usersRow as $id=>$name){
echo "<option value=\"$id\"";
echo ">$name</option>\n";
}
}
?>
</select>
Any help would be great.
Thanks in advance.
mysqli_fetch_array() is a function which converts your query results into an array. which means you can display your values like you would with a normal array value.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
echo "<option value=\"".$usersRow['id']."\"";
echo ">".$usersRow['name']."</option>\n";
}
?>
</select>
No need for the foreach iteration; mysqli_fetch_array() already provides an associative array. After each fetch do
// Assuming "id" is a numeric value
printf('<option value="%d">%s</option>', $usersRow['id'], $usersRow['name']);
The foreach is unnecessary - using a while loop on the mysqli_fetch_array command will return all the results with each row in an array - you can use it like so:
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
echo "<option value=\"".$usersRow['id']."\"";
echo ">".$usersRow['name']."</option>\n";
}