2

I need to add point from data which I get from mysql database. At this time, i get data by json but i don't know why in the output array, data have double quote(" ") like this:

["{name:'Chip 3',data:[[moment('2015-05-14 13:26:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13
:26:51','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13:27:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14
 13:27:51','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13:28:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14
 14:42:54','HH:mm:ss').valueOf(),32],]}"]

So Highcharts can not access to the data and show data on chart. Now I need to remove double quote from array or do something else to make Highcharts can regconize data.

This is my code in data.php file which i use to get and update series data.

<?php

header("Content-type: text/json");


 include_once 'include/connection.php';
 $db = new DB_Class(); 



     $query = "select distinct idchip from datatable ";
     $result = mysql_query( $query );
     $rows = array();
     $count = 0;
     $getall = array();
     while( $row = mysql_fetch_array( $result ) ) {

         $table = array();

         $query2 = "select datetime,temperature from datatable where idchip=".$row['idchip'].' group by datetime ';

            $dataresult = mysql_query($query2);
            while($datarow = mysql_fetch_array($dataresult))
            {

                $data = '';


                $datatimes .= $datarow['0'].',';


                $data .= "[moment('".$datarow['0']."','HH:mm:ss').valueOf(),".(integer)$datarow['1']."],";
                $stringdata .= $data;

                }

        $newstring = $stringdata ;

        $stringdata = '';
        $stringtime = '';

        $namedata = "{name:'Chip ".$row["idchip"]."',data:[$newstring]}";
        $getall[] = $namedata;


     }


echo json_encode($getall);
?>

This is code which i use to get ajax return data.

function getData() {
        jQuery.ajax({
            url: 'data.php',
            type: 'GET',
            dataType: 'json',
            mimeType: 'multipart/form-data',
            contentType: false,
            cache: false,
            processData: false,
            success: function( data, jqXHR ) {
                if( data == "null" ) {

                } else {
                  $.getJSON("data.php", function(json) {

            chart = new Highcharts.Chart({
               chart: {
                renderTo: 'container',
                defaultSeriesType: 'spline',
                events: {
               //     load: requestData
                }
            },
 series: json

            });
        });
                }
            },
            error: function( textStatus ) {
                console.log(" error. damm. ");
                //console.log(error);
            }
        });
    } 
duong khang
  • 342
  • 1
  • 4
  • 19
  • 2
    Problem is with the way you constructing the json object in php. Please check out this [json](http://stackoverflow.com/questions/682260/returning-json-from-php-to-javascript) . You don't have to create the json string. You need to create final array then simply use json_encode($getall); that will do it – irfan rasool May 14 '15 at 08:57
  • @irfanrasool the problem is if i add by json array i can't add [moment(...)] or some thing like this in array. can you show me how to do this please. – duong khang May 14 '15 at 09:10
  • Generally in the json you cannot use functions, because will not run / evalutate, so irfan rasool suggestion is correct. – Sebastian Bochan May 15 '15 at 09:40
  • @SebastianBochan can you help me on this link please, i have a right direction to follow, :D. http://stackoverflow.com/questions/30233041/highcharts-add-point-to-line-chart-with-json?noredirect=1#comment48612240_30233041 – duong khang May 15 '15 at 09:43
  • Your link is the same as current topic. – Sebastian Bochan May 15 '15 at 09:49
  • sorry @SebastianBochan my bad, this is right link: http://stackoverflow.com/questions/30256040/add-dynamic-data-to-line-chart-from-mysql-database-with-highcharts?noredirect=1#comment48612681_30256040 – duong khang May 15 '15 at 09:53

1 Answers1

0

I think something like this

    $query = "select distinct idchip from datatable ";
    $result = mysql_query( $query );
    $rows = array();
    $count = 0;
    $getall = array();
    while( $row = mysql_fetch_array( $result ) ) {

        $table = array();
        $stringdata = array();
        $query2 = "select datetime,temperature from datatable where idchip=".$row['idchip'].' group by datetime ';

        $dataresult = mysql_query($query2);

        $stringdata = array();
        while($datarow = mysql_fetch_array($dataresult))
        {
            $stringdata[] = "moment('".$datarow['0']."','HH:mm:ss').valueOf(),".(integer)$datarow['1'];
        }

        $namedata['name'] = "Chip ".$row["idchip"];
        $namedata['data'] =  $stringdata;
        $getall[] = $namedata;
    }

    echo json_encode($getall);
irfan rasool
  • 158
  • 1
  • 2
  • 12
  • with this code output data will be like this: [{"name":"Chip 3","data":["moment('2015-05-14 13:26:21','HH:mm:ss').valueOf(),29","moment('2015-05-14 13:26:51','HH:mm:ss').valueOf(),29",}] but the format which Highcharts need is like this: [name:"some thing ", data:[moment('date','HH:mm:ss').valueOf(),'another data' ]]; – – duong khang May 14 '15 at 10:09
  • then you have to create js flat array which you will feed to highchart instance – irfan rasool May 14 '15 at 10:47
  • Show me the output that u getting me from server side and the output that u need for the highchart so that I will js for you – irfan rasool May 15 '15 at 05:49
  • this is output from my code: [{"name":"Chip 3","data":[["moment('2015-05-14 13:26:21','HH:mm:ss').valueOf()",29],["moment('2015-05-14 13:26:51','HH:mm:ss').valueOf()",29],["moment('2015-05-14 13:27:21','HH:mm:ss').valueOf()",29]]. – duong khang May 15 '15 at 06:21
  • and this is format what i need from the output. [ {"name":" name retrieved from mysql", "data":[moment:('date from mysql','HH:mm:ss'.valueOf()), data from mysql ], [another moment point] } ]. And this is example data which i need (don't have double quote around moment....):. {name:'Chip 3',data:[[moment('2015-05-14 13:26:21','YYYY-MM-DD HH:mm:ss').valueOf(),29] ] } thank you so much, – duong khang May 15 '15 at 06:22
  • please help me http://stackoverflow.com/questions/30233041/highcharts-add-point-to-line-chart-with-json?noredirect=1#comment48612240_30233041 :( – duong khang May 15 '15 at 09:43
  • Sorry for late reply. So, you want yout ouput like this only `[ {"name":" name retrieved from mysql", "data":[moment:('date from mysql','HH:mm:ss'.valueOf())], [another moment point], [another moment point], [another moment point]} ]` and not like this `[{"name":"Chip 3","data":[[moment_1],[moment_2]},{"name":"Chip 4","data":[[moment_1],[moment_2]}]` Right? – irfan rasool May 15 '15 at 10:01
  • just like this @irfran [ {"name":" name retrieved from mysql", "data":[moment:('date from mysql','HH:mm:ss'.valueOf()), data value]\], [another moment point, data value], [another moment point, data value], [another moment point, data value]} ] – duong khang May 16 '15 at 17:15
  • can you pls share the code,me too having issue in this section – Rp9 Mar 05 '18 at 04:18