display from database to dropdown list - php

I have stored value from drop down list to database. I want to echo the same value to be displayed in that drop down list in my edit form. how can I achieve that in php?
Region
<select name="stf_region">
<option>Select</option>
<option value="1">MDU</option>
<option value="2">TMM</option>
</select>
i have stored in database using value of selection
but i dont know to display that value in same drop down

Use something like this:
<?
$sql = "SELECT id, description FROM dropDownTable";
$rs = mysql_query($sql);
?>
<select name="dropDown">
<option value="-1">Please select...</option>
<? while ($obj = mysql_fetch_object($rs)) { ?>
<option value="<?= $obj->id; ?>" <? if ($data['downDown'] == $obj->id) echo "SELECTED"; ?>>
<?= $obj->description; ?>
</option>
<? } ?>
</select>
Please note $data needs to be set as an associative array containing attributes of the entity that is being edited. This code is flexible because in the case of a form where a user may have submitted an incomplete form $data could be set to the $_POST variable and so all entered fields can be included without the user needing to re-specify fields they previously filled in. This basically means your form template, inserting an entry and editing an entry can be the same!

Well you could do like this
$query = "select id, label from lookup_table";
$result = mysql_query($query);
$html = "<select name='yourname'><option value="">Please select...</option>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$html .= "<option value='$row[id]'>$row[label]</option>";
}
$html = "</select>";
echo ($html);//Display the select in the page

If you're able to get db data into array, you can work with them like this:
<?php
$options = array('label 1' => 'value 1', 'label 2' => 'value 2');
echo "<select name=somename>";
foreach($options as $key => $value){
echo "<option value=" . $value . ">" . $key . "</option>";
}
echo "</select>";
?>

