query mysql database using php for plotting with morris.js -
i have mysql database query format. note: in real dataset there more categories in series field aswer should able handle unknown amount of categories (in case there two: licensed , sorned)
period series values 1349046000000 licensed 3407 1349046000000 sorned 660 1313103600000 licensed 3351 1313103600000 sorned 629
i can't find way convert table using php format required morris.js library. such as: (following example)
{"period": 1349046000000, "licensed": 3407, "sorned": 660}, {"period": 1313103600000, "licensed": 3351, "sorned": 629}
here php code i've tried write, outputs wrong format, it's table. required output of library should have each serie columns in "wide" format.
<?php mysql_connect("localhost","root","martin"); mysql_select_db('table'); $query = "select unix_timestamp(period) * 1000 period, series, values table"; $result = mysql_query($query); $data = array(); while($data[] = mysql_fetch_assoc($result)); echo json_encode($data); ?>
here solution in php pivot table "feed" morris.js chart.
<?php mysql_connect("localhost","root","martin"); mysql_select_db('table'); // first query fields of pivot table $query_fields = "select series dominio table"; $result_fields = mysql_query($query_fields); $var = array(); while ($row = mysql_fetch_assoc($result_fields)) { $var[] = $row['series']; } // here write "pivot" query dinamically foreach($var $i){ $query2 .= ",max(case when series = '$i' values end) $i"; } $query1 = "select period "; $query3 = " table group 1"; $query_pivot = $query1 . $query2 . $query3; $result_pivot = mysql_query($query_pivot); $output = array(); while($row = mysql_fetch_assoc($result_pivot)){ $output[] = $row; } //print_r($output); echo json_encode($output); ?>
now using ajax call can morris.js plot this
Comments
Post a Comment