2016-11-16 93 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>