Here's some PHP you can try out:

$sth = mysql_query("SELECT rating, country, COUNT(*) AS RatingPerCountry 
FROM userdata GROUP BY rating, country");
$rows = array();
$flag = true;

$table = array();
$table['cols'] = array(
    array('label' => 'rating', 'type' => 'string')
);
$tempData = array();
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
// add a new column for this country
$table['cols'][] = array('label' => $r['country'], 'type' => 'number');
 if (is_null($tempData[$r['rating']])) {
$tempData[$r['rating']] = array();
}
 $tempData[$r['rating']][$r['country']] = $r['RatingPerCountry']; 
}
foreach ($tempData as $rating => $row) {
    $temp = array();
 foreach (array('v' => $i['datetime']) as $column) {
if ($column['label'] == 'rating') { // make sure this matches the label 
used for the rating column
$temp[] = array('v' => $rating);
}
else {
$temp[] = array('v' => (int) $row[$column['label']]);
}
$rows[] = array('c' => $temp);
}
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);

Note that this code expects that all countries have an entry for each 
rating (even if it is 0).  That likely won't be a problem, but it's worth 
noting just in case.

On Saturday, March 23, 2013 8:03:05 AM UTC-4, Zarko wrote:
>
> Hi guys,
>
> I'm trying to implement google charts in one of my projects, but just 
> can't get my head around how to construct a column array in PHP which will 
> comply with Json formatting. In summary, this is what I have:
>
> 1) MySQL table with two columns "Country", "Rating", both of which hold 
> string values (example: Germany can have a rating of "Good" or "Bad", 
> depending on entries by users and their view on Germany).
>
> 2) In order to construct a numeric value, i.e. how many times has Germany 
> been rated as "Good" for example, I have a following SQL query in my php 
> file, which works as expected:
>
>     $query = "SELECT rating, country, COUNT(*) AS RatingPerCountry FROM 
> userdata GROUP BY rating, country";
>
> 3) A problem that I'm facing is how to code a dynamic array of countries 
> as columns in Google Charts, which will hold numeric values, because all 
> the PHP examples I've seen only have a static numeric colum, which can be 
> labeled as "Country" for example. What I really need  instead of static 
> label "Country", is to dynamically import countries from my database as 
> columns. Does that make sense? 
>
> I've constructed an example in Google Playground of how I'd expect my 
> chart to look like. Thanks in advance for any help I can get:
>
>
>
> <https://lh3.googleusercontent.com/-yt_hdYoUftk/UU2ZsZVsBMI/AAAAAAAAABk/QqNCOYk3uN0/s1600/Screen+shot+2013-03-23+at+11.30.01.png>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at 
http://groups.google.com/group/google-visualization-api?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to