PHP function to output flot data from a SQL query

In the hopes that other find this useful, here’s a concise function that assumes the first field is foor the date and names date, the rest are multiple series in a chart. Edit to suit.

function sql2Flot($sql){
$result  = mysql_query($sql);
//Data for FLOT =¬† [ {data:[[x,y]],label:”str”} ]
//row loop
$arrData = array();
while ($row = mysql_fetch_assoc($result)){
//field loop
foreach($row as $name=>$value){
//create the different series. assumes the first col is x, the rest are diff y series
$intDate = date(‘U’,strtotime($row['date']))*1000;
if($y==0 & $x>0){array_push($arrData,array(“label”=>$name,”data”=>array()));}
if($x>0){array_push($arrData[$x-1]['data'], array($intDate,$value));}
return json_encode($arrData);

Be Sociable, Share!

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>