2016-11-16 70 views
0

我有選定的下拉列表的問題,我點擊表單edit.dropdown不能選擇空(選擇)如果數據沒有在數據庫中。 我想,如果數據爲空,下拉像這樣: enter image description here 所以在這裏我的代碼:編輯窗體選定的值輸入

//mycontroller 
 
public function edit($id) { 
 
     $data = array(
 
      'title'  => 'Users', 
 
      'breadcrumb'=> 'User Edit', 
 
      'groups' => $this->groups->get_all(), 
 
      'position' => $this->users->get_position(), 
 
      'report' => $this->users->get_leader(), 
 
      'users'  => $this->users->get_by(array('nik' => $id)), 
 
      'content' => 'edit' 
 
     ); 
 
     $this->load->view ('default', $data); 
 
    } 
 

 
//my model 
 
public function get_leader() 
 
\t { 
 
\t \t $idstat=array(4); 
 
\t \t $this->db->select('nik'); 
 
\t \t $this->db->select('username'); 
 
\t \t $this->db->from('t_mtr_employee'); 
 
\t \t $this->db->where_in('t_mtr_employee.group_id',$idstat); 
 
\t \t $query= $this->db->get(); 
 
\t \t return $query->result(); 
 

 
\t }

<div class="form-group"> 
 
    <label class="control-label col-md-3">Reporting To<span 
 
    class="required"> * </span></label> 
 
    <div class="col-md-6"> 
 
    <select class="form-control" name="reporting"> 
 
     <option disabled>Choose</option> 
 
     <?php 
 
     $id = $users->nik; 
 
     foreach($report as $report) { 
 
     $selected = $id == $report->nik ? 'selected' : ''; 
 
     echo '<option '.$selected.' value="'.$report->nik.'">'.$report->username.'</option>'; 
 
     } 
 
     ?> 
 
             
 
    </select> 
 
    </div> 
 
</div>

代碼視圖上面不能顯示在數據下拉選擇,我想當我點擊窗體編輯下拉選擇。 哪裏是我的錯碼,如何解決?

回答

0

下面是我用來弄清楚這個過程......我已經在所有的調試 - 測試/驗證中留下了證明這個概念的額外獎勵,以幫助展示如何計算這些東西......

我希望它有用

<?php 
/** 
* Test code for Selection dependant upon database entries 
*/ 

/** 
* So we want to... 
* 1. Build the Selections from the Databsae 
* 2. Set the id to choose the selection 
* 
* @var object $users 
*/ 
// Simulate the Data from the Database for creating the Selection Dropdown 
$reports_to_object = [ 
    (object) [ 'nik' => 1, 'username' => 'Fred' ], 
    (object) [ 'nik' => 2, 'username' => 'Sam' ], 
    (object) [ 'nik' => 3, 'username' => 'George' ] 
]; 
$reports = (object) $reports_to_object; 

// We expect when there is no entry from the Database 
// our id will be set to 0. 

$id = 3; // Change this for testing 
// Results: 
// 0 = Choose - Is correctly Selected 
// 1 = Fred - Is correctly Selected 
// 2 = Sam - Is correctly Selected 
// 3 = George - Is correctly Selected 
var_dump($reports); // make sure our mockup data is correct 
?> 
<form> 
    <div class="form-group"> 
     <label class="control-label col-md-3">Reporting To<span 
       class="required"> * </span></label> 
     <div class="col-md-6"> 
      <select class="form-control" name="reporting"> 
       <?php 
       // DEBUG - Remove this as we are manually setting it. 
       // $id = $users->nik; 
       $selected = ($id == 0) ? 'selected' : ''; 
       echo '<option value="0" ' . $selected . '>Choose</option>'; 
       foreach ($reports as $report) { 
        $selected = ($id == $report->nik) ? 'selected' : ''; 
        echo '<option value="' . $report->nik . '" ' . $selected . '>' . $report->username . '</option>'; 
       } 
       ?> 
      </select> 
     </div> 
    </div> 
</form>