Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DB Array to expected javascript format

I am implementing Highcharts in my application. It needs data in specific format.

The data in my table is as follows

enter image description here

The javascript needs data in below format

enter image description here

When I var_dump my x_axis array and y_axis array, I get below result

enter image description here

Which php functions should I use to format my array elements and pass to that JavaScript in that format?

data:[
     [<? echo PHP_some_function(" ' ", x_axis) ?>, <? echo (y_axis) ?>]   //quotes for x, no quotes for y value
]  //Moreover it should run for all the values in x_axis and y_axis

I need some logic here..

My final graph would look like

enter image description here

like image 504
Some Java Guy Avatar asked Dec 20 '12 15:12

Some Java Guy


2 Answers

The problem is your query.

It should be like the following.

SELECT x_axis, y_axis FROM yourTableName;

This way you'll get exactly the format that Highcharts needs. You just have to insert it inside an array.

like image 81
Ricardo Alvaro Lohmann Avatar answered Sep 28 '22 01:09

Ricardo Alvaro Lohmann


Assuming:

$x_axis = array('Safari', 'Opera', 'Firefox', 'IE', 'Chrome', 'Others');
$y_axis = array(10, 6, 40.5, 20, 10.6, 0.5);

This should work:

$data = array();
$length = count($x_axis);
for ($i = 0; $i < $length; $i++) {
    $data[] = array($x_axis[i], $y_axis[i]);
}
like image 36
Cerbrus Avatar answered Sep 28 '22 02:09

Cerbrus