0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

But the $list will echo Array is it? not the content of the array? how do you solve that, then?
my bad I have interchanged the parameters of the in_array function. $list will only have the value and not array. Please see my update.
Not really it is not working, I have to make it in different process with long code and with a bit of trick. But now it is ok. Thanks anyway. I just give you point for your effort.
0

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"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.