【问题标题】:Codeigniter- Ajax Codeigniter Ajax -Retrurning more than one row through ajaxCodeigniter- Ajax Codeigniter Ajax - 通过ajax返回不止一行
【发布时间】:2017-03-23 05:49:55
【问题描述】:

我如何从数据库表中检索多行并通过控制器在视图文件中访问它:)

我正在使用 AJAX 检索数据 我的查看文件:

<script>
$(document).ready(function(){
$(".listproduct".click(function(){
    var value = $(this).text();
$.ajax({
        type:'POST',
        url:'<?=site_url("purchasecont/getproductlist"; ?>',
        data: {'data' : value} ,
        success:function(result){
         console.log(result);
         for(i=0;i<result['count'];i++){
            var table = $('#products'); 
    var tr = (
    '<tr>' +
    '<td>'+ result[i]['invoiceno']; +'</td>'+
    '<td>'+ result[i]['price']; +'</td>'+
    '</tr>'
     );
  $('#products').append(tr);

         }
    }

    });
 $(".collapse".collapse('toggle');

});
});
</script>

我的控制器文件:我将从表中检索不止一行

public function getproductlist(){
//check if is an ajax request
if($this->input->is_ajax_request()){
    //checks if the variable data exists on the posted data
    if($this->input->post('data')){


    $query = $this->purchasemodel->getproductlist($this->input>post('data'));
         $data['records'] = $query['records'];
        $data['count'] = $query['count']; 
          $this->output->set_content_type('application/json');
        $this->output->set_output(json_encode($data));
        return $data;
                }
    }
  }

我的模型文件:我将从表中检索不止一行

 public function getproductlist($q){
  $this->db->select('*');    
    $this->db->from('purchaseprolist');
   $this->db->where('purchaseprolist.invoice' , $q);
    $this->db->where('purchaseprolist.price != ',0,FALSE);
    $query = $this->db->get();
    $row = $query->result();
    return array(
'records' => $row,
'count' => count($row),
);

}

My Table 显示数据: 我不知道如何在此处显示它,请帮助我完成

<table id="products">                                
    <tbody>
        <tr>
            <td>Invoice</td>
            <td>price</td>
            <td id="quantity"></td>
         </tr>
     </tbody>
</table>

【问题讨论】:

    标签: php mysql ajax codeigniter


    【解决方案1】:

    如果您从数据库中检索的结果包含多个元素,您需要在视图中使用循环以使用元素(例如 DataTable)填充 html。您的代码应如下所示:

    $(document).ready(function(){
    $(".listproduct".click(function(){
        var value = $(this).text();
        $.ajax({
            type:'POST',
            url:'<?=site_url("purchasecont/getproductlist"; ?>',
            data: {'data' : value} ,
            success:function(result){
                 for(i=0;i<result.length;i++){//loop trough elements
                   $('#myTable tr:last').after('<tr><td>' + $('#invoiceno').html(result[i]['invoiceno']); + '</td><td>' + $('#productname').text(result[i]['productname']); + '</td><td>' + $('#price').text(result[i]['price']); + '</td></tr>');
                 }
            }
        });
     $(".collapse".collapse('toggle');
    
    });
    });
    

    【讨论】:

    • 在你的视图中注意模型返回的结果,例如如果你从ajax请求返回JSON,你需要先反序列化结果,然后再在循环和html中使用它。
    • 我很抱歉,但我如何在行表中显示它:) 我不知道如何访问 ajax 结果并使用它:)
    • 我该如何反序列化它?
    • 使用console.log(result);并查看结果的结构。如果要创建html表,请使用javascript动态生成html。
    • 我做到了,我会在上面更新它:) 只需检查我是否做得对
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多