drlq92444 2018-09-14 15:40
浏览 92
已采纳

如何从mySQL中获取数据并在Modal中查看

I am trying to show some records from my table columns named tid and ketprob by showing Modal when clicking on a link. The modal and query looks fine (checked by echoing last_query), but the modal is showing no data... Please help me :(

JS Code:

$('#showdata').on('click', '.item-info', function(){
  var tid = $(this).attr('data');

  $.ajax({
    type: 'ajax',
    method: 'get',
    url: '<?php echo base_url() ?>repeatproblem/infoReprob',
    data: {tid:tid},
    async: false,
    dataType: 'json',
    success: function(data){
      var html = '';
      var i;
      for(i=0; i<data.length; i++){
        html +='<p>'+data[i].tid+'</p>'+
        '<p>'+data[i].ketprob+'</p>';
      }
      $('#infoModal').modal('show');
      $('#view_errorcode').html(html);
    },
    error: function(){
      alert('Gagal Info Kode Error!');
    }
  });
});

My Controller:

public function infoReprob(){ 
    $result = $this->m->infoReprob(); 
    echo json_encode($result); 
}

My Model:

public function infoReprob(){
    $tid = $this->input->get('tid');
    $this->db->select('tid, ketprob')->where('tid', $tid);
    $query = $this->db->get('histprob');
    if($query->num_rows() > 0){
        return $query->row();
    }else{
        return false;
    }
 }
  • 写回答

3条回答 默认 最新

  • duanpacan2583 2018-09-14 19:52
    关注

    You are using return $query->row(); syntax in your model if this condition is true: $query->num_rows() > 0, so that means your model will return the object representation of the first row of the query and the $result variable in your controller below will be an object with two properties: tid and ketprob

    public function infoReprob(){ 
        $result = $this->m->infoReprob(); 
        echo json_encode($result);
    }
    

    Now have a look at your ajax call success callback function

    success: function(data){
        var html = '';
        var i;
        for(i=0; i<data.length; i++){
            html +='<p>'+data[i].tid+'</p>'+
            '<p>'+data[i].ketprob+'</p>';
        }
        $('#infoModal').modal('show');
        $('#view_errorcode').html(html);
    }
    

    Since your controller above uses echo json_encode($result); syntax, your ajax call will return the json representation of $result variable and the data variable in your success callback function above will be like below

    { "tid": "1", "ketprob": "abc" }
    

    The problem is, data.length in your ajax success callback function will be undefined because data isn't an array, so the for loop won't be executed and html will be an empty string, see this jsfiddle. That's why your modal is showing no data.

    To fix the problem, I'd suggest changing your model code as below

    public function infoReprob(){
        $tid = $this->input->get('tid');
        $this->db->select('tid, ketprob')->where('tid', $tid);
        $query = $this->db->get('histprob');
        return $query->result();
    }
    

    By using return $query->result(); syntax, your model will always return an array of object. As the result, your ajax call will return a json like this

    [ { "tid": "1", "ketprob": "abc" } ]
    

    which is a json array, so data.length in your ajax success callback function won't be undefined and your modal will show the data. See this jsfiddle, you'll see that the html variable is not empty.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongqian1893 2018-09-14 16:09
    关注

    I guess you should use echo $query->row(); instead of return $query->row();.

    评论
  • doubaisui2526 2018-09-14 16:48
    关注

    Solved by changing return $query->row(); to return $query->result();

    Going to learn about this. Or can anybody tell about the different.. Thanks

    public function infoReprob(){
        $tid = $this->input->get('tid');
        $this->db->select('tid, ketprob')->where('tid', $tid);
        $query = $this->db->get('histprob');
        if($query->num_rows() > 0){
            return $query->result();
        }else{
            return false;
        }
     }
    
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Qt 不小心删除了自带的类,该怎么办
  • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
  • ¥15 有没有哪位厉害的人可以用C#可视化呀
  • ¥15 可以帮我看看代码哪里错了吗
  • ¥15 设计一个成绩管理系统
  • ¥15 PCL注册的选点等函数如何取消注册
  • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
  • ¥15 Python爬虫程序
  • ¥15 crypto 这种的应该怎么找flag?
  • ¥15 代码已写好,求帮我指出错误,有偿!