简体   繁体   中英

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

when i click on the 'invoice number' in my 'purchase list' i will need to open up a collapsible table that will list the products list matching the invoice no I am using jquery to open up a collapsible table and Ajax to retrieve the data

MY VIEW FILE: I am using AJAX to retrieve the data

<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 will be retrieving more than one row from the table

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 will be retrieving more than one row from the table

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; inside of a loop will return only one row.

Instead of:

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

Just do:

return $query->result();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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