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



RSS Twitter
Articles Php Functions

Dynamic Graphs with PHP, MySQL and GD - Page 7

by: Ramsey Nasser
|
February 19, 2003

Take a look at these two lines:

<?php
$currentnumquery 
mysql_query("SELECT * FROM poll WHERE vote='$votevalue'");
$currentnum mysql_num_rows($currentnumquery);
?>
The query in the first line is almost identical to the one used to find how many total votes there were ("SELECT * FROM poll"). The only exception is that there was a bit of discrimination here: only the votes with their 'HTML value' column matching the HTML value of the current vote option will be selected. This means that only the people who voted for the current vote option will be counted. This way, we get the number of votes for the current vote option, and we store it in the variable $currentnum using mysql_num_rows. Now that we have the total number of votes and the number of votes for our current vote option, we no longer need any outside data. The rest of the calculations will take place within the script. The graph in this example uses percents, not actual values. Were going to need to calculate these percents before the drawing begins. As usual, I put up two lines for analysis:

<?php
$per 
floor(($currentnum/$numvotes)*184);
$rper floor(($currentnum/$numvotes)*100);
?>
The two calculations are identical, but with one difference: one is in relation to 184, and the other to 100. $per is used to determine how high the current graph should be. It is multiplied by 184, the height of the y-axis. $rper (real percent) is simply the actual percentage that will be used only for display later on. We have determined, called, calculated and processed all the necessary data. Now its time to actually get results (the graph is still quite empty if you haven't checked).

<?php
imagefilledrectangle 
($im$xval, (200-$per), ($xval+$barwidth), 200$red);
imagerectangle ($im$xval, (200-$per), ($xval+$barwidth), 200$black);
?>
It may look like a lot, but the coordinates in both functions are the same. Each coordinate is an expression based off of all the calculations we just went through (except the last one). $xval is the current value for the left most side of the bar (essentially, the x coordinate for the upper-left hand corner) that will be incremented by $barwidth+10 later on. 200-$per uses $per, the height of the bar. Since we have the actual whole height of the bar, we are forced to work from down up, i.e. from 200 (the bottom-most line in the actual graph, but necessarily in the actual image) to the top, 0. Hence, we minus $per. The second x value is the same as the first, but with $barwidth added to it. The last value is the easiest: its 200 no mater what happens. 200 is the bottom-most point on the graph, and the last y value (the lowest y value) has to be on it. Mind you, if your lowest point is not 200, don't put and expect it to work :).

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