I am trying to pass data from a mysql query into an array in order to create a graph in JPGraph.

Conceptually it seems very simple! I have hard coded the data from the query and it creates the graph, the problem appears to be the way in which I am attempting to pass data into the array for JPGraph.

JPGraph uses a $variable = array (data) to operate.. how does one pass data (whether pre formatted or created via concatenate loops in a query) into an array() statement?

My code that half works...

<?php
include ("../jpgraph.php");
include ("../jpgraph_line.php");

mysql_connect("foo", "bar", "foo");
mysql_select_db("foo");

//whip out preformatted data from the db (in format 22.45,45.23 etc etc)

$row = mysql_query ("SELECT * FROM fullresult where id=1");

// fetch the data required (only 1 row in db at present)

$result = mysql_fetch_array($row);

$driver1 = $result['laps'];

// test the output (which works)

echo $driver1,"<br>";

// crunch point! this is where I attempt to make an array
// Normal Usage = $ydata = array(22.45,45.23,etc,etc);

// My attempt to extract data..

$ydata = array($driver1);

//OR

$ydata[] = $driver1

// Neither works!

//After this it all works..

// Create the graph. These two calls are always required
$graph = new Graph(800,300);
$graph->SetScale("textlin");

// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor("blue");
$lineplot->SetWeight(1);

// Add the plot to the graph
$graph->Add($lineplot);
$graph->SetShadow();

// Display the graph
$graph->Stroke();
?>

Very grateful for any pointers.

Oo

    2 months later

    Hello,
    there is a couple ideas I have that may help:

    1) you can not have any echo statements or print statements in this file, since this is a generated image.

    2) let's say you have this:
    $arr = array(1,2,3,4);
    then you should be able to simply use:
    $ydata = $arr;
    that's it, since $arr is an array already, you don't have to use the array() function.

    3) you can use explode() if you have some data like this for example:
    $data = "12,34,11,3";
    then do an explode:
    $data = explode(",", $data);
    $ydata = $data;

    I just discovered the JpGraph, it's an amazing piece of work! Credits to this guy.

    I hope this helped,
    DrTebi

      Write a Reply...