Zan
Zan

Reputation: 43

Highcharts summing results that have same date

I have installed Highcharts on my web site, but I have a problem. MySQL returns data in that value:

['26.03.2013', 1], ['26.03.2013', 1], ['26.03.2013', 1], ['26.03.2013', 1], ['26.03.2013', 1], ['11.03.2013', 1], ['21.03.2013', 1], ['22.03.2013', 1]

And it is included in Highcharts like that:

$('#chart').highcharts({
    chart: {
        type: 'line'
    },
    title: {
        text: 'Statistika prenosov'
    },
    xAxis: {
        title: {
            text:'Datum'
        }
    },
    yAxis:{
        title: {
            text:'Prenosi'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },


   series: [{
            name: 'Prenosi',
            data: [['26.03.2013', 1], ['26.03.2013', 1], ['26.03.2013', 1], ['26.03.2013', 1], ['26.03.2013', 1], ['11.03.2013', 1], ['21.03.2013', 1], ['22.03.2013', 1]]
        }]
    });

});

But I want, if there are three dates 26.3.2013, then don't show them for each alone, but put them together, so result should be 3, not 3x1.

Here is, how I fetch results with PHP:

$query=mysql_query("SELECT * FROM downloads WHERE prjID='".$_GET['id']."' ORDER BY date ASC");
$num=mysql_num_rows($query);
$res='';
$i=0;
while($row=mysql_fetch_array($query)){

$i++;
$date=date("d.m.Y", strtotime($row['date']));

$numb=1;

if($i!=$num){
$res.="['".$date."', ".$numb."], ";
}
else{
$res.="['".$date."', ".$numb."]";
}

}

Upvotes: 0

Views: 135

Answers (1)

SteveP
SteveP

Reputation: 19093

The simplest thing would be to modify your SQL to sum it for you. If you are trying to display the count of each date, then change your query to:

SELECT date,count(*) as num FROM downloads WHERE prjID='".$_GET['id']."' GROUP BY date ORDER BY date ASC"

Upvotes: 1

Related Questions