I use codeigniter to create a dropdownlist with customers.
If I enter selected, than the last customer in the dropdown is automatically choosen.
Is it possible to select a customer inside that foreach?
My code snippet:
<?php foreach ($customers as $c): ?>
<option value="<?php echo $c->customer_id;?>"><?php echo $c->customer_name; ?></option>
<?php endforeach; ?>
Yes it is. However you have to know which customer is chosen at the moment, and then inside the loop check if the chosen customer_id is the same as the current one:
<?php
$chosenCustomer_id = 5; //of course don't hardcode it
foreach ($customers as $c):
$selected = $c->customer_id == $chosenCustomer_id ? 'selected' : '';
?>
<option value="<?php echo $c->customer_id;?>" <?php echo $selected; ?>> <?php echo ><?php echo $c->customer_name; ?></option>
<?php endforeach; ?>
Just add ternary condition in <option> inside for loop where $selectedOption is your value to be selected.
<?php $selectedOption = "yourvalue";
foreach ($customers as $c): ?>
<option value="<?php echo $c->customer_id;?>" <?= ($c->customer_id == $selectedOption ? "selected" : "")><?php echo $c->customer_name; ?></option>
<?php endforeach; ?>
Use selected attribute in Option Tag
Selected needs to be set based on condition
$selected=(condition): "selected","";
<option <?php echo $selected; ?>> Option Inner Html </option>
Compare variable with attribute in option tag
<?php $chosenCustomer_id = 5;?>
<select name="customer" required>
<?php foreach ($customers as $c){?>
<option <?=($chosenCustomer_id==$c['customer_id']?'selected="selected"':'')?> value="<?=$c['customer_id']?>"><?=$c['customer_name']?></option>
<?php }
?>
</select>
Related
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']
I am trying to populate a drop-down list of the database. In my view file I have the following code
Here is my controller
$query = $this->interprete_model->interpreteID($this->session->userdata('user_id'));
print_r($query);
$data['interprete'] = $query;
Aqui esta mi vista, usa set_select.
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($result as $row):?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, TRUE); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
Result:
enter image description here
Many selected, I need one selected to modify (update) the data.
You can try this :
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($users as $row):
$selected = FALSE;
// 1 is the id u want to be selected u can change it according to you
if ($row->id == 1){
$selected = TRUE;
}
?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, $selected); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
You can also use form_dropdown as
// FOR ids
$ids = array(1,2,3,4); // array of user ids
echo form_dropdown('regionI',$ids,1,array('class'=>'form-control'));
// FOR name
$names= array('name1','name2','name4','name3'); // array of user names
echo form_dropdown('regionI',$names,'name1',array('class'=>'form-control'));
For More :
https://www.codeigniter.com/user_guide/helpers/form_helper.html
i write this way for edit time selection
<?php foreach ($select_single as $select_single_show):?>
<select class="form-control" name="regionI">
<?php foreach ($users as $row):?>
<option <?php if($row->id==$select_single_show->regionI)echo "selected";?> value="<?php echo $all_branch_show->id?>"><?php echo $row->name?>
</option>
<?php endforeach;?>
</select>
<?php endforeach;?>
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>
Here is my code.
$result = mysqli_query($link, 'SELECT product_name FROM products');
while ($row = mysqli_fetch_array($result))
{
$products[] = array('product_name' => $row['product_name']);
}
<select name="products" id="products">
<option name="search_product" value="All Products"<?php if (isset($search_product_selected) AND $sort_product_selected == "All Products") { echo ' SELECTED'; } ?>> All Products</option>
<?php foreach ($products as $product): ?>
<option name="search_product" value="<?php echo $product['product_name'];"><?php echo $product['product_name']; ?></option>
<?php endforeach; ?>
</select>
This is part of a form that reloads the same page, adjusting the items shown on the page. The form works perfectly, and the search works perfectly, but how do I use SELECTED to highlight which product was selected in the previous search?
Compare the value with your search word, if they are equal then put selected into the option.
<?php foreach ($products as $product): ?>
<option name="search_product" value="<?php echo $product['product_name'];?>"<?php echo $product['product_name'] === $_POST['search_key'] : ' selected' : '' ?>><?php echo $product['product_name']; ?></option>
<?php endforeach; ?>
I am creating a like this:
<select name="id" id="id">
<OPTION VALUE="null">Select<OPTION>
<? foreach ($genres as $row){ ?>
<OPTION VALUE="<? echo $row->id; ?>"><? echo utf8_encode($row->name); ?></OPTION>
<?
}
?>
</select>
I am using the $_GET to check for a value on the URL and then just take it and with that value I have to pre-select the option in the select menu, any ideas how to do this? I'm unsure of how to do it via javascript (or even if it would be more complicated).
just use
selected="selected"
on your pre-selected option
<select name="id" id="id">
<OPTION VALUE="null">Select<OPTION>
<? foreach ($genres as $row){ ?>
$selected = $_GET['your_var'] == $row->id ? 'selected="selected"' : false;
<OPTION <? echo $selected; ?> VALUE="<? echo $row->id; ?>"><? echo utf8_encode($row->name); ?></OPTION>
<?
}
?>
</select>