score:1

Accepted answer

could construct the formatted series data to begin with like below:

<?php


/*
    get data from the mysql database and return it in json format
*/


//setup global vars
$debug = $_get['debug'];
$format = $_get['format'];

if($format=='json'){
    header("content-type: text/json");
}


$db = new mysqli('localhost', root, 'kudanil123', 'pt100', 3306);

if (mysqli_connect_error()) {
    die('connect error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

    if ($debug == 1) {echo 'success... ' . $db->host_info . "\n";}

// get data
$sql = "select meas_date,ai0_hist_value";
$sql .= " from ai0_hist";
$sql .= " where board_temp_hist_value > 30"; //filter out bad data
$sql .= " group by 1";
$sql .= " order by meas_date desc"; //highcarts requires you order dates in asc order
$sql .= " limit 5;";


if ($result = $db->query($sql)) {

    if ($debug == 1) {echo "fetched data! <br/><br/>";}


    while($row = $result->fetch_array()){
        $rows[] = $row;
    }

    foreach($rows as $row){
        $seriesdata[] = [ strtotime($row['meas_date'])*1000, (float)$row['ai0_hist_value'] ];

    }


       echo (json_encode($seriesdata));

       $result->close();

} else {
    echo "error: " . $sql . "<br>" . $db->error;
}

$db->close();

score:1

this will generate the array you want, there is no need to do all that fiddling with the data from the database

// get data
$sql = "select meas_date,ai0_hist_value";
$sql .= " from ai0_hist";
$sql .= " where board_temp_hist_value > 30"; //filter out bad data
$sql .= " group by 1";
$sql .= " order by meas_date desc"; //highcarts requires you order dates in asc order
$sql .= " limit 5;";


$rows = array();
if ($result = $db->query($sql)) {

    while($row = $result->fetch_array()){

        $rows[] = array(strtotime($row['meas_date'])*1000, 
                        $row['ai0_hist_value']
                        );
    }
}
echo json_encode($rows);

now you will need to convert the text to float in the javascript. this is because json is passed as text and not any other data type, so it has to be converted, if necessary in the receiving javascript.

score:2

if you want the code like that, you must fix the code:

while($row = $result->fetch_array()){
    $rows[] = $row;
}

foreach($rows as $row){

    $text[]  = (float)$row['ai0_hist_value'];
    $date[] = strtotime($row['meas_date'])*1000;

} 

//$data[0] = $names;
   $data1 = $date;
   $data = $text;
   $data2 = array($data1, $data);
  //$data[2] = $text;
   echo (json_encode($data2));

must be something like this:

while($row = $result->fetch_array()){
    $rows[] = array(
           (float)$row['ai0_hist_value'],
           strtotime($row['meas_date'])*1000);
}

echo (json_encode($rows));

you were saving in $data2 an array with two arrays, the text and the data. you must save a row for each pair of 'text' and 'data'.


Related Query

More Query from same tag