我需要帮助在我的模型中进行查询,我想找到我输入到控制器的带有变量的字段,目前的问题是字段不会读取我的变量,所以这里是代码
控制器
public function tampil_soal(){
$kode_soal = $this->input->post('kode_soal');
$where = array('kode_soal' => $kode_soal);
$data['tampilan']= $this->m_model->tampil_soal($kode_soal)->result();
$this->load->view('soal_tampil',$data);
}
型号
public function tampil_soal($where){
return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()");
}
查看
<form action="<?php echo base_url()?>siswa/tampil_soal" method="post" class="form">
<input class="input" name="kode_ujian" placeholder="Kode ujian"/>
<input class="button" type="submit" name="submit" value="Selesai"/>
</form>
试试这个:
//controller
public function tampil_soal(){
$kode_soal = $this->input->post('kode_ujian');
$data['tampilan']= $this->m_model->tampil_soal($kode_soal);
$this->load->view('soal_tampil',$data);
}
// model
public function tampil_soal($where){
$this->db->where('kode_soal',$where);
return $this->db->get('soal')->result();
// use the return $this->db->get('soal')->row();
// if your query return one record
}
查看
<form action="<?php echo base_url()?>siswa/tampil_soal" method="post" class="form">
<input class="input" name="kode_ujian" placeholder="Kode ujian"/>
<input class="button" type="submit" name="submit" value="Selesai"/>
</form>
控制器
您使用了错误的帖子名称"$this->input->post('kode_soal'(",将其更改为$this->input->post('kode _ujian'(然后
public function tampil_soal(){
$kode_soal = $this->input->post('kode_ujian');
$where = array('kode_soal' => $kode_soal);
$data['tampilan']= $this->m_model->tampil_soal($kode_soal);
$this->load->view('test',$data);
}
型号
public function tampil_soal($where){
$qry = $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()");
if($qry->num_rows()>0){
return $qry->result();
}else{
return array();
}
}
我认为您必须将result((函数与查询一起使用
public function tampil_soal($where){
return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()")->result();
}
尝试此查询://控制器
public function tampil_soal(){
$data['tampilan']= $this->m_model->tampil_soal($this->input->post('kode_soal');
$this->load->view('soal_tampil',$data);
}
//型号
public function tampil_soal($where){
return $this->db->where('kode_soal',$where)->get('soal')->result();
}
型号
public function tampil_soal($where){
$condition = $where;
// Your Condition...
$this->db->where($condition);
// Order By ...
$this->db->order_by('RAND()');
// Return Data from Table ...
return $this->db->get('soal');
}
更改此
public function tampil_soal($where){
return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()");
}
到这个
public function tampil_soal($kode_soal){
return $this->db->query("select * from soal where kode_soal='$kode_soal' ORDER BY RAND()");
}
希望它能帮助