Jump to content

real time graph plotting


mariam

Recommended Posts

i am trying to plot a graph taking values from a database table in my sql.

follwing is the code

 

<?php

 

$host="localhost"; // Host name

$username="root"; // Mysql username

$password=""; // Mysql password

$db_name="mehreen"; // Database name

 

include "db.php";

mysql_connect($host, $username, $password)or die("cannot connect");

mysql_select_db($db_name)or die("cannot select DB");

 

$qt=mysql_query("select * from gd_graph");

header ("Content-type: image/jpg");

 

$x_gap=40; // The gap between each point in y axis

 

$x_max=$x_gap*13; // Maximum width of the graph or horizontal axis

$y_max=250; // Maximum hight of the graph or vertical axis

// Above two variables will be used to create a canvas of the image//

 

 

$im = @ImageCreate ($x_max, $y_max)

or die ("Cannot Initialize new GD image stream");

$background_color = ImageColorAllocate ($im, 234, 234, 234);

$text_color = ImageColorAllocate ($im, 233, 14, 91);

$graph_color = ImageColorAllocate ($im,25,25,25);

 

 

$x1=0;

$y1=0;

$first_one="yes";

while($nt=mysql_fetch_array($qt)){

//echo "$nt[month], $nt[sales]";

$x2=$x1+$x_gap; // Shifting in X axis

$y2=$y_max-$nt[sales]; // Coordinate of Y axis

ImageString($im,2,$x2,$y2,$nt[month],$graph_color);

//Line above is to print month names on the graph

if($first_one=="no"){ // this is to prevent from starting $x1= and $y1=0

imageline ($im,$x1, $y1,$x2,$y2,$text_color); // Drawing the line between two points

}

$x1=$x2; // Storing the value for next draw

$y1=$y2;

$first_one="no"; // Now flag is set to allow the drawing

}

 

ImageJPEG ($im);

 

?>

 

 

the error occured is:

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\drawgraph.php:2) in C:\xampp\htdocs\drawgraph.php on line 15

Link to comment
Share on other sites

The headers already sent error can be caused having white space (extra blank spaces before or after the opening and closing PHP tags.

(<?php  ?>)

all headers should generally be placed at the top of the page to avoid this. Also if you have included another file that is declaring a header before line 15 then that will cause a problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.