bambinou1980 Posted September 2, 2015 Share Posted September 2, 2015 Hello, I currently have this code: <?php $query = "SELECT SUM(cust_order_total) AS label, due_date AS y FROM orders WHERE YEAR(due_date) = YEAR(CURDATE()) AND MONTH(due_date) = MONTH(CURDATE()) GROUP BY due_date ORDER BY label"; $rows = []; $result = mysqli_query($connection,$query); $rows = mysqli_fetch_all($result, MYSQLI_ASSOC); ?> The output in json format is: [{"label":"95.00","y":"2015-09-01"},{"label":"112.50","y":"2015-09-10"},{"label":"150.00","y":"2015-09-23"}] I am trying to get this data into a line chart from the Google chart API but I just cannot get it to work. There is an example in jsfiddle here: [https://jsfiddle.net/api/post/library/pure/][1] Here is what I tried(Which gives me a blank graph): <script type="text/javascript"> google.load('visualization', '1', {packages: ['corechart', 'line']}); google.setOnLoadCallback(drawBasic); function drawBasic() { var data = new google.visualization.DataTable(); data.addColumn('number', 'Sales'); data.addColumn('date', 'Time'); data.addRows(<?php echo json_encode($rows); ?>); var options = { hAxis: { title: 'Time' }, vAxis: { title: 'Popularity' } }; var chart = new google.visualization.LineChart(document.getElementById('chart_div')); chart.draw(data, options); } </script> <div id="chart_div"></div> I believe the problem comes from the way my json_encode formats the output, but how to change this to get it to work with the Google Chart API please? Thank you, Quote Link to comment Share on other sites More sharing options...
Barand Posted September 2, 2015 Share Posted September 2, 2015 I gave you the code for using your query with Google charts two days ago http://forums.phpfreaks.com/topic/297975-php-encode-to-json-help-with-morrisjs-please/?do=findComment&comment=1520002 Quote Link to comment Share on other sites More sharing options...
scootstah Posted September 2, 2015 Share Posted September 2, 2015 data.addRows(<?php echo json_encode($rows); ?>); Don't do that. That is an XSS exploit waiting to happen. See here: http://forums.phpfreaks.com/topic/294115-json-encode-is-not-a-security-feature-or-how-to-pass-php-values-to-javascript/ Quote Link to comment Share on other sites More sharing options...
bambinou1980 Posted September 3, 2015 Author Share Posted September 3, 2015 I gave you the code for using your query with Google charts two days ago http://forums.phpfreaks.com/topic/297975-php-encode-to-json-help-with-morrisjs-please/?do=findComment&comment=1520002 Thank you, yes you are right and I forgot about it.... I had to come back to google charts as I could not parse the output with the other libraries...I did not know it was so complicated to get those charts working properly.. Quote Link to comment Share on other sites More sharing options...
bambinou1980 Posted September 3, 2015 Author Share Posted September 3, 2015 Don't do that. That is an XSS exploit waiting to happen. See here: http://forums.phpfreaks.com/topic/294115-json-encode-is-not-a-security-feature-or-how-to-pass-php-values-to-javascript/ Interesting post but I still do not understand why would a hacker cross site script a graph data, what would be the point if the data is useless to use on another website? Quote Link to comment Share on other sites More sharing options...
scootstah Posted September 3, 2015 Share Posted September 3, 2015 To spread malware or gather user information. Quote Link to comment Share on other sites More sharing options...
bambinou1980 Posted September 7, 2015 Author Share Posted September 7, 2015 Thank you, got it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.