i used the COOKIE to match the value that should be selected when it comes to edit the form.
<?php
while($result_row=mysql_fetch_array($result)){
if ( $_COOKIE['MY_COOKIE'] == $result_row[importance_level_id )
{
echo "<option SELECTED=\"SELECTED\" value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
else
{
echo "<option value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
?>

Assuming you are ok with selecting the value out of the database into a PHP variable (let's say $region) I think you are just after
Region
<select name="stf_region">
<option>Select</option>
<option value="1"<?php echo ($region == '1') ? ' selected="selected"' : ''; ?>>MDU</option>
<option value="2"<?php echo ($region == '2') ? ' selected="selected"' : ''; ?>>TMM</option>
</select>
This is the most concise answer to the question that I think you are asking. However this is a very specific answer and assumes that your dropdown values are hard-coded and won't really be changing.
If you want a more flexible setup that retrieves the values of the dropdown from the database you are looking for something more along the lines of what has been suggested by Gordon Murray Dent above

<div class="form-group has-success col-md-6">
<label class="control-label" for="state-success">Select Buyer</label>
<select id="state-success" class="form-control ">
<?php
$sql="SELECT full_name FROM `new_customer`";
$data=mysqli_query($dbcon,$sql);
?>
<option>Select byer...</option>
<?php while($row1=mysqli_fetch_array($data)){?>
<option value="<?php echo $row1['full_name'];?>"><?php echo $row1['full_name'];?></option>
<?php } ?>
</select>

Related

Duple select from database in same table but diffent proposes

First option of select must be the name referring to the ID. The remaining select options are the remaining names
<select class="input" name="client_id">
<?php
$sel_client_detail="Select * from client WHERE client_id=".$id."";
$result_detail = mysqli_query($con,$sel_client_detail);
while($new_record_row = mysqli_fetch_assoc($result_detail)) { ?>
<option selected><?php echo $row['nome'];?></option>
<?php };?>
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>
Output:
<select>
<option selected> Izzi (current ID name)</option>
<option> ____________</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
</select>
If you want the user to be first in your option list just run the query once and build the HTML parts in 2 seperate strings. Then once the loop is complete put them together and echo them
<?php
echo '<select class="input" name="client_id">';
$itsme = '';
$others = '<option>-----------</option>';
$sql = "Select * from client";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
if ( $id == $row['id'] ) {
$itsme = "<option selected='selected'>$new_record_row[nome]</option>";
} else {
$others += "<option>$new_record_row[nome]</option>";
}
}
// put the option tags together in the order you specified
echo $itsme . $others . '</select>';
Here's a different, but more conventional, approach to this common scenario:
Why not just make the chosen ID selected when you get to it in the list? Then it will still show to the user first. It's more efficient than having two separate queries.
Like this:
<select class="input" name="client_id">
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option <?php echo ($new_record_row["client_id"] == $id ? "selected": ""); ?> ><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>

Select form - use last posted value if blank

Sorry in advance for the novice question here...
I currently have my first value as a disabled and defaulted "Select" option which then changes to the selected option when a selection is made.
However if the user submits again without reselecting, the value defaults back because the post is blank. Therefore is there a way to use the previous value if so?
<select name="test_select" style="width: 110px">
<option disabled="disabled" selected="selected">
<?php
if(!empty($_POST['test_select'])){
echo $_POST[test_select'];}
else
echo "Select Option"; ?>
</option>
<?php $sql = mysql_query("SELECT test FROM test_settings");
while ($row = mysql_fetch_array($sql)){
?><option><?php echo $row['test']; ?></option><?php }?>
</select>
Thanks in advance,
Dan
I suppose that problem is that forms are not sending disabled values.
I would edit code as following:
<select name="test_select" style="width: 110px">
<?php
if (empty($_POST['test_select']))
echo '<option selected="selected">Select Option</option>';
$sql = mysql_query("SELECT test FROM test_settings");
while ($row = mysql_fetch_array($sql)){
$selected = isset($_POST['test_select']) && $row['test'] == $_POST['test_select']
? ' selected="selected"'
: '';
echo '<option'.$selected.'>'.$row['test'].'</option>';
?>
</select>

Remember dropdown item OR select if GET

I currently have this code to remember the last selected item on post.
<select tabindex="1" name="system_customer">
<option value="Choose">- Choose a Customer -</option>
<?PHP while ($row = mysql_fetch_array($result_get_users))
{
echo "<option value='".$row['id']."'";
echo $system_customer == $row['id'] ? 'selected="selected"' : '';
echo ">".$row['firstname']." ".$row['lastname']."</option>";
} ?>>
</select>
On load (before submitting) the list defaults to "Choose". What I would like to do, is if the URL has "userid=1" to select user 1, or if it is "userid=2" then select user 2, but keep the code to remember the last selected.
Thank you.
PS - cannot use mysqli or PDO due to the software I am writing a module for. It is hard coded and encrypted.
Add this above your code -
$system_customer = "";
if (!empty($_GET['userid'])) {
$system_customer = $_GET['userid'];
}
Then -
<select tabindex="1" name="system_customer">
<option value="Choose">- Choose a Customer -</option>
<?PHP
while ($row = mysql_fetch_array($result_get_users)) {
echo "<option value='".$row['id']."'";
echo $system_customer == $row['id'] ? 'selected' : '';
echo ">".$row['firstname']." ".$row['lastname']."</option>";
}
?>
</select>
You can use session or cookie
for exp :
<?PHP
Session_start();
if ( isset ( $_POST['system_customer'] ) ) {
$_SESSION['SELECTED_USER_ID'] = $_POST['system_customer'];
?>
and you haave to make some changes in you dropdown list
<select tabindex="1" name="system_customer">
<option value="Choose">- Choose a Customer -</option>
<?PHP while ($row = mysql_fetch_array($result_get_users)) {
echo "<option value='".$row['id']."'";
echo $_SESSION['SELECTED_USER_ID'] == $row['id'] ? 'selected="selected"' : '';
echo ">".$row['firstname']." ".$row['lastname']."</option>";
}
?>>
</select>
in you select form you have to check if session id is equal to row if then put selected attribiut for that option .
but if you want to save details for long time it's better to use cookie or databases you can't do it in another cases .

How to put a default value in a dropdown in php?

My code looks like:
<select autofocus="autofocus" name="SourceCountry" id="SourceCountry">
<?php populate_country();?>
</select>
I am populating all the countries of the world in that dropdown. I want a particular country name(say Japan) as default selected. I am not sure which attribute or property of the select tag does this. Tried to use many, but didnt work out. Can anyone suggest?
The code of populate_country goes like this:
if(connect_to_DB()==1)
$result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row = mysqli_fetch_assoc($result))
{
echo '<option value='.$row['CountryName'].'>'.$row['CountryName'].'</option>';
}
In FetchCountriesList(), there is a select query like:
SELECT distinct CountryName from Countries
Thanks
Edit your function and try to find Japan and set this one as Selected :)
if(connect_to_DB()==1) $result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row = mysqli_fetch_assoc($result)) {
if($row['CountryName'] !== "Japan") {
echo '<option value='.$row['CountryName'].'>'.$row['CountryName'].'</option>';
} else {
echo '<option value='.$row['CountryName'].' selected="selected">'.$row['CountryName'].'</option>';
}
}
Add attribute selected. Try this:
<select name="country">
<option value="kor">Korea</option>
<option value="rus">Russia</option>
<option selected="selected" value="jap">Japan</option>
</select>
Add selected for your option in your function populate_country(). (in the option you want to select as default only)
Add the seleteced attribute, e.g.:
<option value="test" selected="selected">Bla</option>
$defValue = 'Japan';
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row['CountryName'] . '"';
if ($row['CountryName'] === $defValue) {
echo ' selected="selected"';
}
echo '>';
echo $row['CountryName'] . '</option>';
}
Just add attribute selected="selected" on the option value that you wish to be selected.
E.g.:
<option value="Japan" selected="selected">Japan</option>
To use this in your function, replace your function with this:
function populate_country($selected = NULL) {
if(connect_to_DB()==1) $result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['CountryName'].'" '.($selected == $row['CountryName'] ? 'selected="selected"' : NULL).'>'.$row['CountryName'].'</option>';
}
}
You can then use this in your HTML:
<select autofocus="autofocus" name="SourceCountry" id="SourceCountry">
<?php populate_country("Japan");?>
</select>
<select>
<option> Red </option>
<option selected="selected"> blue </option>
<option> yellow </option>
</select>
Blue would be the selected in that dropdown.
Basically you need to loop throught your values and match if the value you want matches with the ones you have, and add the selected="selected" string to that option:
I would do something like this: (untested)
function populate_country($country){
$countries = array('algeria', 'japan', 'mexico', 'united kingdom' ..... );
foreach($countries as $country){
$selected = (strtolower($selected) == $country) ? ' selected="selected"' : null;
echo "<option$selected>".ucwords($country)."</option>\r\n";
}
}

option select by based on the value from db with php

How can I select the option based on the value from db with php .For example;
<select>
<option value="1">Val1</option>
<option value="2">Val2</option>
<option value="3">Val3</option>
<option value="4">Val4</option>
<option value="5">Val5</option>
</select>
when value=3 change
<option value="3" selected="selected">Val3</option>
How can I do this with php&mysql?
You need something like
<?
$values = array(); // array from DB
$selectedKey = 10; // some key
?>
<select>
<? foreach ($values as $key => $value) { ?>
<option value="<?=$key?>" <?= $key==$selectedKey ? 'selected' : ''?>> <?=$value?> </option>
<? } ?>
</select>
Hope This Helps
<select>
<?php
$value = 3; // Desired Value
$sql ='' ;// sql to get values from mysql
$res = mysql_query($sql);
while($row=mysql_fetch_array($res))
{?>
<option value="<?php echo $row['id'];?>" <?phh if($row['id']==$value)echo "selected='selected'" ; ?> ><?php echo $row['name']; ?></option>
<?php }
?>
</select>
In the loop that prints out the possible values, compare each value to the value acquired from the DB. If they match, add the selected attribute.
First give the select tag a name.
<select name="name">
Get the table rows through a mysql_query into $row_set array
If say the value to be searched for is $search
while($row = mysql_fetch_array($row_set))
if($row['column_name']==$search) echo "<option value='{$row['value']}' selected='selected'>{$row['name']}</option>";

Categories