Try this like your PHP:
$query = mysql_query(" SELECT 'Like' as 'preference', SUM(IF(Apple = 'Like', 1, 0)) as Apple, SUM(IF(Orange = 'Like', 1, 0)) as Orange, SUM(IF(Strawberry = 'Like', 1, 0)) as Strawberry FROM data UNION SELECT 'Dislike' as 'preference', SUM(IF(Apple = 'Dislike', 1, 0)) as Apple, SUM(IF(Orange = 'Dislike', 1, 0)) as Orange, SUM(IF(Strawberry = 'Dislike', 1, 0)) as Strawberry FROM data "); $table = array(); $table['cols'] = array( array('label' => 'preference', 'type' => 'string'), array('label' => 'Apple', 'type' => 'number'), array('label' => 'Orange', 'type' => 'number'), array('label' => 'Strawberry', 'type' => 'number') ); $rows = array(); while($r = mysql_fetch_assoc($query)) { $temp = array(); $temp[] = array('v' => $r['preference']); $temp[] = array('v' => (int) $r['Apple']); $temp[] = array('v' => (int) $r['Orange']); $temp[] = array('v' => (int) $r['Strawberry']); $rows[] = array('c' => $temp); } $table['rows'] = $rows; $jsonTable = json_encode($table); echo $jsonTable;
SQL should return two rows of data, one sum of likes and another sum of dislikes, which is then parsed in the DataTable format of the Google visualization API and displayed as a JSON string. This is useful for use as an AJAX data source for a chart, but with a slight modification, it would be convenient to directly output the data in javascript to draw a chart.
source share