Retrive select option from database php - php

The below code show the retrived data from database and it also repeat the same option
For example: if you select employed from dropdown, it repeat the same employed twice in dropdown
<select class="txtbox" name="currentstatus">
<option value="<?php echo "$currentstatus"; ?>"><?php echo "$currentstatus"; ?></option>
<option value="Employed"<?php if(isset($_POST["currentstatus"]) && $_POST["currentstatus"] == "Employed") echo "selected"; ?>>Employed</option>
<option value="Unemployed"<?php if(isset($_POST["currentstatus"]) && $_POST["currentstatus"] == "Unemployed") echo "selected"; ?>>Unemployed</option>
</select>

What about this? This displays two options and selects one according to the database or by $_POST:
<select class="txtbox" name="currentstatus">
<?php foreach (array('Employed', 'Unemployed') as $status): ?>
<option value="<?php echo $status; ?>"<?php if ($currentstatus == $status || (isset($_POST["currentstatus"]) && $_POST["currentstatus"] == $status)) echo ' selected'?>><?php echo $status; ?></option>
<?php endforeach; ?>
</select>

Related

How to add option fetched from the database to the end in select php

I have a categories table where I have different categories. I have also add Other in the category. When I'm getting the categories to populate the select I want the other option to appear at last. how can I do that?
<select name="category" id="category" class="form-control <?php if (isset($errors['category'])) echo 'form-error'; ?>">
<option value="">Select a category...</option>
<?php while ($category = mysqli_fetch_assoc($categories)) { ?>
<option value="<?php echo h($category['id']); ?>" <?php if (isset($_POST['category']) && h($category['id']) === $_POST['category']) echo 'selected'; ?>><?php echo h($category['name']); ?></option>
<?php } ?>
</select>
If the Id of Other category is fixed, then you have to ignore it in your database query.
Let's say that the Id of that option is 1, our query will be like this:
select * from categories where Id <> 1
This query brings all categories except "other".
After that do the following:
<select name="category" id="category" class="form-control <?php if (isset($errors['category'])) echo 'form-error'; ?>">
<option value="">Select a category...</option>
<?php while ($category =
mysqli_fetch_assoc($categories)) { ?>
<option value="<?php echo h($category['id']); ?>" <?php
if (isset($_POST['category']) && h($category['id']) ===
$_POST['category']) echo 'selected'; ?>><?php echo
h($category['name']); ?></option>
<?php } ?>
<option value="1">Other</option>
</select>
hope it helps

Option value doesn't match the right value

I have a $seasons variable that contains 21 seasons. For each season I want to make a option in html that you can select. If I press the submit button I want the option that is selected before submitting, is still selected. I try to do that with this code:
<select name="season" id="season" class="filter-season">
<option value="all">-- Alle seizoenen --</option>
<?php foreach($seasons as $season): ?>
<option <?php if (isset($_GET['season']) == $season['season']){?> selected = "true" <?php }; echo "selected" ?>\value="<?php echo $season['season'] ?>"><?php echo $season['season']; ?></option>
<?php endforeach; ?>
</select>
The problem is that the value of the option always jumps back to 21.
You need
selected="selected"
instead of
selected=true
<select name="season" id="season" class="filter-season">
<option value="all">-- Alle seizoenen --</option>
<?php foreach($seasons as $season): ?>
<?php
$isSelected = (isset($_GET['season']) && $_GET['season'] == $season['season']) ? 'selected="selected"' : '';
?>
<option <?php echo $isSelected;?> value="<?php echo $season['season'] ?>"><?php echo $season['season'];?></option>
<?php endforeach; ?>
</select>
It depends on your array type, but for a normal array:
$seasons = [
'winter',
'summer'
];
$selected_season = isset($_GET['season']) ? $_GET['season'] : false;
<select name="season" id="season" class="filter-season">
<option value="all">Alle seizoenen</option>
<?php foreach($seasons as $season): ?>
<option value="<?= $season; ?>" <?php $season == $selected_season ? 'selected="selected"' : ''?>><?= $season; ?></option>
<?php endforeach; ?>
</select>
You made a mistake.
I've rewritten your code:
<select name="season" id="season" class="filter-season">
<option value="all">-- Alle seizoenen --</option>
<?php foreach($seasons as $season): ?>
<option <?php if (isset($_GET['season']) && $_GET['season'] == $season['season']) echo "selected" ?> value="<?php echo $season['season'] ?>"><?php echo $season['season']; ?></option>
<?php endforeach; ?>
</select>
Your "echo selected" call was outside of your if statement. In your case you selected all the options and your browser then shows the last selected, in your case option 21. Also your if statement itself was wrong.
I've rewritten you're code. It now checks if $_GET['season'] is set and if $_GET['season'] equals $season['season']

