0
The following code is returning an array as a string and therefore I cannot distribute the data to popular Flot Chart.
The
PHP code:
<?php
include_once("config.php");
$viagemid = $_POST['id'];
$results_array = array();
$sql = "SELECT adiantamento AS 'Antecipado' FROM viagem WHERE viagemid = $viagemid";
$query = $mysqli->query($sql);
while($row =mysqli_fetch_assoc($query)){
array_push($results_array, $row);
}
$sql = "SELECT SUM(vl_desp) as 'Despesas' FROM despesas WHERE viagemid = $viagemid";
$query = $mysqli->query($sql);
while($row =mysqli_fetch_assoc($query)){
array_push($results_array, $row);
}
$sql = "SELECT SUM(venda.vl_venda * venda.qtd) AS 'Venda SP' FROM venda WHERE venda.viagemid = $viagemid AND venda.ufid = 1";
$query = $mysqli->query($sql);
while($row =mysqli_fetch_assoc($query)){
array_push($results_array, $row);
}
$sql = "SELECT SUM(venda.vl_venda * venda.qtd) AS 'Venda local' FROM venda WHERE venda.viagemid = $viagemid AND venda.ufid = 2";
$query = $mysqli->query($sql);
while($row =mysqli_fetch_assoc($query)){
array_push($results_array, $row);
}
$string = "[[0,".$results_array[0]['Antecipado']."],[1,".$results_array[1]['Despesas']."],[2,".$results_array[2]['Venda SP']."],[3,".$results_array[3]['Venda local']."]]";
var_dump(json_decode(serialize($results_array), true));
?>
THE JS:
$(function() {
$.post('chart_data.php', {id:<?php echo $viagemid; ?>}, function(data) {
alert(data);
var d1 = [[0,data],[1,data],[2,data],[3,data]];
alert(d1);
var barOptions = {
series: {
bars: {
show: true,
barWidth: 0.6,
fill: true,
align:'center',
fillColor: {
colors: [{
opacity: 0.8
}, {
opacity: 0.8
}]
}
}
},
xaxis: {
tickDecimals: 0,
ticks:[[0,'Adiantamento'],[1,'Despesas'],[2,'Venda SP'],[3,'Venda local']]
},
colors: ["#ffbb00"],
grid: {
color: "#999999",
hoverable: true,
clickable: true,
tickColor: "#D4D4D4",
borderWidth:0
},
legend: {
show: false
},
tooltip: true,
tooltipOpts: {
content: "%x: %y"
}
};
var barData = {
label: "bar",
data: d1
};
$.plot($("#flot-bar-chart"), [barData], barOptions);
});
//return false;
});
What is the return of
JSON
?– Marcelo de Andrade
Are you using the
var_dump
to send to Javascript? Strip and placeecho
.– Sergio
@Marcelodeandrade after using var_dump(json_decode( it from the error (json_decode expects Parameter 1 to be string), so I used serialize() to convert to string and then returned NULL and with echo it returns "[{"Anticipated":"30000"},{"Expenses":"18256.61"},{"Sale SP":"13080"},{"Local sale":"18021.5"}]"
– Felipe Chagas
Why are you Serializing? No need, it worked normally giving
json_decode
in this return, see in ideone– Marcelo de Andrade