1

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

Pradeep
  • 9,667
  • 13
  • 27
  • 34
sridhar pv
  • 134
  • 2
  • 13

1 Answers1

1

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);
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • kk in ajax how can I get those values in for loop?? – sridhar pv Jul 14 '18 at 09:10
  • use each method in js success method like this : `$.each(data.res, function(key,value) { alert(value); });` – Pradeep Jul 14 '18 at 09:17
  • I am using this for (var i=0; i – sridhar pv Jul 14 '18 at 09:26
  • there are many ways to do it whichever works for u, note that ur data may be in two loop so use another loop within first to access – Pradeep Jul 14 '18 at 09:28
  • without seeing ur json data structure unable to do, pls show it – Pradeep Jul 14 '18 at 09:38
  • [{…}] 0:{invoice_id: "SV-CI-1-07-18", product_id: "SV-CI-3-07-18-91", description: "enada", hsn_sac: "df", quantity: "4", …} length:1 __proto__ : Array(0) – sridhar pv Jul 14 '18 at 09:41
  • try with this in success : `var json =[{invoice_id: "SV-CI-1-07-18", product_id: "SV-CI-3-07-18-91", description: "enada", hsn_sac: "df", quantity: "4"}]; $.each(json, function () { $.each(this, function (name, value) { console.log(name + '=' + value); }); });` – Pradeep Jul 14 '18 at 09:55
  • From this how can I get particular value only like product_id and quantity – sridhar pv Jul 14 '18 at 10:00
  • use this code to get the particular key : `var json =[{invoice_id: "SV-CI-1-07-18", product_id: "SV-CI-3-07-18-91", description: "enada", hsn_sac: "df", quantity: "4"}]; $.each(json, function () { console.log(this.invoice_id); console.log(this.quantity); });` – Pradeep Jul 14 '18 at 10:06