Call all the value from DB to dropdown list - php

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>

Related

set default value in dropdown when options in dropdown are dynamic

I have two table in database. in table their is my all divisions which are shown in dropdown options. In second table, i submit my division name in division field.
I want value in division field show as a default value in dropdown.here is my code..
here is my first query by which all division are comes
$all_customers = mysql_query("select * from `supp_customers`");
<select name="division" id="c_name" required class="form-control" onchange="my_function(this,<?php echo $users['id']; ?>)">
<option value="">All Divisions</option>
<?php while($customer = mysql_fetch_array($all_customers)){ ?>
<option value="<?php echo $customer['name']; ?>"><?php echo $customer['name']; ?></option>
<?php } ?>
</select>
and my second query
$pro_qry = mysql_query("SELECT * FROM `supp_average_notes` where `id`='$id'");
$div_query = mysql_fetch_array($pro_qry);
$dive_query['division'];
?>
i Want to set $div_query['division'] as a default in dropdown. How it can be possible
In your while loop, you should compare the division to the current division. If it's the same, select the option. I would recommend you to
do what #RiggsFolly says,
look into a PHP framework; it will take a lot of work out of your hands and add security, e.g. Laravel, Yii or Symfony,
use id's in your select option values instead of names,
use alternative code style in views like below,
improve your variable naming so the name states what it holds.
.
<?php foreach ($divisions as $division): ?>
<?php if ($division['id'] == $currentDivision['id']) $selected = ' selected="selected"'; else $selected = ''; ?>
<option <?= $selected ?> value="<?= $division['id'] ?>">
<?= $division['name'] ?>
</option>
<?php endforeach ?>

PHP populate using data from table

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.

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>

Is there a way to have an updated drop down menu that pull records from database using php

<?php
$sql="select software from software_table";
$qry=mysql_query($sql);
$count=mysql_num_rows($qry);
$row=mysql_fetch_array($qry);
$data=['software_name'];
?>
<?php
for($i=0; $i<=count($data); $i++){
?>
<select name="software_name">
<option vale="<?php echo $data[$i]?>">$data</option>
}
?>
`I dont know if this is possible, but what I want to happen is that, when I add new software from software_table. My drop-down menu for software will get its option value from the database to become updated. I am thinking to use For-Loop for this, but I dont know how to start or is it really possible.
example.
in my drop-down menu. i have 3 option value.
1.MS OFFICE
2.AUTODESK
3.PRIMAVERA
and then I add another software from the database which is WINDOWS 8.
I want my drop-down menu to have those 4 value.
Try to use the following code -
$sql="select software from software_table";
$qry=mysql_query($sql); ?>
<select name="software_name">
<?php while($row=mysql_fetch_array($qry)) {
$data=$row['software_name']; ?>
<option vale="<?php echo $data; ?>"><?php echo $data; ?></option>
<?php }
?>
</select>
<select name="software_name">
<?php
$sql="select software from software_table";
$qry=mysql_query($sql);
while ($row=mysql_fetch_array($qry))
{
// In your case, each row contains only one value: $row[0] = the value of software field
echo "<option vale=\"$row[0]\">$row[0]</option>";
}
?>
</select>
This will generate the HTML code as:
<select name="software_name">
<option vale="MS OFFICE">MS OFFICE</option>
<option vale="AUTODESK">AUTODESK</option>
<option vale="PRIMAVERA">PRIMAVERA</option>
</select>

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