[英]PHP JSON_encode output not as expected
Hello I have a PHP script that pulls info from a database table. 您好,我有一个PHP脚本,可从数据库表中提取信息。
The table structure is : 表结构为:
Delaytype(varchar)| Delayhours(int)
The PHP script groups by delay type. PHP脚本按延迟类型分组。
<!doctype html>
<?php
?>
<head>
<script src="chartjs/Chart.js-master/Chart.js"></script>
<script src="chartjs/Chart.js-master/Chart.min.js"></script>
<script src="chartjs/Chart.js-master/package.json"></script>
</head>
<body>
<?php
include "config.php";
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to DataBase: " . mysqli_connect_error();
}
else
{
$result = mysqli_query($con, "SELECT Delaytype, COUNT(1) as cnt FROM
delays GROUP BY Delaytype; ");
echo "<table id='mytable'><th>Delay Type</th><th>Delay Hours</th>";
$pieData = array();
while($row = mysqli_fetch_array($result))
{
$pieData[] = array('value' =>$row['cnt'], 'color' =>'#878BB6 ');
}
?>
<script>
var pieData = <?php echo" ".json_encode($pieData).""; ?>;//problem
// pie chart options
var pieOptions = {
segmentShowStroke : false,
animateScale : true
};
// get pie chart canvas
var countries= document.getElementById("buy").getContext("2d");
// draw pie chart
new Chart(countries).Pie(pieData, pieOptions);
</script>
</body>
</html>
<?php
mysqli_close($con);
}
?>
I am trying to output the JSON in the following format - The example below works with just the entered values. 我正在尝试以以下格式输出JSON-以下示例仅适用于输入的值。 Its Javascript to creat a pie chart with chart js. 用图表js创建饼图的Javascript。
It expects the JSON like 它期望像
[{value:?,color:"?"},{value:?,color:"?"}]
When I echo my JSON I get 当我回显JSON时,我得到
[{"value":"?",color:"?"}],[{"value":?,color:"?"},{"value":?,color:"?"}]
The first set is a duplicate of the second and then it works?. 第一组是第二组的副本,然后起作用? Is there a better way of doing this? 有更好的方法吗? With the above code I get part of a chart. 通过上面的代码,我得到了图表的一部分。 The other issue is I have to get a different color for each row?currently it is the same color as I do not know how I can get different colors in as it is not part of my database structure? 另一个问题是我必须为每行获取不同的颜色吗?当前它是同一颜色,因为它不属于数据库结构的一部分,所以我不知道如何获得不同的颜色?
The below example works to create the chart not using the database. 下面的示例不使用数据库来创建图表。 So ideally I would like some advice on 1) how to get the JSON out of my database to fit in the below format. 因此,理想情况下,我希望对1)提供一些建议,即如何从数据库中获取JSON以适合以下格式。 2) Is there a way without storing colors in a database table where I can apply different color values to the below 2)有没有一种方法可以不将颜色存储在数据库表中,我可以在其中将不同的颜色值应用于以下内容
var pieData = [
{
value: 20 , // ideal :I want database data
color:"#878BB6"//color that I have chosen
},
{
value : 40,
color : "#4ACAB4"
},
{
value : 10,
color : "#FF8153"
},
{
value : 30,
color : "#FFEA88"
}
];
when I do VAR_DUMP i GET THE FOLLOWING : 当我做VAR_DUMP时,得到以下提示:
array(2) {[0]=>array(2) {["value"]=> string(2) "21"["color"]=>string
(8) "#878BB6"}[1]=>array(2){["value"]=>string(2)"99"["color"]=>string
(8)"#878BB6"}}
Just look at your code, Spagetti of HTML
, Javascript
& PHP
. 只需看看您的代码, HTML
, Javascript
和PHP
Spagetti。
You should not mix them that way, intead you should make a request using AJAX for example: 您不应该这样混合使用它们,例如,您应该使用AJAX进行请求:
PHP
) query your database. 让服务器端( PHP
)查询您的数据库。 javascript
). 在客户端( javascript
)中解析响应。 Here is an example: 这是一个例子:
PHP PHP
<?php
include "config.php";
if (mysqli_connect_errno($con)){
echo "Failed to connect to DataBase: " . mysqli_connect_error();
}
else
{
$result = mysqli_query($con, "SELECT Delaytype, COUNT(1) as cnt
FROM delays GROUP BY Delaytype;");
$pieData = array();
while($row = mysqli_fetch_array($result)){
$pieData[] = array('value' =>$row['cnt'], 'color' =>'#878BB6 ');
}
mysqli_close($con);
}
echo json_encode($pieData);
?>
Javscript Javscript
window.onload = function() {
var hr = new XMLHttpRequest();
hr.open("GET", "get_pie_data.php", true);
hr.setRequestHeader("Content-type", "application/json");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var pieData = JSON.parse(hr.responseText);
// pie chart options
var pieOptions = {
segmentShowStroke : false,
animateScale : true
};
// get pie chart canvas
var countries= document.getElementById("buy").getContext("2d");
// draw pie chart
new Chart(countries).Pie(pieData, pieOptions);
}
}
hr.send(null);
}
If your comfortable with jQuery
then look into ajax, it's a little easier to work with. 如果您对jQuery
感到满意,那么可以考虑使用ajax,使用起来会容易一些。
Hello got chart working final code below. 您好,下面是图表的最终代码。 It seems it was not a JSON issue after all. 看来这毕竟不是JSON问题。 JSON was fine but the quotes around my numbers in the chart data broke the chart. JSON很好,但是图表数据中我的数字周围的引号打破了图表。 I added the (int) as follows. 我添加了(int)如下。
Delay TypeDelay Hours"; $pieData = array(); while($row = mysqli_fetch_array($result)) { $pieData[] = array('value' =>(int)$row['cnt'], 'color' =>'#878BB6 '); } ?> Delay TypeDelay Hours“; $ pieData = array(); while($ row = mysqli_fetch_array($ result)){$ pieData [] = array('value'=>(int)$ row ['cnt'],'color' =>'#878BB6');}?> <script> var pieData = <?php echo json_encode($pieData) ?>;// // pie chart options var pieOptions = { segmentShowStroke : false, animateScale : true }; // get pie chart canvas var countries= document.getElementById("buy").getContext("2d"); // draw pie chart new Chart(countries).Pie(pieData, pieOptions); </script> </body> </html> <?php mysqli_close($con); } ?>
I did also remove quotes (spaces) from echo of json encode. 我的确也从json编码的回声中删除了引号(空格)。 I did this before I added (int). 在添加(int)之前,我已经这样做了。 It did not fix the chart. 它没有修复图表。 However I have not tested if the would have broken the chart with the (int) in place. 但是,我还没有测试,如果使用(int)可以打破图表。
I still have a problem of trying to get different colors in the chart. 我仍然有尝试在图表中获得不同颜色的问题。
Thanks for input. 感谢您的输入。 The question was a little off the real problem. 这个问题与实际问题有些偏离。 Apologies for that. 对此表示歉意。 I didnt know this when posting 发布时我不知道这一点
Sorry just saw someone wrote this when posting my awnser - thankyou. 抱歉,刚看到有人在发布我的遮阳篷时写了这个-谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.