我在36小时内有大约2个小时的睡眠时间,所以我可能在这里有轻微的景点,但这是我之前的帖子的链接到达我现在的位置: Pivot MySQL Table with Dynamic Columns
在该帖子的帮助下,我能够使用PDO和PHP生成动态SQL查询。与之前操作数组并以表格格式显示它的尝试相比,我完全被这个难以接受。
以下是查询:
$(document).ready(function () {
toggleFields();
$("#id_type_choice").change(function () {
toggleFields();
});
});
function toggleFields() {
if ($("#id_type_choice").val() == 0)
$("#div_id_period, #div_id_product_type").hide();
else if ($("#id_type_choice").val() == 1)
$("#div_id_period").hide();
//else if ($("#id_type_choice").val() == 2)
//else if ($("#id_type_choice").val() == 3)
//else if ($("#id_type_choice").val() == 4)
//else if ($("#id_type_choice").val() == 5)
//else if ($("#id_type_choice").val() == 6)
//else if ($("#id_type_choice").val() == 7)
//else if ($("#id_type_choice").val() == 8)
//else if ($("#id_type_choice").val() == 9)
//else if ($("#id_type_choice").val() == 10)
//else if ($("#id_type_choice").val() == 11)
//else if ($("#id_type_choice").val() == 12)
//else if ($("#id_type_choice").val() == 13)
//else if ($("#id_type_choice").val() == 14)
//else
//$("#parentPermission").show();
}
这是我的阵列的vardump:
$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);
更新:这是转储在此操作之后的样子
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)
下面
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
}
var_dump($temp);
我的问题是:
这就是表格的出现方式: 同时使用嵌套循环并回显键和值。 https://ibb.co/dPYSuQ
EDITED
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)