Search code examples
phpjqueryjsonflot

jQuery flot plugin data from SQL


I am currently using the jQuery plotting plugin (Click), but I am having trouble fetching the data out of my database.

I am using PHP for the SQL part, which returns an array like this:

Array (
    [0] => 13
    [id] => 13
    [1] => 320.82
    [real_value_1] => 320.82
)

Now I need it in the following format in order for the flot plugin to pick it up:

[13, 320.82]

But I am having trouble figuring out how.

This is the plotting code:

$(document).ready(function(){
    var url = "view_json_graph.php?type=2";
    $.get(url, function(data){
        console.log(data);
        $.plot($("#placeholder"), data, { yaxis: { max: 100 }, xaxis: { max: 100 } });
    });
});

Where the javascript var data, would be the data returned ( [13, 320.82] ).

Could anyone please give me advice on how to do it? I've also looked into JSON, but I couldn't get it to work with JSON either.


Solution

  • You should simplify your array first, so it would only contain the values only once:

    Array (
        [0] => 13
        [1] => 320.82
    )
    

    After that, you could use php's implode() function, to make your array into a string, separated by commas:

    $result = '[' . implode( ',', $array ) . ']';
    

    This will be your desired format.