1

How can I add data attributes in the <option> tags using the form_dropdown() helper ? To generate something like this, for example:

// the part that I'm interested is the **data-code** attribute
<select class="form-control" name="country">
     <option value="a" data-code="code1">itemOne</option>
     <option value="b" data-code="code2">itemTwo</option>
     <option value="c" data-code="code3">itemThree</option>
</select>
0

2 Answers 2

3

Actually I don't know it is possible or not. But I would do this way:

<?php if(sizeof($countries) > 0): ?>
<select class="form-control" name="country">
    <?php foreach($countries as $country): ?>
        <option value="<?php echo $country['value'] ?>" data-code="<?php echo $country['code'] ?>"><?php echo $country['display'] ?></option>
    <?php endforeach; ?>
</select>
<?php endif; ?>

Where as, array of $countries should be as below:

$countries = array(
    array('value'=>'a','code'=>'code1','display'=>'itemOne'),
    array('value'=>'b','code'=>'code2','display'=>'itemTwo'),
    array('value'=>'c','code'=>'code3','display'=>'itemThree')
    );
Sign up to request clarification or add additional context in comments.

Comments

0
$array = array();
    foreach($appertisers as $row ){
        $array[] = $row->product;
        $price[] = $row->price;
    }
    $array = array_merge(array('' => 'Please Select'), $array);
    echo form_dropdown('appetisers',  $array);

Dome something like that.

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.