PHPBuilder - Dynamic Graphs with PHP, MySQL and GD Page 5



RSS Twitter
Articles Php Functions

Dynamic Graphs with PHP, MySQL and GD - Page 5

by: Ramsey Nasser
|
February 19, 2003

Instead of imagecreate, imagecreatefrompng was used. This stores the base image, "graphtemp.png", resource into $im, rather than just white. Here are the dimensions etc. for the picture, incase you decide to make one to follow along: it is 400x220, the y-axis is 25 pixels away from the left hand of the image and the x-axis is 200 pixels away from the top of the image, the y-axis is 184 pixels in height and the y-axis is 360 pixels in length. Keep these numbers in mind, as they will be used later in the calculations. Next, we allocate the colors to be used. Using black for the outline gives a good effect, and the inside color is you choice. I will use red in this example. So

<?php
$red 
imagecolorallocate ($im25500);
$black imagecolorallocate ($im000);
?>
Nothing new. Now, a graph is quite empty without data, so the next step is to get the data that will fill this graph up. We connect to the MySQL database:

<?php
mysql_connect
("localhost""user""password");
mysql_query("USE database");
?>
Remember, of course, to change the host, user, password and database to match your servers set up (so that's why it wouldn't work! :)). Now that we have the connection, we can begin to call data. The first and most important number, yes even more important than knowing how many votes have been cast, is the number of vote options you are working with. Without this piece pf data, there would be no way to ensure that the right number of bars has been drawn. Call this magic variable as such:

<?php
$optionsquery 
mysql_query("SELECT * FROM voteoptions");
$numoptions mysql_num_rows($optionsquery);
?>
Using simple MySQL functions, we now know the number of vote options were working with, and that number is stored in the variable $numoptions. Even though just be looking at the MySQL table we can tell that there are four options, this may change. Having this number loaded dynamically allows the coder to have a truly dynamic graph system. Now, we need to know how many people have voted. This is a number that can't be checked manually, not with any ease for that matter.

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Ramsey Nasser

Comment:



Comment:

(Maximum characters: 1200). You have characters left.