I want make html option checked with condition from mysql data - php

so how to make condition in echo php, i have code
the scanario is, data will edit and ind option will output value checked at same in database
<select required="" class="form-control select2_category" name="dokter">
<option>Pilih Dokter....</option>
<?php foreach ($dokter as $r) {
echo "<option value='$r->dokter_name'>$r->dokter_name</option>";
}
?>

Check it
<select required="" class="form-control select2_category" name="dokter">
<option>Pilih Dokter....</option>
<?php foreach ($dokter as $r) {
$selected = '';
if($r->dokter_name == 'data that comming from table' )
$selected = 'selected';
else
$selected = '';
echo "<option $selected value='$r->dokter_name'>$r->dokter_name</option>";
}
?>

Related

Could set selected value in drop down dynamically using php

I need to select value dynamically using PHP in drop down list but in my case its not working as expected. I am explaining my code below.
<?php
$citySelected = "";
if(!isset($_GET['city'])){
$citySelected = 'selected="selected"';
}
?>
<select id="selectedLoc" name="selectedLoc" class="chosen-select form-control">
<option value="">Select City</option>
<option value="0" <?php echo $citySelected; ?>>Global</option>
<?php
foreach ($locationArr as $key => $value) {
$id = $value['id'];
$city = $value['city'];
$location = $value['location'];
$selected = "";
if(isset($_GET['city']) && $_GET['city']== $value['id']) {
$selected ='selected="selected"';
}
echo "<option value='$id' $selected>$location</option>";
}
?>
</select>
Here I need when there is any query string value then it will match with respective id and select that option and if there is no query string value at all then the global option will select.
Inside if condition ';' is missing. It should be like
if(isset($_GET['city']) && $_GET['city']== $value['id']){echo ' selected="selected"';}else{echo '';}
You can also use conditional operator for code simplicity, eg:-
<?php echo (isset($_GET['city']) && $_GET['city']== $value['id'])? ' selected="selected"':''; ?>

PHP - Edit page , How to set default dropdownlist that have option from MySQL?

I know only this method. this method is assume that you know the values in all <option>
<select name="agama" id="agama">
<option value="Islam"<?php if ($rows['agama'] === 'Islam') echo ' selected="selected"'>Islam</option>
<option value="Khatolik"<?php if ($rows['agama'] === 'Khatolik') echo ' selected="selected"'>Khatolik</option>
<option value="Protestan"<?php if ($rows['agama'] === 'Protestan') echo ' selected="selected"'>Protestan</option>
<option value="Hindu"<?php if ($rows['agama'] === 'Hindu') echo ' selected="selected"'>Hindu</option>
<option value="Buddha"<?php if ($rows['agama'] === 'Buddha') echo ' selected="selected"'>Buddha</option>
<option value="Lain-Lain"<?php if ($rows['agama'] === 'Lain-Lain') echo ' selected="selected"'>Lain-Lain</option>
</select>
.... the above code is example from other people not mine.
but My case is the <option> is select from database too.
I have 2 table, oav_event and oav_album
the oav_album has foreign key (event_id) from oav_event table
I want to check if row['event_id'] from oav_album table is equal to option value (from oav_event table) if true, then set selected="selected"
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row['event_id']; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
the option will change depend on change in database table, so I don't know the value in option. How should I do?
<select name="event_id">
<?php
$sql = "SELECT * FROM oav_event";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$selected = "";
if($row['event_id'] == $Yourmatchvalue)
{
$selected = "selected";
}
?>
<option value="<?php echo $row['event_id']; ?>" selected="<?php echo $selected; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
</select>
may this helps your. you need to replace $Yourmatchvalue variable with your variable.
You can use $_GET as the method on your form and pass the id of the record using it:
while($row = mysqli_fetch_assoc($result)) {
if (!empty($_GET['event_id']) && $row['event_id'] == $_GET['event_id']) {
$selected = 'selected = "selected"';
} else {
$selected = '';
}
echo '<option '.$selected.' value="'.$row["event_id"].'">'.$row["event_date"].'</option>';
}
Here is a solution,
$selected_value = 'Hindu'; // This will come from database
Change option tag with this
<option value="<?php echo $row['event_id']; ?>" <?php echo ($row['event_id'] == $selected_value) ? 'selected="selected"' : ''; ?> >Event: <?php echo $row['event_date']; ?> </option>
Create one function which will create options list like this:
function setDropdownValue($selectQueue_list,$selectedVal)
{
$queueVal = '';
$selectQueue_list_res=$db->query($selectQueue_list);
while($selectQueue_list_res_row=$db->fetchByAssoc($selectQueue_list_res))
{
$val = $selectQueue_list_res_row['id'];
$name = $selectQueue_list_res_row['name'];
if($val == $selectedVal)
{
$queueVal .= "<option value='$val' selected='selected' label='$name'>$name</option>";
}
else
{
$queueVal .= "<option value='$val' label='$name'>$name</option>";
}
}
return $queueVal;
}
Then create a query:
$get_value_query="SELECT id, name FROM table";
$dropdown_selected_value = !empty($dropdown_value) ? $dropdown_value: ''; // Pass value which you want to be selected in dropdown
Then call this function:
$dropdown_options = setDropdownValue($get_value_query, $dropdown_selected_value);
Later when you get dropdown options in $dropdown_options, use jquery to populate the dropdown, like this:
$('#dropdown_select_id').html("$dropdown_options");
Give it a try, and let me know.

