如何显示选择器中存在的动态值?

时间:2019-02-23 10:23:00

标签: php codeigniter

<select class="selectpicker" multiple data-live-search="true">
    <?php
        $this->db->select('*');
        $this->db->from('candidate_team');
        $where = "candidate_id='".$id."' and member_type='Recruiter'";
        $this->db->where($where);
        $qqs = $this->db->get();
        $result = $qqs->result_array();
        foreach($result as $row)
        {
            echo '<option value="'.$row['member_name'].'">'.$row['member_name'].'</option>';
        }
    ?>
</select>

我有一个多选下拉菜单。现在,我想要的是如果数据库中存在一个值,那么该值已经显示在select picker中。那么,我该怎么做?请帮助我。

谢谢

1 个答案:

答案 0 :(得分:0)

您需要在控制器中获取数据库记录,然后将其传递给视图以使用selectpicker显示

控制器

我想您已经配置了数据库

public function getselectpiker(){
    $this->load->model('model_name');
    $data['result'] = $this->model_name->get_data($id); // Pass candidate id here
    $this->load->view('file_name', $data);
}

模型

public function get_data($id){
    return $this->db->get_where('candidate_team', ['candidate_id' => $id,  'member_type' => 'Recruiter'])->result_array();
}

查看

<select class="selectpicker" multiple data-live-search="true">
<?php
    foreach($result as $row){
        echo '<option value="'.$row['member_name'].'">'.$row['member_name'].'</option>';
    }
?>