Codeigniter-Ajax我需要返回多行,以及如何在我的视图文件中访问它


Codeigniter-Ajax i need to return more than one rows and how to access it in my view file

当我点击"购买清单"中的"发票号"时,我需要打开一个可折叠的表格,列出与发票号相匹配的产品列表我正在使用jquery打开一个可折叠表和Ajax检索数据

我的视图文件:我使用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){
               $('#invoiceno').html(result['invoiceno']);
               $('#productname').text(result['productname']);
               $('#price').text(result['price']);

        }
    });
 $(".collapse".collapse('toggle');
});
});
</script>

MY CONTROLLER FILE: i将从表

中检索不止一行
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 in your model you should verify if the data passed is legit before querying
         $data = $this->purchasemod->getproductlist($this->input->post('data'));
         $this->output->set_content_type('application/json');
        $this->output->set_output(json_encode($data));
        return $data;
                }
}
}

MY MODEL FILE: i将从表

中检索不止一行
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();
 foreach ($query->result() as $row)
  {
    return $row;
  }
  }

return $row;在循环内部将只返回一行。

代替:

foreach ($query->result() as $row)
{
    return $row;
}

只做:

return $query->result();