$currentnumquery = mysql_query("SELECT * FROM poll WHERE vote='$votevalue'");
$currentnum = mysql_num_rows($currentnumquery);
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:
$per = floor(($currentnum/$numvotes)*184);
$rper = floor(($currentnum/$numvotes)*100);
$peris 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).
imagefilledrectangle ($im, $xval, (200-$per), ($xval+$barwidth), 200, $red);
imagerectangle ($im, $xval, (200-$per), ($xval+$barwidth), 200, $black);
$xvalis 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+10later 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
$barwidthadded 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 :).