dtlygweb2017
2018-11-12 06:24
浏览 72
已采纳

Codeigniter - 带变量的mysql查询

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>

图片转代码服务由CSDN问答提供 功能建议

我需要模型中的查询帮助,我想找到我输入的变量字段 控制器和现在问题是该字段不会读取我的变量所以这里是代码

控制器 \ n

  public function tampil_soal(){
 $ kode_soal = $ this-&gt; input-&gt; post('kode_soal'); 
 $ where = array('kode_soal'=&gt; $ kode_soal)  ; 
 $ data ['tampilan'] = $ this-&gt; m_model-&gt; tampil_soal($ kode_soal) - &gt; result(); 
 $ this-&gt; load-&gt; view('soal_tampil',$ 数据); 
} 
   
 
 

模型

  public function tampil_soal($  where){
 return $ this-&gt; db-&gt; query(“select * from soal where kode_soal ='$ where'ORDER BY RAND()”); 
} 
    
 
 

查看

 &lt; form action =“&lt;?php echo base_url()?&gt; siswa / tampil_soal”  method =“post”class =“form”&gt; 
&lt; input class =“inpu  t“name =”kode_ujian“placeholder =”Kode ujian“/&gt; 
&lt; input class =”button“type =”submit“name =”submit“value =”Selesai“/&gt; 
&lt; / form&gt;  
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dongyingtang3803 2018-11-12 08:02
    已采纳

    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();
            }
        }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanfu5239 2018-11-12 06:33

    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();
        }
    
    评论
    解决 无用
    打赏 举报
  • duanlaofu4108 2018-11-12 06:44

    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();
        }
    
    评论
    解决 无用
    打赏 举报
  • doulao2916 2018-11-12 07:37

    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
    }
    
    评论
    解决 无用
    打赏 举报
  • dongxi3859 2018-11-12 08:08

    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');    
    }
    
    评论
    解决 无用
    打赏 举报
  • douxing1850 2018-11-12 08:39

    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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题