Reputation: 645
I have the following codes...
$all = $ent->getAll($fed_code);
Array ( [NC] => Array (
[101] => banana,
[102] => orange,
[103] => apple,
)
)
$select = $ent->getSelected($fed_code);
Array
(
[101] => banana,
)
what i want is if the $select value is found in $all array, Then fill the dropdown by the value selected. Here is the Code i have At the moment
<?php
foreach ($all as $orgKey => $list) {
?>
<tr><td width="5%">
<h5>Extra Fruits</h5>
</td>
<td width="10%">
<label class="control-label">Fruits</label>
<select class="input-xlarge" id="input" name="ent[]">
<option value="">Select</option>';
<? foreach ($list as $key => $value) {
$selected = in_array($select, $key)?'selected="selected"':'';
echo $selected;
?>
<option <?=$selected?> value="<?=$key?>"><?=$value?></option>
<? } ?>
</select>
</td>
</tr>
But, It seems there is something wrong, it doesn't select any thing. Some one has some idea please?
Upvotes: 0
Views: 100
Reputation: 590
Do you really need the foreach inside? I tried this and it can already print your select with those in $select as selected.
<select class="input-xlarge" id="input" name="ent[]">
<? foreach ($all as $orgKey => $list) {
$selected = in_array($select, $list) ? 'selected="selected"' : '';
?>
<option <?=$selected?> value="<?=$orgKey?>"><?=$list?></option>
<? } ?>
</select>
I have just realized that I have interchanged the parameter for the in_array. This should do it. I also added the multiple option in the select tag so if ever we have multiple selected it will be shown.
<select multiple class="input-xlarge" id="input" name="ent[]">
<? foreach ($all as $orgKey => $list) {
$selected = in_array($list, $select) ? 'selected="selected"' : '';
?>
<option <?=$selected?> value="<?=$orgKey?>"><?=$list?></option>
<? } ?>
</select>
If you don't want to use multiple, the last item that will test positive that it is selected will be the one shown as selected in html.
Upvotes: 1
Reputation: 3526
What does printing $selected
produce? Also to select an option from select tag, isn't the syntax like:
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
instead of what you give as selected="selected"
Upvotes: 0