使用 json 和 php 浮动
Flot with json and php
我用 php 创建了一个 json 数组。它有效,但它只给出第一个标签的 'data' 结果,而不是其余的。除此之外,它每次都以数字开头。我该如何解决这个问题?
$sql = $site->mysqli->query("SELECT id, title, event_start_date FROM events");
while($row = $sql->fetch_assoc()) {
$dataset[] = array('label' => $row['title']);
$sql2 = $site->mysqli->query("SELECT timestamp, event_id, COUNT(timestamp) AS row_count FROM order_products WHERE event_id = '".$row['id']."' GROUP BY DATE(FROM_UNIXTIME(timestamp))");
$count2 = $sql2->num_rows;
while($row2 = $sql2->fetch_assoc()) {
$dataset['data'][] = array(intval($row2['timestamp'] * 1000), (int)$row2['row_count']);
}
}
$results[]=$dataset;
die(Json_encode($results));
结果:
[{"0":{"label":"Label 1"},"data":[[1392894348000,10],
[1392998502000,3],[1393066962000,5],[1393262541000,3],
[1393577348000,5],[1393704543000,2],[1393780878000,1],
[1393882353000,3],"1":{"label":"Label 2"},"2":{"label":"Label 3"}}]
您应该清空每个事件的 $dataset
:
$sql = $site->mysqli->query("SELECT id, title, event_start_date FROM events");
while($row = $sql->fetch_assoc()) {
$dataset = array('label' => $row['title'], 'data' => array());
$sql2 = $site->mysqli->query("SELECT timestamp, event_id, COUNT(timestamp) AS row_count FROM order_products WHERE event_id = '".$row['id']."' GROUP BY DATE(FROM_UNIXTIME(timestamp))");
$count2 = $sql2->num_rows;
while($row2 = $sql2->fetch_assoc()) {
$dataset['data'][] = array(intval($row2['timestamp'] * 1000), (int)$row2['row_count']);
}
$results[]=$dataset;
}
die(json_encode($results));
我用 php 创建了一个 json 数组。它有效,但它只给出第一个标签的 'data' 结果,而不是其余的。除此之外,它每次都以数字开头。我该如何解决这个问题?
$sql = $site->mysqli->query("SELECT id, title, event_start_date FROM events");
while($row = $sql->fetch_assoc()) {
$dataset[] = array('label' => $row['title']);
$sql2 = $site->mysqli->query("SELECT timestamp, event_id, COUNT(timestamp) AS row_count FROM order_products WHERE event_id = '".$row['id']."' GROUP BY DATE(FROM_UNIXTIME(timestamp))");
$count2 = $sql2->num_rows;
while($row2 = $sql2->fetch_assoc()) {
$dataset['data'][] = array(intval($row2['timestamp'] * 1000), (int)$row2['row_count']);
}
}
$results[]=$dataset;
die(Json_encode($results));
结果:
[{"0":{"label":"Label 1"},"data":[[1392894348000,10],
[1392998502000,3],[1393066962000,5],[1393262541000,3],
[1393577348000,5],[1393704543000,2],[1393780878000,1],
[1393882353000,3],"1":{"label":"Label 2"},"2":{"label":"Label 3"}}]
您应该清空每个事件的 $dataset
:
$sql = $site->mysqli->query("SELECT id, title, event_start_date FROM events");
while($row = $sql->fetch_assoc()) {
$dataset = array('label' => $row['title'], 'data' => array());
$sql2 = $site->mysqli->query("SELECT timestamp, event_id, COUNT(timestamp) AS row_count FROM order_products WHERE event_id = '".$row['id']."' GROUP BY DATE(FROM_UNIXTIME(timestamp))");
$count2 = $sql2->num_rows;
while($row2 = $sql2->fetch_assoc()) {
$dataset['data'][] = array(intval($row2['timestamp'] * 1000), (int)$row2['row_count']);
}
$results[]=$dataset;
}
die(json_encode($results));