This is my Model:
public function fetch_customer_invoice($f_date,$t_date)
{
$this->db->select('invoice_id');
$this->db->from('invoice');
$this->db->where('date >=',$f_date);
$this->db->where('date <=',$t_date);
$res=$this->db->get();
foreach($res->result() as $row)
{
$invoice=$row->invoice_id;
$this->db->select('*');
$this->db->from('invoice_products');
$this->db->where('invoice_id',$invoice);
$res=$this->db->get();
return $res->result();
}
}
This is my Controller:
public function fetch_customer_invoice()
{
$from_date=$this->input->post('from_date');
$to_date=$this->input->post('to_date');
$data['res']=$this->revenue_reports_model-
>fetch_customer_invoice($from_date,$to_date);
echo json_encode($data);
}
Script:
$('input[type="date"]').change(function() {
var from_date=$("#from_date").val();
var to_date=$("#to_date").val();
$.ajax({
url: "<?php echo base_url(); ?>revenue_reports/fetch_customer_invoice/",
dataType:'json',
type: 'post',
data : {"from_date" : from_date, "to_date" : to_date},
success: function(data) {
console.log(data.res);
}
});
});
This query fetch all invoice id from invoice table for the particular date. From my table there are 2 rows from that particular date but, model return 1st row result only.Why??I don't know where the error is there whether it is in model or ajax function??? Pls help..
Hope this will help you :
IN model method your foreach
should be like this :
public function fetch_customer_invoice($f_date,$t_date)
{
$this->db->select('invoice_id');
$this->db->from('invoice');
$this->db->where('date >=',$f_date);
$this->db->where('date <=',$t_date);
$res = $this->db->get();
foreach($res->result() as $row)
{
$invoice=$row->invoice_id;
$this->db->select('*');
$this->db->from('invoice_products');
$this->db->where('invoice_id',$invoice);
$res = $this->db->get();
$data[] = $res->result();
/* Or better use $res->row() if only one record per id*/
}
return $data;
}
You can use count()
method like this :
public function fetch_customer_invoice()
{
$from_date=$this->input->post('from_date');
$to_date=$this->input->post('to_date');
$results = $this->revenue_reports_model->fetch_customer_invoice($from_date,$to_date);
$data['res']= $results;
//$data['count']= count($results);
echo json_encode($data);
}