I've had around 2 hours sleep in 36 hours so I may have minor over sights here but Here is the link of my previous post to get to where I am now: Pivot MySQL Table with Dynamic Columns
With the help of that post I was able to generate a dynamic SQL query using PDO and PHP. In comparison to my attempts earlier in manipulating the array and displaying it in a tabular format I am completely stumped in this.
Here is the query:
$sql = "
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN EXTRACT(YEAR_MONTH FROM month_payment) = ',
EXTRACT(YEAR_MONTH FROM month_payment),
' THEN AMOUNT ELSE 0 END) AS `',
EXTRACT(YEAR_MONTH FROM month_payment),
'`'
)
) AS `pivot_columns`
FROM record_payment
WHERE month_payment BETWEEN ? AND ?
";
$stmt = $pdo->prepare($sql);
$date_from = '2015-01-01';
$date_to = '2017-08-01';
$stmt->execute([$date_from, $date_to]);
$row = $stmt->fetch();
$stmt->closeCursor();
$pivot_columns = $row['pivot_columns'];
$sql = "
SELECT title AS `Payment Method`, {$pivot_columns}
FROM record_payment t1
JOIN setting_payment_method spm ON spm.id = t1.method_id
WHERE month_payment BETWEEN ? AND ?
GROUP BY title WITH ROLLUP
";
echo $sql;
$stmt = $pdo->prepare($sql);
$stmt->execute([$date_from, $date_to]);
$results = $stmt->fetchAll();
$stmt->closeCursor();
var_dump($results);
Here is a vardump of my array:
array (size=3)
0 =>
array (size=9)
'Payment Method' => string 'Cash' (length=4)
0 => string 'Cash' (length=4)
201704 => string '1500' (length=4)
201705 => string '120' (length=3)
201701 => string '800' (length=3)
201706 => string '800' (length=3)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
1 =>
array (size=9)
'Payment Method' => string 'Cheque' (length=6)
0 => string 'Cheque' (length=6)
201704 => string '10' (length=2)
201705 => string '0' (length=1)
201701 => string '590' (length=3)
201706 => string '590' (length=3)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
2 =>
array (size=9)
'Payment Method' => null
0 => null
201704 => string '1510' (length=4)
201705 => string '120' (length=3)
201701 => string '1390' (length=4)
201706 => string '1390' (length=4)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
UPDATED: This is how the dump looks after this manipulation
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
}
var_dump($temp);
BELOW
array (size=3)
'Cash' =>
array (size=7)
'Payment Method' => string 'Cash' (length=4)
0 => string 'Cash' (length=4)
201704 => string '1500' (length=4)
201705 => string '120' (length=3)
201701 => string '800' (length=3)
201706 => string '800' (length=3)
201707 => string '120' (length=3)
'Cheque' =>
array (size=7)
'Payment Method' => string 'Cheque' (length=6)
0 => string 'Cheque' (length=6)
201704 => string '10' (length=2)
201705 => string '0' (length=1)
201701 => string '590' (length=3)
201706 => string '590' (length=3)
201707 => string '0' (length=1)
'' =>
array (size=7)
'Payment Method' => null
0 => null
201704 => string '1510' (length=4)
201705 => string '120' (length=3)
201701 => string '1390' (length=4)
201706 => string '1390' (length=4)
201707 => string '120' (length=3)
My issue are this:
- The keys of the array is generated dynamically from the user interface. They select between 2 date fields and the query extracts the year and month from it and sums up the values. e.g 201504 = April 2015 but an index key (0) comes from nowhere and just replicates the payment method. The Payment method should be at the top level with the date as the key and the aggregated amount the key pair value.
- I have tried manipulating the array by doing a nested loop and
creating an array from the subarray but with little result. I have
tried splicing array, slicing the array, shifting the array,
flattening the array, merging the array and many other things in order to manipulate and format the array.
This is how the table comes out: whilst using a nested loop and echoing the key and value. https://ibb.co/dPYSuQ
EDITED
<div class='box-body no-padding'>
<table class='table cell-border' id='summary'><thead>
<tr>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
} foreach ($v as $k => $v){
echo '<th>'.$k.'</th>';
}}
?>
</tr></thead>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
foreach ($v as $k => $v){
echo '<td>'.$v.'</td>';
}
echo "</tr>";
}
?>
</table>
</div>
</div> </div>
array_walk(function($a) { unset($a[0]); }, $results)?