简体   繁体   English

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

[英]How to fetch data from mySQL and view in Modal

I am trying to show some records from my table columns named tid and ketprob by showing Modal when clicking on a link. 我试图通过单击链接时显示Modal来显示名为tidketprob的表列中的一些记录。 The modal and query looks fine (checked by echoing last_query), but the modal is showing no data... Please help me :( 模态和查询看起来很好(通过回显last_query进行检查),但是模态没有显示数据...请帮助我:(

JS Code: JS代码:

$('#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;
    }
 }

You are using return $query->row(); 您正在使用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 如果满足此条件,则模型中的语法为: $query->num_rows() > 0 ,这意味着模型将返回查询第一行的对象表示,并且下面的控制器中的$result变量将为对象具有两个属性: tidketprob

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

Now have a look at your ajax call success callback function 现在看看您的ajax调用成功回调函数

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); 由于上面的控制器使用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 语法,您的ajax调用将返回$result变量的json表示,并且您上面的成功回调函数中的data变量将如下所示

{ "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 . 问题是,由于data不是数组,因此ajax成功回调函数中的data.length将是不确定的,因此不会执行for循环,并且html将为空字符串,请参阅此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(); 通过使用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 结果,您的ajax调用将返回这样的json

[ { "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. 这是一个json数组,因此不会在ajax成功回调函数中定义data.length ,而您的模式将显示数据。 See this jsfiddle , you'll see that the html variable is not empty. 看到这个jsfiddle ,您将看到html变量不为空。

I guess you should use echo $query->row(); 我猜你应该使用echo $query->row(); instead of return $query->row(); 而不是return $query->row(); .

Solved by changing return $query->row(); 通过更改return $ query-> row();来解决 to return $query->result(); 返回$ 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;
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM