2016-08-02 80 views
0

我無法通過連接mysql數據庫中的名字和姓氏來填充下拉列表。我嘗試了一些查詢,但沒有一個似乎工作。我知道我在查詢中有錯誤,但無法弄清楚發生了什麼。我也粘貼了我的MVC代碼以及我的表格的圖像。使用從Mysql數據庫返回的值填充下拉列表

我的控制器的代碼是:exits.php

function admin_add_absconding(){ 
    global $SITE,$USER; 
    $data = array(); 
    $data['row'] = new stdClass(); 
    $data['row'] = $this->admin_init_elements->set_post_vals($this->input->post()); 
    $data['offices']=$this->mod_common->get_all_offices(); 
    $clients = currentuserclients(); 
    $data['roles'] = $this->mod_common->get_cat_array('designation','status',"1' AND id > '0",'designation'); 
    get_city_state_country_array($data,array('cityid'=>$data['row']->cityid)); 
    $data['error_message'] = ''; 
    $data['row']->id = $this->uri->segment(3); 
    $data['id'] = $this->uri->segment(3); 
    $data['action'] = 'add'; 
    $data['heading'] = 'Add'; 
    $data['msg_class'] = 'sukses'; 
    $data['path']=$path; 
    $post_action = $this->input->post('action'); 
    $data['groups'] = $this->exit_common->get_all_names(); 

    if($post_action=='add' || $post_action =='update'){ 
     $post_array = $this->input->post(); 
     $action = ($post_action == 'add')?'inserted':'updated'; 
     //echo '<pre>';print_r($SITE);die; 
     echo $post_array['exit_type'] = 'Employee Initiated'; 

     if($data['error_message'] == 'Record '.$action.' successfully'){ 
      $data['row'] = new stdClass(); 
      $data['row']->id = $this->uri->segment(3); 
      $data['row']->status = 1; 
     } 

    } 

我的模型代碼是:exit_common.php

function get_all_names(){ 

    $query = $this->db->query('SELECT firstname,lastname FROM pr_users_details'); 
    echo $this->db->last_query(); 
    die; 

    return $query->result(); 
} 

我的視圖的代碼是:backend_add_new_exit.php

<select class="form-control"> 
    <?php 

    foreach($groups as $row) 
    { 
     echo '<option value="'.$row->firstname.'">'.$row->lastname.'</option>'; 
    } 
    ?> 
</select> 

我Mysql表是: enter image description here

+0

什麼是錯誤?解釋更多。不清楚 –

+0

爲什麼不在MySQL中創建名稱:SELECT concat(firstname,'',lastname)作爲名稱 – user3741598

+0

解決了..但是視圖未從控制器正確呈現 – shank

回答

1

在您的get_all_names()函數中,您在返回結果之前回顯查詢和diedie將立即停止您的腳本。

根據您所展示的表定義,您的查詢似乎沒有任何錯誤,但有很多可能的原因可能導致SQL語法錯誤以外的錯誤。

0

試試這個模型上的exit_common.php

<?php 
 

 
function get_all_names(){ 
 
\t \t $query = $this->db->get('pr_users_details'); 
 
\t \t if ($query->num_rows() > 0) 
 
\t \t { 
 
\t \t return $query->result(); 
 
\t \t } 
 
\t } 
 

 
?>

在您瀏覽使用下面的代碼,而不是

<select class="form-control"> 
 
<?php foreach($groups as $row):?> 
 
\t <option value="<?php echo $row->firstname; ?>"> <?php echo $row->lastname; ?> </option> 
 
\t <?php endforeach; ?> 
 
</select>

希望能幫到