We help IT Professionals succeed at work.
Get Started

Create javascript array from php sql query for use with charts.js

Steve Tinsley
Last Modified: 2016-02-09
I am using http://www.chartjs.org/ to create some bar charts. The data is coming from a php / sql query.

I need to get the php data into the js arrays but im not sure how.

This is as far as I have got:

<script type="text/javascript">
<?php foreach ($rows as $row) {
	extract($row); ?>
	console.log('<?= $questionOption; ?>');
	console.log('<?= $questionResponseCount; ?>');
<?php } ?>

$( document ).ready(function() {

	var barData = {
		labels : ["January","February","March","April","May","June"],
		datasets : [
				data : [203,156,99,251,305,247]

    var canvas = document.getElementById('canvas').getContext('2d');
    new Chart(canvas).Bar(barData);

Open in new window

I hope you can help.

Watch Question
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE