L Ja
L Ja

Reputation: 1506

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.

Upvotes: 1

Views: 106

Answers (2)

Roleg
Roleg

Reputation: 111

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.

Upvotes: 1

Snm
Snm

Reputation: 435

Dont know whether the below code helps you

<?php
$sql = "SELECT id, value FROM sample";
  $query = mysqli_query($connect, $sql);

  while($res = mysqli_fetch_array($query))
  {
      $out[] = "[".$res[0].','.$res[1]."]";
  }
  $comma_separated = implode(",", $out);
  echo $comma_separated;
?>

Upvotes: 0

Related Questions