Доступ к num_rows в codeigniter и возврат в ajax

Это моя модель:

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();
}
}

Это мой контроллер:

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

Автор сценария:

$('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);
}
});
});

Этот запрос извлекает все идентификаторы счетов из таблицы счетов на определенную дату. Из моей таблицы есть 2 строки с этой конкретной даты, но модель возвращает только 1-й результат строки. Почему? Я не знаю, где ошибка, в модели или в функции ajax ??? Пожалуйста помогите ..

1

Решение

Надеюсь, что это поможет вам :

В модельном методе ваш foreach должно быть так:

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

Ты можешь использовать count() метод как это:

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);
}
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector