0

I am trying to generate a php-graph, but not with fixed data, rather with dynamic data from mysql database. For the graph I use the code of KoolPHPSuite.

I have come pretty far, for someone with almost no experience with php coding. I only need the cherry on the cheesecake!

So to give you some context; this is my code that generates a Line-Graph.

    <?php 

     require "KoolControls/KoolChart/koolchart.php";

    $chart = new KoolChart("chart");
    $chart->scriptFolder="KoolControls/KoolChart";
    $chart->Width = 1000;
    $chart->Title->Text = "Patient's Progression Week#";
    $chart->PlotArea->XAxis->Title = "Days";
    $chart->PlotArea >XAxis>Set
(array("Monday","Tuesday","Wednesday","Thursday","Friday"));
    $chart->PlotArea->YAxis->Title = "Value";
    $chart->PlotArea->YAxis->Set(array("10","20","30"));
    $series = new LineSeries();
    $series-> $ds;
    $series->ArrayData(array(10, 30, 25, 50, 40)); //Here you can see they y-coordinates per day. But I fixed these myself and as you have noticed, they aren't from my database.
    $series->ArrayData(array(10, 20, 30));
    $chart->PlotArea->AddSeries($series); 
?>
<html>
    <head>
        <title>GRAPH</title>
    </head>
    <body>
        <?php echo $chart->Render(); ?>
    </body>
</html>

So you have read my comment after the $series line about the fixed data. This code displays a respectful line graph. However, it's just the beginning of my idea.

So I began to think, "Ok, so if the format for the code is: 10, 30, 25, 50, 40 than I should write a php that 'SELECTS' the respectful data from a specific table from my database and displays the inputs just like that format, Than I should hook that output to a variable and put that variable into the place of X just like this: $series->ArrayData(array(X));

So I wrote this:

<?php
// Connect to MySQL
$link = mysql_connect( '', '', '');
if ( !$link ) {
  die( 'Could not connect: ' . mysql_error() );
}

// Select the data base
$db = mysql_select_db( 'WebApplication', $link );
if ( !$db ) {
  die ( 'Error: could not select database \'WebApplication\' : ' . mysql_error() );
}

// Fetch the data
$query = " SELECT heartrate FROM info;";
$result = mysql_query( $query );

// All good?
if ( !$result ) {
  // Nope
  $message  = 'Bad Query: ' . mysql_error() . "\n";
  $message .= 'Whole Query: ' . $query;
  die( $message );
}

$prefix = '';
echo "\n";
while ( $row = mysql_fetch_assoc( $result ) ) {
  echo $prefix . "\n";
  echo $row['heartrate'];
  $prefix = ",\n";}
  echo "\n";

// Close the connection
mysql_close($link);
?>

This generates the following:

72.5, 64, 100, 93, 98, 84, 74, 96, 68, 57

Which is the good format, but how do I make a variable that is hooked to the output (72.5, 64, 100, 93, 98, 84, 74, 96, 68, 57) and put that variable into the place of the X ?

I have tried some things here and there, for instance putting into the first code a line like this:

$source_ds = "db_connect.php";

and substituting that X with $source_ds

But that just runs the php code..

Thank you very much!

-M

1 Answer 1

1

replace:

echo $row['heartrate'];

with:

$heartrate[]=$row['heartrate'];

This will create the array to use for the graph code

Then replace:

$series->ArrayData(array(10, 30, 25, 50, 40)); 

with:

$series->ArrayData($heartrate); 

Uses your 'dynamic' data for the graph

To be proper if this works you should add

$heartrate=array();

above

while ( $row = mysql_fetch_assoc( $result ) ) {
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Dagon, I'll try this immediately !
post any issues and i'll try and help
May God Bless you, It worked. The graph show all the right coordinates! Only thing left is, it also outputs comma's like this: , , , , , , , , , That must be from the normal format output.
Every solution, should be rewarded ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.