2015-05-17 52 views
1

我的任務是使用數據庫中的數據創建圖形。我已經用給定的信息完成了以下PHP代碼。使用PHP的圖形用法(使用數據庫中的數據創建條形圖)

<?php 
# ------- The graph values in the form of associative array 
$values1 = array (
    "Shopping" => 8491, 
    "Transport" => 8098, 
    "Food & beverages" => 7975, 
    "Accommodation" => 6130, 
    "Expenditure" => 894, 
    "Others" => 2667 
); 

$values2 = array (
    "Shopping" => 13149, 
    "Transport" => 10019, 
    "Food & beverages" => 9691, 
    "Accommodation" => 5028, 
    "Expenditure" => 1097, 
    "Others" => 3362 
); 


$img_width = 450; 
$img_height = 300; 
$margins = 20; 


# ---- Find the size of graph by substracting the size of borders 
$graph_width = $img_width - $margins * 2; 
$graph_height = $img_height - $margins * 2; 
$img = imagecreate($img_width, $img_height); 


$bar_width = 20; 
$total_bars = count($values1 + $values2); 
$gap = ($graph_width - $total_bars * $bar_width)/($total_bars + 1); 


# ------- Define Colors ---------------- 
$bar_color = imagecolorallocate($img, 0, 64, 128); 
$bar_color2 = imagecolorallocate($img, 100, 64, 128); 
$label_color = imagecolorallocate($img, 0, 0, 0); 
$background_color = imagecolorallocate($img, 240, 240, 255); 
$border_color = imagecolorallocate($img, 200, 200, 200); 
$line_color = imagecolorallocate($img, 220, 220, 220); 

# ------ Create the border around the graph ------ 

imagefilledrectangle($img, -10, 1, $img_width + 8, $img_height - 2, $border_color); 
imagefilledrectangle($img, $margins, $margins, $img_width - 1 - $margins, $img_height - 1 - $margins, $background_color); 


# ------- Max value is required to adjust the scale ------- 
$max_value = max($values2); 
$ratio = $graph_height/$max_value; 


# -------- Create scale and draw horizontal lines -------- 
$horizontal_lines = 20; 
$horizontal_gap = $graph_height/$horizontal_lines; 

for($i = 1; $i <= $horizontal_lines; $i++) { 
    $y = $img_height - $margins - $horizontal_gap * $i ; 
    imageline($img, $margins, $y, $img_width - $margins, $y, $line_color); 
    $v = intval($horizontal_gap * $i/$ratio); 
    imagestring($img, 0, 0, $y - 5, $v, $bar_color); 
} 


# ----------- Draw the bars here ------ 
for($i = 0; $i < $total_bars; $i++) { 
    # ------ Extract key and value pair from the current pointer position 
    list($key, $value) = each($values1); 
    $x1 = $margins + $gap + $i * ($gap + $bar_width) ; 
    $x2 = $x1 + $bar_width; 
    $y1 = $margins + $graph_height - intval($value * $ratio) ; 
    $y2 = $img_height - $margins; 
    imagestring($img, 0, $x1 + 1, $y1 - 10, $value, $bar_color); 
    imagestring($img, 0, $x1 + 1, $img_height - 15, $key, $label_color);   
    imagefilledrectangle($img, $x1, $y1, $x2, $y2, $bar_color); 
} 

for($i = 0; $i < $total_bars; $i++) { 
    # ------ Extract key and value pair from the current pointer position 
    list($key, $value) = each($values2); 
    $x1 = ($margins + $gap + $i * ($gap + $bar_width)) + 22 ; 
    $x2 = $x1 + $bar_width; 
    $y1 = $margins + $graph_height - intval($value * $ratio) ; 
    $y2 = $img_height - $margins; 
    imagestring($img, 0, $x1 + 1, $y1 - 10, $value, $bar_color2); 
    imagefilledrectangle($img, $x1, $y1, $x2, $y2, $bar_color2); 
} 

header("Content-type:image/png"); 
imagepng($img); 

當我嘗試將給定的信息替換爲下面的代碼時,它不起作用。

include('conn.php'); 

# ------- The graph values in the form of associative array 
$values1_query = mysql_query("SELECT * FROM domestic_visitors WHERE year = 2010"); 
if(!$values1_query) { 
    die("Database query failed: " . mysql_error()); 
} 

