Reputation: 13
I'm making a timeline graph and I have problem with the data I need the result of the query to the database did not result in double quotes ("). They leave a sample of how I get the result
[{"name":"Tv Cable","data":["[1370925000000,100]"]},{"name":"Tv Satelital","data":["[1365654600000,100]","[1368505800000,200]","[1370320200000,1500]","[1370925000000,500]","[1370925000000,560]","[1370925000000,50]","[1370925000000,500]","[1370925000000,800]","[1370925000000,500]","[1373776200000,1000]"]},{"name":"Tv Internet","data":["[1371097800000,500]"]},{"name":"Tv Telefonia"}]
I need it follows
[{"name":"Tv Cable","data":[[1370925000000,100]]},{"name":"Tv Satelital","data":[[1365654600000,100],[1368505800000,200],[1370320200000,1500],[1370925000000,500],[1370925000000,560],[1370925000000,50],[1370925000000,500],[1370925000000,800],[1370925000000,500],[1373776200000,1000]]},{"name":"Tv Internet","data":[[1371097800000,500]]},{"name":"Tv Telefonia"}]
I have while trying to fix it and I have not managed to do?
I let my graphic codes to see what I do
sql.php
<?php
$fecha = date("d-m-Y"); // fecha actual
$ano = date("Y"); // A単o actual
$mes = date("m"); // Mes actual
$dia = date("d"); // Dia actual
$mes_inicio= $mes-2;
$con = mysql_connect("localhost","xyolcarx_inter","xYolcar19572059");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xyolcarx_inter", $con);
$rows = array();
for ($i=1;$i<=4;$i++){
$sth = mysql_query("SELECT monto,(unix_timestamp(fecha)*1000) FROM ventas WHERE codigo_ser = '".$i."' ORDER BY fecha ASC ");
$sth2 = mysql_query("SELECT * FROM servicio WHERE codigo_ser= '".$i."'");
while($r2 = mysql_fetch_array($sth2)) {
$rows[$i]['name'] = $r2['servicio'];
}
while($r = mysql_fetch_array($sth)) {
$rows[$i]['data'][] = '['.$r['(unix_timestamp(fecha)*1000)'].','.$r['monto'].']';
}
}
$result = array();
for ($i=1;$i<=4;$i++){
array_push($result,$rows[$i]);
}
print json_encode($result, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type='text/css'>
</style>
<script type='text/javascript'>//<![CDATA[
$(function() {
$.getJSON('sql.php', function(data) {
// Create the chart
window.chart = new Highcharts.StockChart({
chart : {
renderTo : 'container'
},
rangeSelector : {
selected : 1
},
title : {
text : 'AAPL Stock Price'
},
series : data
});
});
});
//]]>
</script>
</head>
<body>
<script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
<div id="container" style="height: 500px; min-width: 500px"></div>
</body>
</html>
Upvotes: 1
Views: 386
Reputation: 27247
This line
$rows[$i]['data'][] = '['.$r['(unix_timestamp(fecha)*1000)'].','.$r['monto'].']';
should look like this
$rows[$i]['data'][] = array($r['(unix_timestamp(fecha)*1000)'],$r['monto']);
Don't build the json yourself. Just build the array structure, and let json_encode do the rest of the work for you.
edit more advice:
Change this:
SELECT monto,(unix_timestamp(fecha)*1000) FROM ventas...
to this:
SELECT monto, (unix_timestamp(fecha)*1000) AS fecha_timestamp FROM ventas...
so you can access it easier in php:
$rows[$i]['data'][] = array($r['fecha_timestamp'],$r['monto']);
edit more advice:
Mysql sometimes has performance issues with unix_timestamp(fecha)*1000
because it has to turn what it thinks was an 32-bit int into a 64-bit int, and that sucks. Do that in php.
Change this:
SELECT monto, (unix_timestamp(fecha)*1000) AS fecha_timestamp FROM ventas...
to this:
SELECT monto, unix_timestamp(fecha) AS fecha_timestamp FROM ventas...
so you can do the work in php:
$rows[$i]['data'][] = array($r['fecha_timestamp']*1000,$r['monto']);
Upvotes: 1