根据列 codeigniter 对行进行分组
group rows based on a column codeigniter
我想根据列对行进行分组。我附上了我的 table
我试过了,我的问题是,当使用 group_by 时,每个用户只会 return 一行。我想要这个 table 分组的全部数据 user_id
型号
public function get_pdf_print( $start_date, $end_date) {
$this->db->select('*');
$this->db->from('jil_invoices');
$this->db->group_by('jil_invoices.inv_userid');
$this->db->where('jil_invoices.inv_dated >=', $start_date);
$this->db->where('jil_invoices.inv_dated <=', $end_date);
$query = $this->db->get();
return $query->result();
}
如果您想将 GROUP BY
与 userid 列一起使用,请注意它不会 return 所有行。
我建议您获取所有行,而不是使用 PHP 进行分组,例如:
在您的模型中:
public function get_pdf_print($start_date, $end_date) {
$this->db->select('*');
$this->db->from('jil_invoices');
$this->db->where('jil_invoices.inv_dated >=', $start_date);
$this->db->where('jil_invoices.inv_dated <=', $end_date);
$query = $this->db->get();
return $query->result_array(); // here i am using result_array function will return result into array.
}
在您的控制器中:
$records = $this->model_yourmodel->get_pdf_print($startDate,$endDate);
$userData = array();
foreach ($records as $key => $value) {
$userData[$value['inv_userid']][] = $value; // get all record against each user
}
我想根据列对行进行分组。我附上了我的 table 我试过了,我的问题是,当使用 group_by 时,每个用户只会 return 一行。我想要这个 table 分组的全部数据 user_id 型号
public function get_pdf_print( $start_date, $end_date) {
$this->db->select('*');
$this->db->from('jil_invoices');
$this->db->group_by('jil_invoices.inv_userid');
$this->db->where('jil_invoices.inv_dated >=', $start_date);
$this->db->where('jil_invoices.inv_dated <=', $end_date);
$query = $this->db->get();
return $query->result();
}
如果您想将 GROUP BY
与 userid 列一起使用,请注意它不会 return 所有行。
我建议您获取所有行,而不是使用 PHP 进行分组,例如:
在您的模型中:
public function get_pdf_print($start_date, $end_date) {
$this->db->select('*');
$this->db->from('jil_invoices');
$this->db->where('jil_invoices.inv_dated >=', $start_date);
$this->db->where('jil_invoices.inv_dated <=', $end_date);
$query = $this->db->get();
return $query->result_array(); // here i am using result_array function will return result into array.
}
在您的控制器中:
$records = $this->model_yourmodel->get_pdf_print($startDate,$endDate);
$userData = array();
foreach ($records as $key => $value) {
$userData[$value['inv_userid']][] = $value; // get all record against each user
}