2016-03-20 92 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這是一個正確的方向步驟....但在此之後,仍然全部爲零 –