Open Flash Chart
Support This Project :: Home - Tutorials - Database
Info: Open Flash Chart 2 is out. Version 1.x and these pages will never disappear and the charts will continue working forever, but further development on 1.x has stopped. Take a look at version 2 here Open Flash Chart 2.

Get data from the database.

A simple example where we query the database for some values, then display the results in the graph.

Add this to your PHP:

Create the flash object
include_once 'ofc-library/open_flash_chart_object.php';
open_flash_chart_object500250'http://'$_SERVER['SERVER_NAME'] .'/open-flash-chart/data-3.php?id=1' );

As I don't have any data tables in my database, I have created an SQL query that generates some sin() values to display. You would replace the SQL query in the example with one that queries your database like normal. I have also hidden my connection details in a seperate include file (see the comments for details.)


// What you will probably do is:

if( isset( $_GET['id'] ) )
  $user = intval( $_GET['id'] );
  $sql = 'SELECT * FROM results WHERE fk_user='.$user;


// I don't have any tables set up, so I
// simulate getting data from the database.
// This SQL will get the DB to produce a
// nice sin wave:
$t = array();
$i=0$i<(4*3.14); $i+=0.3)
$t[] = 'select sin('$i .')';

$sql implode' union '$t );

// This opens the db connection as usual:
//   $db = mysql_connect("localhost", "user","***") or die("Could not connect");
//   mysql_select_db("database",$db) or die("Could not select database");
// Uncomment the above lines and fill in the db, user name and password, then
// delete the following two lines:
include_once( 'includes/db.php' );
$db openDataBase( );

$data = array();
$res mysql_query($sql,$db) or die("Bad SQL 1");
$row mysql_fetch_array($res) )
$data[] = floatval$row[0] ) + 1.5;

// use the chart class to build the chart:
include_once( 'ofc-library/open-flash-chart.php' );
$g = new graph();
$g->title'Sin + 1.5''{font-size: 12px;}' );

$g->set_data$data );

// display the data
echo $g->render();

To see the data produced : data-3.php, then 'view source'.

If you are interested in what the SQL command looks like, try pasting in an 'echo $sql;' into the code, then browse to that page to see it (because the SQL has been inserted into the data the Open Flash Chart will not render this data file, so take out the echo command to view the data as a chart.)

Help support the project: Support This Project