如何在foreign中返回2个数据的值,每个数据在codeigniter的模型中有5行来查看

时间:2023-01-27 22:31:42

I have a similar problem that the function only return the first loop data. The result should be 2 loops and each loop has rows data. For example, The first loop is A and has 5 rows data and second loop is B and has 5 rows. The return value only A with 5 rows, it sould be 10 rows. 5 rows A + 5 rows B. How is the code should be? This function in model is bellow

我有一个类似的问题,该函数只返回第一个循环数据。结果应该是2个循环,每个循环都有行数据。例如,第一个循环是A,有5行数据,第二个循环是B,有5行。返回值只有A有5行,它应该是10行。 5行A + 5行B.代码应该如何?模型中的此功能如下所示

function getDataForm($data_search)
{
    $i=0;
    if(!empty($data_search['arr_idacl']))
    {
        foreach($data_search['arr_idacl'] as $idx => $val)
        {
            if(!empty($operatingHour))
            {
                $sql = "SELECT be_acl_resources.description as description,DATE_FORMAT(tbl_camera_detail.start_time,'%d %M %Y ') as datadate, SUM(IF(tbl_consolid_detail.operation = 'plus' AND tbl_consolid_detail.direction = 'IN',tbl_camera_detail.enters,0))+SUM(IF(tbl_consolid_detail.operation = 'minus' AND tbl_consolid_detail.direction = 'IN',tbl_camera_detail.enters*(-1),0))+SUM(IF(tbl_consolid_detail.operation = 'plus' AND tbl_consolid_detail.direction = 'OUT',tbl_camera_detail.exits,0))+SUM(IF(tbl_consolid_detail.operation = 'minus' AND tbl_consolid_detail.direction = 'OUT',tbl_camera_detail.exits*(-1),0)) as total_enter FROM tbl_consolid JOIN tbl_consolid_detail ON tbl_consolid.id = tbl_consolid_detail.consolid_id JOIN tbl_config_data ON tbl_consolid_detail.config_data_id = tbl_config_data.id JOIN tbl_camera ON tbl_config_data.id_acl = tbl_camera.id_acl JOIN tbl_camera_detail ON tbl_camera.id = tbl_camera_detail.camera_id JOIN be_acl_resources ON tbl_consolid.id_acl = be_acl_resources.id WHERE tbl_camera.date >= '".$data_search['start_date']."' AND date_format(tbl_camera_detail.start_time, '%H, %i %S') >= '".$startOpratingHour."' AND date_format(tbl_camera_detail.start_time, '%H, %i %S') <= '".$operatingHour['finish_time']."' AND tbl_camera.date <= '".$data_search['end_date']."' AND tbl_consolid.id_acl = '".$val."' GROUP BY tbl_camera.date";               
                echo $sql;               
                $query = $this->db->query($sql);
                $result_data[] = $query->result();
                //return $result;
            }
            $i++;
        }
        return $result_data;
    }   
}

1 个解决方案

#1


2  

  1. Declare $result_data = array(); just before if(!empty($data_search['arr_idacl'])) condition.
  2. 声明$ result_data = array();就在if(!empty($ data_search ['arr_idacl']))条件之前。

  3. Replace $result_data[] = $query->result(); with $result_data[$i] = $query->result();
    Update:
    Just close to the answer. :) Try this:
    Replace $result_data[] = $query->result(); with $result_data[$i][] = $query->result();
  4. 替换$ result_data [] = $ query-> result(); with $ result_data [$ i] = $ query-> result();更新:接近答案。 :)试试这个:替换$ result_data [] = $ query-> result(); with $ result_data [$ i] [] = $ query-> result();

#1


2  

  1. Declare $result_data = array(); just before if(!empty($data_search['arr_idacl'])) condition.
  2. 声明$ result_data = array();就在if(!empty($ data_search ['arr_idacl']))条件之前。

  3. Replace $result_data[] = $query->result(); with $result_data[$i] = $query->result();
    Update:
    Just close to the answer. :) Try this:
    Replace $result_data[] = $query->result(); with $result_data[$i][] = $query->result();
  4. 替换$ result_data [] = $ query-> result(); with $ result_data [$ i] = $ query-> result();更新:接近答案。 :)试试这个:替换$ result_data [] = $ query-> result(); with $ result_data [$ i] [] = $ query-> result();