我正在尝试从数据库中获取数据并使用它填充Highstock图表,之前我正在分配数据:'对于从搜索生成的数据表的图表选项,我意识到数据将以随机顺序绘制系列。这是我尝试过的:
function json_original()
{
$sql = "SELECT UNIX_TIMESTAMP(t1.`date`) as `date`, sum(t1.`present`) as `present` from record_attendance_cell t1 group by t1.`date`";
$present_g = $this->db->query($sql)->result_array();
foreach($present_g as $row) {
$value = $row['present'];
$datetime = $row['date'] * 1000; // convert from Unix timestamp to JavaScript time
$data[] = "[$datetime, $value]";
// echo join($data, ',');
$result = json_encode($data);
echo $result;
}
}
代码的结果包含许多相同数据的迭代: [" [1483833600000,2]"] [" [1483833600000,2]"," [1484697600000,1]"] [" [ 1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]"] [" [1483833600000,2]&#34 ;," [1484697600000,1]"," [1491174000000,1]"," [1497222000000,22]"] [" [ 1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]"," [1497222000000,22]" ," [1502319600000,11]"] [" [1483833600000,2]"," [1484697600000,1]"," [1491174000000 ,1]"," [1497222000000,22]"," [1502319600000,11]"," [1502492400000,3]"] [" [1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]"," [1497222000000, 22]"," [1502319600000,11]"," [1502492400000,3]"," [1503010800000,3]"] [ " [1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]"," [1497222000000,22 ]"," [1502319600000,11]"," [1502492400000,3]&#3 4;," [1503010800000,3]"," [1503183600000,30]"] [" [1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]"," [1497222000000,22]"," [1502319600000,11]&#34 ;," [1502492400000,3]"," [1503010800000,3]"," [1503183600000,30]"," [1503270000000 [12]"] [" [1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]" ," [1497222000000,22]"," [1502319600000,11]"," [1502492400000,3]"," [1503010800000, 3]"," [1503183600000,30]"," [1503270000000,12]"," [1503356400000,10]"] [ " [1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]"," [1497222000000,22 ]"," [1502319600000,11]"," [1502492400000,3]"," [1503010800000,3]",&# 34; [1503183600000,30]"," [1503270000000,12]"," [1503356400000,10]"," [1503961200000,9]& #34;] [" [1483833600000,2]"," [1484697600000, 1]"," [1491174000000,1]"," [1497222000000,22]"," [1502319600000,11]",& #34; [1502492400000,3]"," [1503010800000,3]"," [1503183600000,30]"," [1503270000000,12] "," [1503356400000,10]"," [1503961200000,9]"," [1506985200000,3]"] [&# 34; [1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]"," [1497222000000,22]& #34;," [1502319600000,11]"," [1502492400000,3]"," [1503010800000,3]"," [1503183600000,30]"," [1503270000000,12]"," [1503356400000,10]"," [1503961200000,9]&#34 ;," [1506985200000,3]"," [1507158000000,3]"] [" [1483833600000,2]"," [ 1484697600000,1]"," [1491174000000,1]"," [1497222000000,22]"," [1502319600000,11]" ," [1502492400000,3]"," [1503010800000,3]"," [1503183600000,30]"," [1503270000000, 12]"," [1503356400000,10]"," [150396120000 0,9,"," [1506985200000,3]"," [1507158000000,3]"," [1508367600000,31]" ] [" [1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]"," [1497222000000 ,22]"," [1502319600000,11]"," [1502492400000,3]"," [1503010800000,3]", " [1503183600000,30]"," [1503270000000,12]"," [1503356400000,10]"," [1503961200000,9 ]"," [1506985200000,3]"," [1507158000000,3]"," [1508367600000,31]",&# 34; [1509408000000,3]"] [" [1483833600000,2]"," [1484697600000,1]"," [1491174000000,1] "," [1497222000000,22]"," [1502319600000,11]"," [1502492400000,3]",&#34 ; [1503010800000,3]"," [1503183600000,30]"," [1503270000000,12]"," [1503356400000,10]&# 34;," [1503961200000,9]"," [1506985200000,3]"," [1507158000000,3]"," [ 1508367600000,31]"," [1509408000000,3]"," [15096672000 00,8]"] [" [1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]&#34 ;," [1497222000000,22]"," [1502319600000,11]"," [1502492400000,3]"," [1503010800000 ,3]"," [1503183600000,30]"," [1503270000000,12]"," [1503356400000,10]", " [1503961200000,9]"," [1506985200000,3]"," [1507158000000,3]"," [1508367600000,31 ]"," [1509408000000,3]"," [1509667200000,8]"," [1510790400000,10]"] [& #34; [1483833600000,2]"," [1484697600000,1]"," [1491174000000,1]"," [1497222000000,22] "," [1502319600000,11]"," [1502492400000,3]"," [1503010800000,3]",&#34 ; [1503183600000,30]"," [1503270000000,12]"," [1503356400000,10]"," [1503961200000,9]&# 34;," [1506985200000,3]"," [1507158000000,3]"," [1508367600000,31]"," [ 1509408000000,3]"," [1509667200000,8]"," [1510790400 000,10,"," [1511049600000,12]"] [" [1483833600000,2]"," [1484697600000,1]&#34 ;," [1491174000000,1]"," [1497222000000,22]"," [1502319600000,11]"," [1502492400000 ,3]"," [1503010800000,3]"," [1503183600000,30]"," [1503270000000,12]", " [1503356400000,10]"," [1503961200000,9]"," [1506985200000,3]"," [1507158000000,3 ]"," [1508367600000,31]"," [1509408000000,3]"," [1509667200000,8]",&# 34; [1510790400000,10]"," [1511049600000,12]"," [1511395200000,12]"] [" [1483833600000,2] "," [1484697600000,1]"," [1491174000000,1]"," [1497222000000,22]",&#34 ; [1502319600000,11]"," [1502492400000,3]"," [1503010800000,3]"," [1503183600000,30]&# 34;," [1503270000000,12]"," [1503356400000,10]"," [1503961200000,9]"," [ 1506985200000,3]"," [1507158000000,3]"," [150836 7600000,31]"," [1509408000000,3]"," [1509667200000,8]"," [1510790400000,10]" ," [1511049600000,12]"," [1511395200000,12]"," [1514764800000,2]"]
数据应如下所示: Format
参考文件: High Charts
答案 0 :(得分:1)
请记住 json_encode
必须使用一次:
foreach($present_g as $row) {
$value = $row['present'];
$datetime = $row['date'] * 1000; // convert from Unix timestamp to JavaScript time
// Also remove quotes here, you don't need a string
$data[] = [$datetime, $value];
}
$result = json_encode($data);
echo $result;