CodeIgniter Forums

Full Version: Explode for selected
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I have trouble on this code,

In DB "planCurrencies" = "1,2,3,4"
PHP Code:
<select class="select2" name="currency[]" style="width: 100%" multiple="multiple" data-placeholder="Choose">
    <?
php
    $cid 
explode(',',$plan->planCurrencies);
    foreach(
$this->currencies_model->get() as $c) { ?>
    <option value="<?php echo $c->ID?><?php echo $c->ID == $cid "selected":"" ?>><?php echo $c->currencyCode?></option>
    <?php ?>
</select> 

This code already show all currencies but selected currency not working,

Thanks you
$cid is array in your case, so the <option> selected will be like this

Code:
<option value="<?php echo $c->ID; ?>" <?php echo in_array($c->ID, $cid) ? "selected":"" ?>><?php echo $c->currencyCode; ?></option>


I hope this works for you
(11-06-2017, 10:43 PM)pravins Wrote: [ -> ]$cid is array in your case, so the <option> selected will be like this

Code:
<option value="<?php echo $c->ID; ?>" <?php echo in_array($c->ID, $cid) ? "selected":"" ?>><?php echo $c->currencyCode; ?></option>


I hope this works for you

Oh man!! it's really work! Thanks you so much!!