codeigniter - How do I Populate Multi Select drop dwon using php -
here listing cars.customers want compare car select drop down. person can select multiple cars. @ first time selecting 'audi' , saab' store data base next if came need populate saab , audi select how can using php
<select name="cars" multiple> <option value="volvo">volvo</option> <option value="saab">saab</option> <option value="opel">opel</option> <option value="audi">audi</option> </select>
here code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]"> <?php if($carslist->num_rows() >0) { foreach($carslist->result_array() $entry): ?> <option value="<?php echo($entry['id']); ?>" ><?php echo($entry['car_name']); ?></option> <?php endforeach; } ?> </select>
following code tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]"> <?php if($carslist->num_rows() >0) { foreach($carslist->result_array() $entry): if($resources->num_rows() >0) { foreach($resources->result_array() $car): if($entry['id'] == $employee['car_id']) { $select = 'selected="selected"'; } else { $select = ''; } endforeach; } ?> <option value="<?php echo($entry['id']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option> <?php endforeach; } ?> </select>
but showing error
here, try this, , see if works:
here controller:
<?php function something(){ $data = array(); $data['cars'] = $this->some_model->some_function_to_return_cars_array(); $data['selected'] = $some_array_of_selected_cars(); $this->load->view('some_view', $data); } ?>
and view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]"> <option value="">select:</option> <?php foreach( $cars $key => $val ){ ?> <option value="<?php echo $val['some_id'] ?>" <?php if( in_array( $val['some_id'], $selected ) ) echo ' selected'; ?> ><?php echo $val['some_name'] ?></option> <?php } ?> </select>
Comments
Post a Comment