php - How set option value selected for select tag with existing values -


the $output array return option vlaues select tag print <?php print_r($store);?> , when tried update form need show store selected value , select list must have other existing values same before come $output.

model:

public function getstore() {         $this->db->select('*');         $store = $this->db->get('tbl_store_info');         $data = $store->result();          $output = array();         $output[]='<option value="" disabled selected>select store</option>';         foreach ($data $row) {             $output[]= '<option value="' . $row->store_id . '">' . $row->store . '</option>';         }         return $output;     } 

controller:

public function editemployee($id='') {         $empdata=  $this->commonmodel->getemployee_by_id($id);         $data['emp_list'] = $empdata[0];          $data['store']=  $this->commonmodel->getstore();         $data['user_role']=  $this->commonmodel->getuserrole();         $data['msg']=  $this->session->flashdata('usermsg');          $this->load->view('common/topnav');         $this->load->view('common/sidebar');         $this->load->view('common/header');         $this->load->view('common/title-header');         $this->load->view('employee/editemployee',$data);         $this->load->view('common/footer');     } 

view:

<div class="form-group col-sm-5">  <label for="store">store:</label>   <select class="form-control" id="store" name="store" required>     <option value="<?php echo $emp_list->store_id; ?>" selected><?php echo $emp_list->store; ?></option>     <?php print_r($store);?>   </select> </div> 

i want <option value="<?php echo $emp_list->store_id; ?>" selected><?php echo $emp_list->store; ?></option> selected value.

since building html inside model (which bad practice) 1 view have no way of selecting based on id.

i rewrite whole thing output html in view not model.

but if looking quick fix send $id controller model , have inside foreach this:

foreach ($data $row) {   $option_selected = '';   if ($row->store_id == $id) {     $option_selected = 'selected';   }   $option_str = "<option value='$row->store_id' $option_selected>$row->store</option>"; } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -