【问题标题】:How to fetch data in multiple multidimensional array from mysql using codeigniter?如何使用codeigniter从mysql中获取多个多维数组中的数据?
【发布时间】:2022-01-16 17:44:43
【问题描述】:

我正在编写一份报告,将根据部门明智地获取所有员工及其工资详细信息。我已经使用多维数组成功地按部门获取了员工,但现在我需要在该员工详细信息多维数组上获取employees_salary_detail。这意味着第一个部门->emp_detail->salarydetail。我已成功获取前两部分,但现在我在获取该 emp_detail 数组中的最后一个数组时遇到了问题。

public function getDepartmentReport(){
        $employee = $this->db->select('*')
                ->from('departments')
                ->where('project_id', $this->session->userdata('client_id'))->get()->result_array();

                $data = array();

        foreach($employee as $m => $v){
       
            $v['emp_detail'] =  $this->db->select('first_name,employee_code,employees_salary.*')
                                         ->from('employees')
                                         ->join('employees_salary', 'employees_salary.employee_id = employees.id')
                                         ->where('employees.department_id',$v['id'])
                                         ->where('employees_salary.month', 'Nov')
                                         ->get()->result_array();
                                         $data[] = $v;         
   

            foreach($v['emp_detail'] as $m => $s){
                $s['salary_detail'] = $this->db->select('*')
                ->from('employees_salary_detail')->where('employees_salary_detail.salary_id', $s['id'])
                ->get()->result_array();  
                $data[] = $s;         

            }
            
        }
        return $data;
   }

但是现在它正在创建单独的数组来显示不在那个 emp_detail 数组中的工资详细信息。 我不知道我在哪里犯错。请帮我解决这个问题。

提前感谢您的帮助

【问题讨论】:

    标签: php mysql arrays codeigniter multidimensional-array


    【解决方案1】:

    使用 Join 方法根据公共键连接数据库中的所有三个表。我可以看到你已经加入了两个表,就像你可以加入多个表并创建一个数据数组一样。

    $qry = $this->db->query("SELECT  * FROM product_section INNER 
    JOIN products ON product_section.ps_prid = products.prid INNER 
    JOIN wishlist ON wishlist.wish_product_id = products.product_id 
    INNER JOIN customer ON wishlist.wish_user_id = customer.cust_id
    INNER JOIN brands ON brands.brand_id = products.product_brand
    WHERE customer.cust_id = '$cid' GROUP BY  
    product_section.ps_prid");
    

    像上面这样的代码

    【讨论】:

      猜你喜欢
      • 2012-07-21
      • 2015-11-04
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多