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



我的问题是我无法使用代码点火器从数据库获取我使用的第二个表

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');
}
}

查看:我需要来自"kategori"的值

<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>

我不确定您是否需要这个:

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

将另一个函数名称写为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');
}
}

相关内容

  • 没有找到相关文章

最新更新