首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Codeigniter- Ajax Codeigniter Ajax -Retrurning通过ajax多行

Codeigniter- Ajax Codeigniter Ajax -Retrurning通过ajax多行
EN

Stack Overflow用户
提问于 2016-11-09 20:05:46
回答 1查看 85关注 0票数 3

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

我正在使用AJAX来检索我的视图文件中的数据:

代码语言:javascript
复制
<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>

我的控制器文件:我将从表中检索多行

代码语言:javascript
复制
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;
                }
    }
  }

我的模型文件:我将从表中检索多行

代码语言:javascript
复制
 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),
);

}

我的表格显示数据:我不知道如何在这里显示它,请帮助我通过

代码语言:javascript
复制
<table id="products">                                
    <tbody>
        <tr>
            <td>Invoice</td>
            <td>price</td>
            <td id="quantity"></td>
         </tr>
     </tbody>
</table>
EN

回答 1

Stack Overflow用户

发布于 2016-11-09 20:10:26

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

代码语言:javascript
复制
$(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');

});
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40506625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档