【问题标题】:Acessing num_rows in codeigniter and return to ajax在codeigniter中访问num_rows并返回ajax
【发布时间】:2018-07-14 08:23:39
【问题描述】:

这是我的模特:

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

此查询从发票表中获取特定日期的所有发票 ID。从我的表中,该特定日期有 2 行,但是模型仅返回第一行结果。为什么??我不知道错误在哪里,无论是模型还是 ajax 函数?请帮忙..

【问题讨论】:

    标签: php ajax codeigniter codeigniter-3 codeigniter-query-builder


    【解决方案1】:

    希望对您有所帮助:

    IN模型方法你的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);
    }
    

    【讨论】:

    • kk in ajax 如何在 for 循环中获取这些值??
    • 在js成功方法中使用每个方法如下:$.each(data.res, function(key,value) { alert(value); });
    • 我将其用于 (var i=0; i
    • 有很多方法可以做到,无论哪种适合你,请注意你的数据可能在两个循环中,所以首先使用另一个循环来访问
    • 看不到你的json数据结构做不到,请展示一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多