php - Multidimensional array json_encoded -
i try return multidimensional array of dates , revenues each date json array in order display chart in panel dashboard. unfortunately chart makes problem, when don't hardcode json array, debugging right , wonder why json_encoded array looks strange.
this panel, draws chart , requests data data.php:
jquery.getjson("data.php", { request: "revenues" }, function (result) { var revenues = result["amount"]; alert(revenues); $(".monthly-sales").sparkline(revenues, { type: 'bar', barcolor: '#ff4e50', height: '55px', width: '100%', barwidth: 8, barspacing: 1 }); });
the data.php:
elseif($request == "revenues"){ // output revenues per day nexus store $revenues = array(); $revenues["dates"] = array(); $revenues["amount"] = array(); $sql = "select i_total revenue, date(from_unixtime(i_date)) date nexus_invoices i_status='paid' group date(from_unixtime(i_date)) order i_date desc limit 0,10"; $rows = $db->query($sql); foreach($rows $row){ $revenues["dates"][] = $row['date']; $revenues["amount"][] = floatval($row['revenue']); } //$revenues["amount"] = [1,5,5.5,5.4,5.8,6,8,9,13,12,10,11.5,9,8,5,8,9]; echo json_encode($revenues); }
the json result of code how expect it:
{ "dates":[ "2015-06-13","2015-06-12","2015-06-10","2015-06-09","2015-06-07", "2015-06-06","2015-06-05","2015-06-04","2015-06-02","2015-05-31" ], "amount":[8,22,8,8,22,8,8,8,8,8] }
unfortunately doesn't work unknown reason. debug further have uncommented line hardcodes $revenues["amount"]
, commented second line of foreach. worked fine. result of json_encoded echo this:
{ "dates":[ "2015-06-13","2015-06-12","2015-06-10","2015-06-09","2015-06-07", "2015-06-06","2015-06-05","2015-06-04","2015-06-02","2015-05-31" ], "amount":[1,5,5.5,5.4,5.8,6,8,9,13,12,10,11.5,9,8,5,8,9] }
how looks when hardcode array:
how looks when fill array database:
i think problem not in json array rather sparkline plugin.
try adding when display db data: chartrangemin:5
$(".monthly-sales").sparkline(revenues, { type: 'bar', barcolor: '#ff4e50', height: '55px', width: '100%', barwidth: 8, barspacing: 1, chartrangemin:5 });
Comments
Post a Comment