使用从 Mysql 数据库返回的值填充下拉列表

Populating Dropdown with values returned from Mysql database

我无法通过连接 mysql 数据库中的名字和姓氏来填充下拉列表。我尝试了一些查询,但其中 none 似乎有效。我知道我的查询有误,但无法弄清楚发生了什么。另外,我在下面粘贴了我的 MVC 代码以及我的 table.

图像

我的控制器代码是: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 table 是:

在您的 get_all_names() 函数中,您在返回结果之前回显查询和 diedie 将立即停止您的脚本。

根据您显示的 table 定义,您的查询似乎没有任何错误,但除了 SQL 语法错误外,还有许多可能的原因可能会失败。

在您的模型上试试这个 exit_common.php

<?php

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

?>

在您的 View 上,请改用以下代码

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

希望对您有所帮助