0
votes

I have php-page retrieving sqlight-data. The lower part is the important in which I prepared it for Flot graph plotting (unix epoch timestamp as time series data in an array). What am I doing wrong? Please advice.

echo json_encode($dataset1) in php part

gives:

[1320710855000,44][1320711755000,46][1320712655000,44][1320713555000,42][1320714456000,56].. and Chromes Resources show:

script type="text/javascript"> var kurve = [1320774758000,72];

<?php
 error_reporting(E_ALL);
 try {
    /*** DB erstellen ***/
    $dbh = new PDO('sqlite:/var/www/zaehler/inc/db/zaehler.sqlite');
    /*** set all errors to excptions ***/
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $startdate=startdate();
    $sql = "SELECT date FROM energielog ORDER BY OID DESC LIMIT 1";
    $result = $dbh->query($sql);
    foreach($result as $row) {
          if ($row['date']!=$startdate) {
              $startdate=$row['date'];
                  }
          }
    $sql = "SELECT kwh, peak, OID, date, time FROM energielog WHERE date >=".$startdate." ORDER BY OID ASC";
    $result = $dbh->query($sql);

    foreach($result as $row) {                
            $oid=$row['OID']-1;
            $sql2 = "SELECT kwh FROM energielog WHERE oid <".$oid." ORDER BY OID DESC LIMIT 1";
            $result2 = $dbh->query($sql2);
            foreach($result2 as $row2) {
                    $letzter_eintrag=str_replace(",",".",$row2['kwh']);
                    }
                    $aktueller_eintrag=str_replace(",",".",$row['kwh']);
                    $eintrag=round(($aktueller_eintrag-$letzter_eintrag)*1000/0.5,0);
                    $peak=$row['peak']*1000;                       
                    $time=str_pad($row['time'],6,'0',STR_PAD_LEFT);
                    $times=substr($time,0,2).":".substr($time,2,2);                       
                    $timeStamp=mktime(
                    substr($time,0,2),
                    substr($time,2,2),
                    substr($time,4,2),
                    substr($startdate,2,2),
                    substr($startdate,4,2),
                    substr($startdate,0,2)
                    );                        
                   $dataset1 = array(($timeStamp)*1000, $eintrag);                       
                   echo json_encode($dataset1);
            }
}
catch(Exception $e)
{
    echo $e->getMessage();
    die();
}   
?>  
<div id="placeholder" style="width:600px;height:300px;"></div>
<script type="text/javascript">

var kurve = <?php echo json_encode($dataset1); ?>;

$(function () {    
$.plot($("#placeholder"), [kurve], { xaxis: { mode: "time",  timeformat: "%y/%m/%d/%H:%M"} });    
});
</script>
1

1 Answers

1
votes

Looking at this real quick a couple of things jump out at me.

One:

var kurve = "<?php echo json_encode($dataset1); ?>";

You do not want the " around the PHP. This will cause the kurve var to a be string not an array of arrays.

Two:

[1320710855000,44][1320711755000,46][1320712655000,44][1320713555000,42][1320714456000,56]

Did you or the echo drop the , between each array point? This would not be valid JS syntax.

EDITS

I see it now. dataset1 is being re-declared on each loop. You want:

$dataset1 = array();
foreach($result as $row) {
  <do your processing..?
  array_push($dataset1, array(($timeStamp)*1000, $eintrag));
}