我有自动查询,我使用代码点火器,结构表组织有字段 ID 和名称。
$get_organization = $this->db->query("SELECT id,name FROM organization")->result_array();
如果我 print_r 变量 $get_organization 如下:
Array (
[0] => Array
(
[id] => 1
[name] => orgone
)
[1] => Array
(
[id] => 2
[name] => orgtwo
)
)
对于我得到自动总和,我编写如下代码:
foreach($get_organization as $bit){
$id=$bit['id'];
$name=preg_replace("/[^a-zA-Z0-9\-]/", "", $bit['name']);
$getcabang[] = "SUM(CASE WHEN org_id = '$id' THEN total END) as $name";
}
$cabang = implode(',',$getcabang);
并且在表中正在寻找搜索结果是带有字段id, total, tanggal_laporan, org_id 的laporan。
$query = $this->db->query("SELECT id, $cabang, tanggal_laporan FROM laporan GROUP BY tanggal_laporan")->result_array();
然后如果我 print_r 变量 $query 我得到如下结果:
Array (
[0] => Array
(
[orgone] => 218000
[orgtwo] => 112000
[tanggal_laporan] => 2012-11-04
)
[1] => Array
(
[orgone] => 198000
[orgtwo] => 411000
[tanggal_laporan] => 2012-11-05
)
[2] => Array
(
[orgone] => 513000
[orgtwo] => 147000
[tanggal_laporan] => 2012-11-06
)
)
所以我的问题,我想自动生成如下:
$array_transform = array(
[orgone] => "218000, 198000, 411000",
[orgtwo] => "112000, 411000, 147000",
[tanggal_laporan] => "2012-11-04, 2012-11-05, 2012-11-06",
);
当它再出现一行表格组织时,例如一行名称=orgthree,所以我想要的结果是:
$array_transform = array(
[orgone] => "218000, 198000, 411000",
[orgtwo] => "112000, 411000, 147000",
[orgthree] => "0, 0, 0",
[tanggal_laporan] => "2012-11-04, 2012-11-05, 2012-11-06",
);
谢谢。