I have query automatic, i using code igniter,
Structure table organization have field id and name.
$get_organization = $this->db->query("SELECT id,name FROM organization")->result_array();
if i print_r variable $get_organization as below:
Array (
[0] => Array
(
[id] => 1
[name] => orgone
)
[1] => Array
(
[id] => 2
[name] => orgtwo
)
)
And for i get sum automatic i write code as below:
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);
And in table are looking to search the results is laporan with field id, total, tanggal_laporan, org_id.
$query = $this->db->query("SELECT id, $cabang, tanggal_laporan FROM laporan GROUP BY tanggal_laporan")->result_array();
And then if i print_r variable $query i have result as below:
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
)
)
So my question, i want to result automatic as below:
$array_transform = array(
[orgone] => "218000, 198000, 411000",
[orgtwo] => "112000, 411000, 147000",
[tanggal_laporan] => "2012-11-04, 2012-11-05, 2012-11-06",
);
And when it comes one more row of the table organization for example one row with name=orgthree, so result i want as:
$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",
);
Thanks.
Updated answer after the question was changed. This will process the fields into the structure you want withhout having to hard code the field names.
Demo