0

I was able to get some help on my original question in order to get a concatenated JSON output from mysql. So now I have a JSON array that I'm trying to get to display in a Morris JS line chart that sits on a PHP page.

Currently this is what a print_r($rows) returns:

Array ( [0] => Array ( [json_data_records] => {"dateRecorded": "2018-04-02 21:15:56","sensor2":64.96,"sensor1":60.91} ) [1] => Array ( [json_data_records] => {"dateRecorded": "2018-04-02 21:25:58","sensor2":64.51,"sensor1":60.91} ) [2] => Array ( [json_data_records] => {"dateRecorded": "2018-04-02 21:36:00","sensor2":64.06,"sensor1":60.91} ) [3] => Array ( [json_data_records] => {"dateRecorded": "2018-04-02 21:46:02","sensor2":63.61,"sensor1":60.35} ) [4] => Array ( [json_data_records] => {"dateRecorded": "2018-04-02 21:56:05","sensor2":63.27,"sensor1":60.46} ) ) 1

But I believe the chart is looking for a structure more like:

[
    {"dateRecorded": "2018-04-02 00:20:00","sensor2":69.91,"sensor1":68.11},
    {"dateRecorded": "2018-04-02 00:30:08","sensor2":69.69,"sensor1":68.00}
]



I tried to set my data input in the chart config to data: <?php echo $rows;?> but I get an error: PHP Notice: Array to string conversion


If I copy each line of the mysql output and put it into the data setting of the chart config, everything prints out as I would like so I know I have the right structure and config set, just need some help getting it from the array to formatted. Would I use some sort of a for loop to iterate through the array and print out each line?

1 Answer 1

0

I think it is not very good to mix javascript and PHP code.. I strongly advice to load the array -encoded in a JSON- from the server with jQuery $.ajax. So that you would not such kind of conversion problem and you could reload the chart content whithout reloading the whole page.

Otherwise, yes you should do a loop with PHP and print out each line.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you elaborate? The data is stored in mysql and upon initial page load, the data is queried and stored in a php variable which is then passed to the morris chart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.