score:2
Accepted answer
select
company,
group_concat(value separator ',') as value,
group_concat(monthyear separator ',') as monthyear
from
yourtable
group by
company
some reference for group_concat
.
php solution:
select the to be grouped attribute sorted (company). loop over them and open a new group every time you encounter a different value for company. as long as the current row has the same row as the previous, add value
and monthyear
to the current company.
you could do this even without sorting:
while($row = mysql_fetch_assoc($resource))
{
$values[$row["country"]][] = $row["value"];
$monthyear[$row["country"]][] = $row["monthyear"];
}
some output example
foreach ($values as $country => $valuesonecountry)
{
// each country
var_dump($country);
foreach ($valuesonecountry as $i => $value)
{
// value, monthyear for each original row
var_dump($value, $monthyear[$country][$i]);
}
}
elegant way with oop:
class tuple
{
public $country, $values, $monthyears;
public function __construct($country, $values = array(), $monthyears = array())
{
$this->country = $country;
$this->values = $value;
$this->monthyears = $monthyears;
}
}
$tuples = array();
while($row = mysql_fetch_assoc($resource))
{
if (!isset($tuples[$row["country"]]))
$tuples[$row["country"]] = new tuple($row["country"]);
// save reference for easy access
$tuple = $tuples[$row["country"]];
// or some method like $tuple->addvalue($row["value"]);
$tuple->values[] = $row["value"];
$tuple->monthyears[] = $row["monthyear"];
}
var_dump($tuples);
Source: stackoverflow.com
Related Query
- A Challenge? converting mysql rows to very specific format with php
- Problems with retrieving mysql data into javascript in a specific format
- Php and MySQL with Highchart
- How to loop the data[] series with php and mysql
- Generating Highcharts from MySQL data with PHP does not work
- Searching large (6 million) rows MySQL with stored queries?
- HighCharts - making a Pie with MySQL and PHP
- Select format mysql to highchart in php
- highchart with a specific json format in django
- Highstock with php and mysql
- mySQL with PHP to JSON for clientside chart
- Only my title shows when using highcharts with php and MySql
- Implementing High charts with PHP and Mysql
- Highcharts code with same set of code/data works in PHP but does not work in JSfiddle
- Highcharts: Format all numbers with comma?
- Highcharts data series issue with ajax/json and PHP
- HighCharts - number format with $ in its short form
- Add dynamic data to line chart from mysql database with highcharts
- Highcharts label format with tickPositioner in a datetime x Axis
- last label in x axis with datetime format not coming in highchart
- How can I configure the legend with a specific height in highstock?
- Highcharts - Global configuration with common code and unique data & Headings
- How to populate a Highcharts axis with string formatted data from a PHP array
- Highcharts export charts as image on serverside with php
- Highcharts / JavaScript month is incompatible with MySQL
- Highcharts Mysql Json arrays PHP
- Highcharts Mysql Json arrays PHP
- Highcharts Column-chart with php and Ajax
- Highcharts - How to populate date with Mysql and PHP?
- How can i get the json data with the specific between Year?
More Query from same tag
- Toggle Show/Hide on Highcharts custom export button
- Highcharts distorsion
- max Y Axis value in a Highchart
- Issue on Setting Up Highchats.js Index of Chart
- Need an image/shape at the position of mouse
- Putting select element to div with z-index position to Highcharts container
- Highcharts tooltip x axis font size
- Highcharts Time Data With Irregular Intervals In Wrong Order
- highcharts multiline text inside donut piechart
- Highchart live server example uses a lot cpu
- Represent extreme values on Doughnut chart in SSRS
- Is there anyway to center the Column Chart on the xAxis
- Highcharts: How to toggle between different xaxis having individual plotbands, onclick rangeselector buttons
- Dotnet.Highcharts error in export image
- highcharts - render text with decimal 'x' and 'y'
- Group SQL data by JSON arrays
- Resize Highcharts StockChart range selector to lessthan 6 data points
- Highchart Pie chat not display Codeigniter
- How to create a range between series?
- Highcharts component that creates secondary yAxis based on props.boolean
- Highchart: show seconday y-axis tick positions 0 to 12
- Highcharts custom error handler
- Highstock multiple series irregular time
- Highcharts 3.0 version changes in exporting options?
- Not able to have highcharts x-axis labels max
- Highcharts DateTime xAxis and tooltip formatting issue
- How to export a Highchart chart to PDF thanks to a button outside the chart?
- HighCharts. How to display X axis labels line without data?
- Highchart combination chart with stacked column
- Highcharts exporting once again