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



RSS Twitter
Articles Php Functions

Dynamic Graphs with PHP, MySQL and GD - Page 6

by: Ramsey Nasser
|
February 19, 2003

Following the method used to get the number of options, we can similarly get the number of votes. Observe:

<?php
$pollquery 
mysql_query("SELECT * FROM poll");
$numvotes mysql_num_rows($pollquery);
?>
Both queries select all entries to the databases without discriminating. Using mysql_num_rows will give us the number of entries in the database, which is the number of vote options in the voteoptions database, or number of votes in the poll database. Now we have all the raw data that has to do with the actual votes, lets get back to the actual graph. There are two more variables that need to be set before the drawing begins:

<?php
$xval 
30;
$barwidth floor(300/$numoptions);
?>
Lets analyze. $xval is the value for the x position of the left-most side of the graph. This value will be manipulated later, but for now, it will be set to 30 (the y-axis's distance from the left hand side of the image, plus 5 pixels for some breathing space). $barwidth determines how wide each bar should be. The 300 used is simply 360 (the length of the x-axis) minus 60 for some more breathing space. This is then divided by the number of vote options. As you may have noticed, a lot of the original values and measurements from the image have been changed to account for "breathing space." This is my term for the space between the bars so that the graph doesn't seem to crowded. Technically, the script would work with out this extra space. But try it, and I assure you will develop a case of claustrophobia.
The actual drawing of the graph, what you started reading this article for. It would be obvious to any programmer that to execute any number of indefinite commands, a loop is required. So if you guessed that we need a loop to draw the bars, pat your self on the back. If not, its okay. My personal favorite loop is the for loop. This script can be rewritten using a while loop, or any other loop that you please, however. Well take this a line or two at a time. The first line is as such:

<?php
for ($i=0;$i<=($numoptions-1);$i++)
{
?>
The setting of $i to 0 and subtracting one from $numoptions may seem pointless, but its necessary. The mysql_result function, that will be used later, reads rows from MySQL tables starting at row 0, not row 1. Subtracting one from $numoptions is done so that the loop doesn't overshoot its target number, and try to draw an extra bar that doesn't exist. This might be a bit confusing, but the next two line will make things a bit clearer:

<?php
$voteoption 
mysql_result($optionsquery,$i,'name');
$votevalue mysql_result($optionsquery,$i,'value');
?>
Since each loop deals with and draws the bar for a different voting option, we need to know which option were dealing with. $voteoption takes on the value of the actual name (the 'name' column of the vote options table), and $votevalue stores the HTML value (the 'value' column of the same table), of the current vote option. The $optionsquery query was the query used earlier to get the total number of vote options. Keep in mind that this loop deals with one vote option at a time, the option that is on row $i. Since $i is incremented after each loop, all vote options are dealt with. We already know how many people have voted overall ($numvotes), we need to know how many people have voted for the current option specifically.

« 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.