I need help for the query in my model, I want to find the field with variable that i'm input to the controller and as for right now the problem is that the field won't read my variable so here is the code
Controller
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);
}
Model
public function tampil_soal($where){
return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()");
}
View
<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>
View
<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
you are using wrong post name "$this->input->post('kode_soal')" change this to $this->input->post('kode_ujian') then
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);
}
Model
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();
}
}
I think you have to use result() function with query
public function tampil_soal($where){
return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()")->result();
}
try this query: //controller
public function tampil_soal(){
$data['tampilan']= $this->m_model->tampil_soal($this->input->post('kode_soal');
$this->load->view('soal_tampil',$data);
}
//model
public function tampil_soal($where){
return $this->db->where('kode_soal',$where)->get('soal')->result();
}
just try this:
//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
}
Model
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');
}
change this
public function tampil_soal($where){
return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()");
}
to this
public function tampil_soal($kode_soal){
return $this->db->query("select * from soal where kode_soal='$kode_soal' ORDER BY RAND()");
}
hope it'll helps