php - Codeigniter- Ajax Codeigniter Ajax - 通过 ajax 返回多行

标签 php mysql ajax codeigniter

我如何从数据库表中检索多行并通过 Controller 在 View 文件中访问它:)

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

<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>

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

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),
);

}

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

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

最佳答案

如果您从数据库中检索的结果有多个元素,您需要在 View 中使用循环,以便用元素(例如 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');

});
});

关于php - Codeigniter- Ajax Codeigniter Ajax - 通过 ajax 返回多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40506625/

相关文章:

PHP + MySQL : Big CSV file import

php - foreach PHP 删除 MySQL 数据库

sql - 我怎样才能使用mysql查询删除表中的第n行

jquery - 在哪里可以找到合适的 jQuery ajax 标题设置文档?

php - 无法登录和重定向 mysql php

javascript - JavaScript 中的加密和 PHP 中的解密

php - 使用邻接列表模型管理 MySQL 中的分层数据

MySQL 滞后列 DATEDIFF

php - 如何使用 AJAX 更新选择菜单 w/out <div> inside form

javascript - 动态添加字段的 jQuery 表单验证