Ahmad Farid
Ahmad Farid

Reputation: 14774

How do add fill an html dropdown list by items in an array in php

This is the HTML in a php file.

<select id="efHidden" style="width:80px;" class="text ui-widget-content ui-corner-all">
     <option value="0">off</option>
     <option value="1">on</option>
</select>

and I have a php variable $arr having the required values to be shown

Upvotes: 0

Views: 3679

Answers (3)

piddl0r
piddl0r

Reputation: 2449

<?php $arr = array('off' => 0, 'on' => 1); ?>

<select>
    <?php 
       foreach($arr as $k => $v)
       {
           echo "<option value=\"$v\">$k</option>";
       }
    ?>
</select>

Upvotes: 1

Bhanu Prakash Pandey
Bhanu Prakash Pandey

Reputation: 3785

<?php
$arr = array('0' => 'off', '1' => 'on');
?>
<select id="efHidden" style="width:80px;" class="text ui-widget-content ui-corner-all">
                        <?php foreach ($arr as $key=>$value) {?>
                        <option value="<?php echo $key; ?>"><?php echo $value; ?></option>
                        <?php } ?>
                       </select>

Upvotes: 1

user549167
user549167

Reputation:

$arr = array('1' => 'Option One', '2' => 'Option Two', '3' => 'Option Three');
foreach($arr as &$item){ echo '<option value="'.$item[0].'">'.$item[1].'</option>'; }

Upvotes: 0

Related Questions