2016-03-20 44 views
1

我真的一直在努力解决这个问题,所以我认为它会保证打开一个新的问题,以便从更有经验的人那里获得帮助。在纯PHP中创建图表示例

我在下面的例子中合作,在纯PHP创建一个非常简单的图表http://code.web-max.ca/image_graph.php

问题

我加入我的代码,我评论过我的代码有问题大部分在首都。发生在$maxv变量设置为0从而导致错误除以零......但是当我复制和唯一的例子粘贴到我的编辑和运行它下面

的问题,这是工作,所以必须有将在我的代码某处的逻辑问题,我藏汉附上截图作为我的代码.....任何帮助非常感谢

IMG我的输出 enter image description here

//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 

谢谢哟你非常喜欢阅读!

回答

1

我注意到了一些事情。看起来你正在为你的$values数组中的每个元素添加一个逗号。

$values[] = '"'.$nr.'"'.','; 

我会改变这个,而不是。

$values[] = $nr; 

然后改变你的破灭行这个......

echo '$values are fine'. implode(", ", $values); 

接下来,我没有看到任何一个地方,你做的分工,除了这里你的示例代码...

$column_width = $width/$columns; 

因此,如果您的$values阵列中没有任何值,那么您应该得到除零误差的唯一时间。

+0

非常感谢你,让我检查一下,然后我会尽快给你回复 –

+0

ps我只加了一部分例子,因为我正在调试段为段,因此没有划分,看看例子链接如果你不'不介意 –

+0

更新:因为改变foreach循环和内爆作为推荐的第一个'$ maxv'值echo'ed现在是15,而不是9这是一个正确的方向步骤....但在此之后,仍然全部为零 –