Reputation: 89
<?php echo form_dropdown('item_id', $records2, '#', 'id="items"'); ?>
I want to set the default value of something in the select box whenever the form loads. If it is not possible, then I would like to convert this into regular select box. Will something like this work?
<select name="select1" id="items" class='cho'>
<option>--Select-- </option>
<?php foreach($records2 as $r) { ?>
<option value="<?=$r->item_id?>">
<?=$r->item_name?>
</option>
<?php } ?>
</select>
I tried this, but I am getting an error and values not come up into the select box. What am I doing wrong?
Upvotes: 0
Views: 299
Reputation: 102755
Here's how this helper function works:
form_dropdown('item_id', $records3, '#', 'id="items"')
// field name options default value HTML attributes
Options must be an array in key/value format*, for example:
$options = array(
'value1' => 'Text 1',
'value2' => 'Text 2',
'value3' => 'Text 3'
);
The default value should be one of the keys in $options
, or nothing will happen.
It looks like you'll need to create this array first rather than just pass in $records2
:
$options = array();
foreach ($records2 as $r) {
$options[$r->item_id] = $r->item_name;
}
echo form_dropdown('item_id', $options, '{default value}', 'id="items"');
* If the array passed as $options is a multidimensional array, form_dropdown() will produce an <optgroup>
with the array key as the label.
Upvotes: 0
Reputation: 685
The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. You can also pass an array of multiple items through the third parameter
<?php echo form_dropdown('select1', $records2, $selected_value); ?>
Upvotes: 1