HTML select option set selected default echo get value php

I have a simple HTML form with a simple select like this
<select name="myselect" selected="<?php echo $_GET['brand'];?>">
<option value="" <?php if($brand== "") echo "selected"; ?>>all brands</option>
<option value="samsung" <?php if($brand== "samsung") echo "selected"; ?>>Samsung</option>
<option value="motorola" <?php if($brand== "motorola") echo "selected"; ?>>Motorola</option>
</select>
What I am trying to do is because its a search filtering results form if the user choose Samsung as default brand to search next page with filteres search results will the select option default selected Samsung and no other. If user select to search Motorola then the selected option default will be Motorola.
How do I fix this?
selected is attribute of option not select, so remove it from select tag, change to:
<?php $brand = trim( strtolower($_GET['brand']) ); ?>
<select name="myselect">
<option value="" <?php if($brand== "") echo "selected"; ?>>all brands</option>
<option value="samsung" <?php if($brand== "samsung") echo "selected"; ?>>Samsung</option>
<option value="motorola" <?php if($brand== "motorola") echo "selected"; ?>>Motorola</option>
</select>
This is Simple Example of selected=selected by using foreach loop and ternary operators in php... use can easily understand and customize that code...
<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $key => $value) { ?>
<option value="<?php echo $key;?>" <?php echo ($key == '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>
<?php $selected_brand = $_GET['brand'];
$temp_str = '';
$temp_str .= '<select name="myselect">';
$brands_array = array("all" => "all brands", "samsung" => "Samsung", "motorola" => "Motorola");
foreach ($brands_array as $key => $value) {
if($key == $selected_brand){
// For selected option.
$temp_str .= '<option value="'.$key.'" selected>'.$value.'</option>';
} else {
$temp_str .= '<option value="'.$key.'">'.$value.'</option>';
}
}
$temp_str .= '</select>';
echo $temp_str; // Select box.
?>

Form Post delay when executed

Hi I have a form with selector name "top" in PHP framework Codeigniter. When I submit the form, the input post does not show the value posted. Only when I submit the second time, it post the value. The code is below, please advise?
<div id="selector_form">
<?php
echo form_open(base_url().'home/choose_car/'); ?>
<select name="select_car">
<option>Please Select Car</option>
<?php
foreach ($list as $key => $value) {
if($value->model == $car[0]->model){
$selected = 'selected';
}else{
$selected = '';
}
//change make to carid
echo "<option ".$selected." value='".$value->carID."''>".$value->make." - ".$value->model."</option>";
}
?>
</select>
<textarea id="description" rows="4" cols="50" ><?php if(isset($car)&&$car){print_r($car[0]->desc);}else{echo "Please Select Car";} ?></textarea>
<input type="submit" />
</div><!-- end selector_form -->
<table id="controls_table">
<th>Section</th><th>Material</th><th>Color</th><th>Show</th>
<tr>
<td><label for="top">Top: </label></td>
<td><select name="top">
<?php
if($material){
foreach ($material as $key => $value) {
if($value->materialID == $this->input->post('top')){
$selected = 'selected';
}else{
$selected = '';
}
echo "<option ".$selected." value='".$value->materialID."'>Carbon/".$value->materialName."</option>";
}
}else{
echo "<option ".$selected." >Please Select Car</option>";
}
?>
</select></td>

To make dropdown selected while fetching dynamically

To make dropdown ticked while fetching dynamically from db.Fetch all values from db and list as dropdown.If the values from db matches particular value make that dropdown selected
<select id="designation" name="designation">
<? while($role=mysql_fetch_array($sql_role)){
if ($role['role'] == $desig ) echo $selected="selected"; else $selected=" ";?>
<option <?=$selected?>>Select</option>
<option value="<?=$role['id']?>"
<?=$selected?> >
<?=$role['role']?>
</option>
<? } ?>
</select>
You are echoing $selected in your check. I also recommend not to use php short tags because it may be deprecated in future PHP versions.
I recommend this code:
$html = '<select id="designation" name="designation">';
$html .= '<option>Select</option>';
while($role = mysql_fetch_array($sql_role))
{
$selected = ($role['role'] == $desig) ? ' selected' : '';
$html .= '<option value="'. $role['id'] .'"'. $selected .'>'. $role['role'] .'</option>';
}
$html .= '</select>';
echo $html;
Don not forget to assign $desig ;-).
Why don't you make the following changes and try. You can totally eliminate the intermediate variable storage. Just print selected if the condition matches. And also you should just put that default select option outside the while loop.
<select id="designation" name="designation">
<option>Select</option>
<? while($role=mysql_fetch_array($sql_role)){ ?>
<option value="<?=$role['id']?>" <? if ($role['role'] == $desig ) echo "selected='selected'"?> ><?=$role['role']?></option>
<? } ?>
</select>
try to avoid short scripts in php like this
<select id="designation" name="designation">
<? while($role=mysql_fetch_array($sql_role)){
<option <?php if ($role['role'] == $desig ) { ?> selected <?php } ?> ><?php echo $role['role']; ?></option>
<? } ?></select>

Categories