Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Flot MySQL PHP

Name: retarded 2012-02-04 2:12

I need to parse the results of an sql query so I can graph it in flot. The only examples flot gives are of when the data is already in the correct format e.x) [[a,b][a1,b1]].

I am trying to figure out how to turn a sql query of



$sql = "select temp,date from database order by date asc



into


<script type="text/javascript">
$(function () {
    var d2 = [];
 var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];

$.plot($("#placeholder"), [
        {
            data: d2,
            lines: { show: true, fill: true }
        },
]


Any hints? Only thing I could find on google was using python to parse it out.

Name: Anonymous 2012-02-04 2:17

Also I read that the mysql timestamps are smaller than the kind used in flot, so I need to concatenate ."000" to the end of the timestamp.

Name: Anonymous 2012-02-04 3:07



$statement = $dbh->query($sql);
$data = $statement->fetchAll( \PDO::FETCH_ASSOC );

$response = array();
foreach($data as $data_row) {
  $response .= '['.$data_row['temp'].', '.$data_row['date'].'000]';
}

?>
<script type="text/javascript">
var mydata = [ <?=implode(", ", $response)?> ];
</script>

Name: Anonymous 2012-02-04 4:00

>>3
Shit I should have checked earlier, I just figured it out.



$connection = mysql_connect($server,$user,$password) or die ("Could not connect to server.".mysql_error());
   
$db = mysql_select_db($database,$connection) or die ("Could not select database.");
   
$query = "select UNIX_TIMESTAMP(date)*1000,temp_1 from database order by date asc";
 
$result = mysql_query($query) or die("Could not query ".mysql_error());

while($row = mysql_fetch_array($result))

{
$dataset[] = array($row[0],$row[1]);

}

$data_temp =  json_encode($dataset);

?>


And then


var d = <? echo $data_temp ?>;


I just spent the last half hour being retarded and not realizing that I needed to ``echo" $ data, rather than just var d = <? $data ?>;


But I have  a working graph now that looks pretty awesome

Thanks for your code

Don't change these.
Name: Email:
Entire Thread Thread List