【发布时间】:2016-10-10 10:11:33
【问题描述】:
我有一个 PHP PDO 查询,它从数据库中选择年、月和日。它还计算每天的行数:
SELECT
dateYear, dateMonth, dateDay,
count(dateDay) AS count_days
FROM just_ink
WHERE dateMonth = :month AND dateYear = :year AND deleted = 0
GROUP BY dateYear, dateMonth, dateDay
回显结果时:
foreach ($result as $subResult) {
foreach ($subResult as $row) {
$year = $row['dateYear'];
$month = $row['dateMonth'];
$day = $row['dateDay'];
$count = $row['count_days'];
echo $month . " " . $day . " " . $year . " " . $count . " lines, ";
}
}
这会返回一个类似的值:
June 7 2016 3 lines, June 8 2016 1 lines,
这意味着,6 月 7 日有 3 个表格,6 月 8 日有 1 个表格。
现在进行格式化,我使用Highcharts 基本折线图。数据需要这样格式化:
<script>
$(function () {
$('#container').highcharts({
title: {
text: 'Monthly New Forms',
x: -20 //center
},
xAxis: {
title: {
text: 'Day of the Month'
},
categories: [
'1', '2', '3', '4', '5',
'6', '7', '8', '9', '10',
'11', '12', '13', '14', '15',
'16', '17', '18', '19', '20',
'21', '22', '23', '24', '25',
'26', '27', '28', '29', '30',
'31'
]
},
yAxis: {
title: {
text: 'Month of June'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: 'Number of New Forms Per Day',
data: [1, 2, 3, 4, 5, 6]
}]
});
});
</script>
其中类别是 x 轴标题,系列数据是每天的频率。所以如果我想代表
June 7 2016 3 lines, June 8 2016 1 lines,
系列数据需要看起来像
0, 0, 0, 0, 0, 0, 3, 1, 0, etc.
有什么想法吗?
【问题讨论】:
-
按照 highcharts 想要的方式构建一个数组,并使用
json_encode()将其发送到 javascript。 -
@jeroen 你如何“构建”一个数组?
-
正如@jeroen 所说,创建一个 php 数组(或特别是关联数组)并将其传递给 echo json_encode。更多信息在这里:php.net/manual/en/function.json-encode.php
标签: javascript php mysql pdo highcharts