Reputation: 5714
I have really been struggling with how to approach this problem, so I thought it would warrant it to open a new question to get some help from someone more experienced.
I am working on the following example to create a very simple chart in pure PHP http://code.web-max.ca/image_graph.php
Problem
I am adding my code, I have commented the problems I am having in the code mostly in capitals.
The problem occurs below when the $maxv
variable is set to 0
which leads to an error division by zero...however when I copy and paste only the example to my editor and run it, it is working so there must be a logical problem in my code somewhere, I am attaching a screenshot aswell as my code.....any help much appreciated
//create array retrieved from DB in code ABOVE and add "" and , to each value
foreach($t1Picks as $key => $nr){
$values[] = '"'.$nr.'"'.',';
}
echo '$values are fine'. implode($values);
echo '<br />';
//values are fine.....go ahead
// Get the total number of columns we are going to plot
$columns = count($values);
echo 'COLUMN COUNT IS FINE'.$columns;
echo '<br />';
//columns count is fine continue
// Get the height and width of the final image
$width = 300;
$height = 200;
// Set the amount of space between each column
$padding = 5;
// Get the width of 1 column
$column_width = $width / $columns;
$column_width = round($column_width, 0);
echo 'COLUMN WIDTH IS FINE'.$column_width;
echo '<br />';
// Generate the image variables
$im = imagecreate($width,$height);
$gray = imagecolorallocate ($im,0xcc,0xcc,0xcc);
$gray_lite = imagecolorallocate ($im,0xee,0xee,0xee);
$gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f);
$white = imagecolorallocate ($im,0xff,0xff,0xff);
// Fill in the background of the image
imagefilledrectangle($im,0,0,$width,$height,$white);
$maxv = 0; //I DONT UNDERSTAND THIS
//WHY MAX VAL 0?
// Calculate the maximum value we are going to plot
for($i=0;$i<$columns;$i++)$maxv = max($values[$i],$maxv); //WHY NO BRACE { ON FOR
echo 'MAXV TEST IS'.$maxv; //THE FIRST LOOP IS 9 AND THEN ZEROS
echo'<br />';
// Now plot each column
Thank you very much for reading!
Upvotes: 1
Views: 293
Reputation: 1767
A couple things I've noticed. It looks like you're adding a comma to each element in your $values
array.
$values[] = '"'.$nr.'"'.',';
I would change that to this instead.
$values[] = $nr;
Then change your implode line to this...
echo '$values are fine'. implode(", ", $values);
Next, I don't see anyplace in your example code that you're doing division except here...
$column_width = $width / $columns;
So the only time you should get a division by zero error would be if you had no values in your $values
array.
Upvotes: 1