echo selected in chosen select

I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>

how to put value in select option

I have select option in my page or drop down list,my problem is how can i set value
in my select option and this value is from the database,
here is my code.
<select name="status" value="<?php echo $status; ?>" >
<option value=""></option>
<option value="public">public</option>
<option value="private">private</option>
</select>
on top of my html tag
if(isset($_GET['status']))
{
$status = $_GET['status'];
$sstatus="select .......";
foreach($db->query($sstatus) as $rows)
{
$status= $rows['status'];
......
......
......
}
}
I tried modify my code and it's seems working but my problem is that it has 2 same
value in the drop down list.
<select name="status" >
<option selected="selected"><?php echo $status; ?></option>
<option value="public">public</option>
<option value="private">private</option>
</select>
it will show like this in my drop down list,my question for this is this is the right way
in displaying the values that comes from the database.
public
public
private
<select name="status" >
<option value=""></option>
<option value="public"<?php if (isset($status) && $status === 'public') echo 'selected'; ?>>public</option>
<option value="private"<?php if (isset($status) && $status === 'private') echo 'selected'; ?>>private</option>
</select>
<?php $options = array('', 'public', 'private') ?>
<select name="status">
<?php foreach ($options as $option): ?>
<option value="<?php echo $option ?>" <?php echo isset($status) && $status == $option ? 'selected="selected"' : '' ?>><?php echo $option ?></option>
<?php endforeach ?>
</select>

PHP while in edit mode show selected value in to drop down

This question was asked already, but my question is very simple.
In the my account page, I have the employee country in a dropdown.
How to select a value in the combo, when in edit mode?
Let's assume you have the user's country in $user_country and the list of all countries in $all_countries array:
<select id="country">
<?php
foreach ( $all_countries as $country ):
$selected = "";
if ( $country == $user_country )
$selected = "selected";
?>
<option value="<?php echo $country; ?>"
selected="<?php echo $selected; ?>">
<?php echo $country; ?>
</option>
<?php
endforeach; ?>
</select>
should work.
An option tag will be the default for a select list when the selected attribute is set. In the following code option 2 will show up as the current selected option when the page loads:
<select>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
To achieve this in your PHP code conditionally display the selected attribute on your options against what the current value is:
<option value="1"<?php if($user['country'] == '1') { ?> selected="selected"<?php } ?>>1</option>
<option value="2"<?php if($user['country'] == '2') { ?> selected="selected"<?php } ?>>2</option>
<option value="3"<?php if($user['country'] == '3') { ?> selected="selected"<?php } ?>>3</option>
function p_edit_combo($cCurstatus,$h_code_default,$h_name=NULL){
<select name="<?php echo $cCurstatus;?>" id="<?php echo $cCurstatus;?>" class="main_form_select">
<option value="">Select</option>
<?php
$sql_h = "SELECT h_code,h_name FROM med_hl WHERE status = 1";
$sql_h_result = mysql_query($sql_h);
while($row=mysql_fetch_array($sql_h_result)){
$h_code = $row['h_code'];
$h_name = $row['h_name'];
?>
<option <?php if($h_code_default==$h_code){ ?> selected="selected" <?php }?> value='<?php echo $h_code; ?>' >
<?php echo $h_code."|".$h_name; ?>
</option>
<?php } ?>
</select>
<?php
}
**i have two table
" users" colmns(fname,lname,...as on ohther_infomation,hobbies datatype(int))
"info" columns (id (primary_key),hobbies(varchar 200)); in which i stored for hobbies name
In my case i am storing values in from (1,2,3,4) in hobbies (int) filled of users table which i matached them through join after time of fetch them,
in my info table i stored hobbies by their name (reading, writing,playing,gyming)
$row has our users selected hobbies (int)
$rows has list of our hobbies(varchar)
edit.php i need Dropdown value selected :==== And i am Doing Like this :--- (100% Working)**
<div class="form-control">
<label for="hobbies">Hobbies</label>
<select name="hobbies">
<?php
$query = "SELECT * FROM info";
$results = mysqli_query($connect, $query);
while ($rows = mysqli_fetch_array($results)) {
?>
<option <?php if ($rows['id'] == $row['hobbies']) { ?> selected="selected" <?php } ?> value='<?php echo $rows['id']; ?>'>
<?php echo $rows['hobbies']; ?>
</option>
<?php
}
?>
</select>
<span class="text-danger"><?php if (isset($err_hobbies)) echo $err_hobbies; ?></span>
</div>

Categories