无法从数据库中的另一个表获取价值

时间:2019-11-28 12:34:17

标签: php mysql database codeigniter

我的问题是我无法从带有Codeigniter的数据库中获得第二个表

模型

public function ambil($id = FALSE){
    if($id===FALSE){
        $query = $this->db->query('SELECT * FROM artikel,kategori WHERE artikel.id_kategori = kategori.id_kategori');
        return $query->result_array();
    }
    $query = $this->db->get_where('artikel',array('id'=>$id));
    return $query->row_array();
}

控制器

public function edit($id){
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('judul', 'judul', 'required');
        $this->form_validation->set_rules('konten','konten','required');
        $this->form_validation->set_rules('id_kategori','id_kategori','required');

        if($this->form_validation->run()===false){
            $data['artikel'] = $this->m_daftar->ambil($id);
            $data['kategori'] = $this->m_daftar->get_kategori();
            $this->load->view('_atas');
            $this->load->view('v_daftar_edit',$data);
            $this->load->view('_bawah');
        }else{
            $this->m_daftar->update($id);
            redirect('/daftar','refresh');
        }
    }

查看:我需要来自“类别”的值

<select name="id_kategori" id="id_kategori" class="form-control">
<option selected>kategori</option>
    <?php foreach($artikel as $item){
        echo '<option value='.$item['id_kategori'].'>'.$item['kategori'].'</option>';
    }?>
    </select>

2 个答案:

答案 0 :(得分:0)

将另一个函数名称写为 get_kategori($ id): 型号:

public function get_kategori($id){
        $query = $this->db->query('SELECT * FROM kategori WHERE id = '.$id.'');
        return $query->row_array();
}

控制器

public function edit($id){
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('judul', 'judul', 'required');
        $this->form_validation->set_rules('konten','konten','required');
        $this->form_validation->set_rules('id_kategori','id_kategori','required');

        if($this->form_validation->run()===false){
            $data['artikel'] = $this->m_daftar->ambil($id);
            $data['kategori'] = $this->m_daftar->get_kategori($id);//Modified param
            $this->load->view('_atas');
            $this->load->view('v_daftar_edit',$data);
            $this->load->view('_bawah');
        }else{
            $this->m_daftar->update($id);
            redirect('/daftar','refresh');
        }
    }

答案 1 :(得分:0)

我不确定您可能需要这个吗?

offsetRadius