Created
March 29, 2014 17:25
-
-
Save 131digital/9858503 to your computer and use it in GitHub Desktop.
Codeigniter - Populate multi select dropdown from database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// put in controller | |
$vendor_categories = $this->vendor_category_model->get_many_by('vendorid',$id); | |
// get the category the vendor belongs to | |
$vendorcat = array(); | |
foreach($vendor_categories as $vendor_category => $value){ | |
$vendorcat[$value->categoryid] = $value->categoryid; | |
} | |
//data array for view | |
$data['$vendorcat'] = $vendorcat; | |
$data['categories'] = "categories from database here"; | |
// put in view | |
?> | |
<div class="form-group"> | |
<label class="control-label">Categories</label> | |
<div class="controls"> | |
<select name="categoryid[]" multiple id="categoryid"> | |
<?php foreach($categories as $category):?> | |
<?php $selected = in_array($category->id,$vendorcat) ? " selected " : null;?> | |
<option value="<?=$category->id?>" | |
<?=$selected?> ><?=$category->name?> | |
</option> | |
<?php endforeach?> | |
</select> | |
</div> | |
</div> |
Thank you, It works !!
Thanks a lot
this is waste of time! more easy:
on controller:
$vendor_categories = $this->vendor_category_model->get_many_by('vendorid',$id);
$vendorcat = array();
foreach($vendor_categories as $vendor_category => $value){
$vendorcat[$value->categoryid] = $value->categoryid;
}
$categories = $this->vendor_category_model->get_many(); // in form of array('key->value) par
$data['$vendorcat'] = $vendorcat;
$data['categories'] = $categories;
on view:
form_dropdown('categoryid[]',$categories,$vendorcat);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks