So here is my problem:
I have this chunck of jquery which pretty much commands data for a donut chart
var data = [
{ label: "Country1", data: 12},
{ label: "Country2", data: 27},
{ label: "Country3", data: 85},
{ label: "Country4", data: 64},
{ label: "Country5", data: 90},
{ label: "Country6", data: 112}
];
However, I need this data not to be hardcoded, but to be dynamically loaded from my databse using MySQL/PhP and usual queries:
$st = $this->db->dbh->query("SELECT COUNT(USER_COUNTRY) AS value, USER_COUNTRY FROM USER GROUP BY USER_COUNTRY");
I would need some kind of loop to irate through the result, assign each country to a label:, a value to the data ...
I have however absolutely no clue about how to do this. I have a feeling I might need ajax, but I don't know anything about it yet. Couldn't find any relevant help on the web.
You need to exchange the data between the client and server using some kind of serialization format, most likely JSON. This doesn't directly entail ajax unless you'd like it to load asynchronously in the frontend.
Look into using php's json_encode
to output the json data into your template, where you can select the element that contains the serial using Jquery. Alternatively, you could just have a url endpoint that echoes out the json and use Jquery's .getJSON
method as a shortcut using ajax.