while ($row = mysql_fetch_array($values1_query)) { 
    $values1_array = array($row["component"] => $row["total"]); 
} 

已經嘗試了幾天,但我只是不知道錯誤是什麼。

謝謝你的幫助。

回答

0

你在底部代碼有這些問題:

  1. 如果代碼是完全按照你所顯示的,那麼$values1_array是不一樣的變量名$values1這意味着你的條形圖的繪製代碼沒有按不知道從哪裏得到數據。
  2. 當您像從前那樣從SQL數據庫中檢索數據時,對於每個循環迭代,行$row = mysql_fetch_array($values1_query)意味着變量$row包含該行中的數據數組,並且僅包含一行!更多關於下面的內容。

處理數據(正常)

對於所使用來創建條形圖的代碼中,

$values1 = array (
    "Shopping" => 8491, 
    "Transport" => 8098, 
    "Food & beverages" => 7975, 
    "Accommodation" => 6130, 
    "Expenditure" => 894, 
    "Others" => 2667 
); 

是所謂的關聯數組。這意味着它是一個數組,其中的鍵/索引是一個名字! (它更像是其他編程語言中的地圖或字典)。而底部的代碼不能正確地構建這個數組。

因此,舉例來說,如果你的表是這樣的:

+---------+-----+----+ 
|component|total|year| 
+---------+-----+----+ 
|Texas |200 |2010| 
+---------+-----+----+ 
|Georgia |300 |2010| 
+---------+-----+----+ 

之後,第一次在循環中的代碼運行時,$ row變量將是

$row = array (
    "component" => "Texas", 
    "total" => 200, 
    "year" => 2010 
); 

和第二次,$行變量將是

$row = array (
    "component" => "Georgia", 
    "total" => 300, 
    "year" => 2010 
); 

請注意,由於您編寫了$values1_array = array($row["component"] => $row["total"]);,每個循環迭代將覆蓋$values1_array中的數據!這意味着在執行while循環之後,您將獲得相當於:

$values1_array = array (
    "Georgia" => 300 
) 

並且您已丟失了以前行中的所有數據。

我猜你可能想要做的事(你沒有提到預期的輸出是什麼)是將每行的數據追加/添加到$values1數組中。這裏有一種方法:

$values1 = array(); 
while ($row = mysql_fetch_array($values1_query)) { 
    $component = $row["component"]; 
    $total = $row["total"]; 

    $values1[$component] = $total; 

    // This can be shortened into: 
    // $values1[$row["component"]] = $row["total"]; 
} 

基本上,這個代碼初始化$ values1到一個空的數組。對於每個循環迭代,我們向數組中插入一個新條目,以使條目的鍵爲component,條目的值爲total

這樣,使用上面相同的例子,在while循環之前,$values1是一個空數組。在第一次迭代後,

$values1 = array (
    "Texas" => 200 
) 

在第二次迭代之後,下一個數據行被添加,並

$values1 = array (
    "Texas" => 200, 
    "Georgia" => 300 
) 

打印您的變量作爲一個調試技術

,如果你不知道已經知道這一點,但如果您遇到問題,打印變量的內容通常會很有幫助。然後你可以檢查變量是否包含你期望的數據。例如,在這種情況下,打印$values1_array(或$values1,無論哪個應該是正確的)的內容都會告訴您,該數組只包含來自SQL查詢返回的最後一行數據。您可以使用的一些有用的功能是print_rvar_dump,它們向您顯示除簡單echo之外的更多信息(請參閱PHP手冊以獲取有關如何使用這些函數的信息),並且可以使用它們查看數組的內容。

+0

試過你的代碼,它不起作用。該圖還沒有顯示出來。預期的輸出是這樣的 - > https://drive.google.com/file/d/0B4evf35NA5_5NG8zM2hUY2tRREk/view?usp=sharing –

+0

您的問題(第一個框)中的代碼已經生成了您發佈的預期輸出,當我嘗試。所以它已經滿足了預期的輸出!那麼你面臨的問題是什麼?據我所知,你已經編寫了繪製圖表的代碼(就像在第一個框中一樣),而你現在在使用MySQL表中的數據並將數據放入條形圖時遇到了問題,這就是我試圖回答。 –

相關問題