2011-08-20 58 views
0

確定..我知道我能找到幫助這裏:)問題陣列和GoogChart

我剛剛走出noobhood的,所以要溫柔:)

我試圖從數據庫獲取數據和用它在GoogChart中調用一個餅圖,這是我的問題...一些像db連接等代碼被跳過以達到這一點。

首先我們來看看GoogChart用來傳遞信息的陣列:

$data = array(
    '8' => 6, 
    '3' => 3, 
    '9' => 2, 
    ); 

現在我們來看看如何我試圖做到這一點從數據庫把數據:

//connect and query here 
while ($row=mysql_fetch_array($query)){ 
$viewid=trim($row['id']); 

$total_views=trim($row['views']); 

// trimmed cuz I can't sort it out 

$dat = "'$viewid' => $total_views,"; //problem likely here 
} 



$data = array(
$dat 
); 

當我回應$ dat,我得到這個:

'8' => 6,'3' => 3,'9' => 2, 

所以理論上,它應該工作?但空操作:(

可能有這樣的一個完全不同的方式,但我很爲難......沒有采取很多工作要做,要麼笑。

回答

1

你在做什麼是創建一個數組與一種元素: 「 '8'=> 6, '3'=> 3, '9'=> 2,」

相反,您應填充的陣列,當您去:

$data = array(); // create the array 
while ($row=mysql_fetch_array($query)){ 
    $viewid=trim($row['id']); 

    $total_views=trim($row['views']); 

    // use the $viewid as the key and $total_views as the value 
    $data[ $viewid ] = $total_views; 
} 

當然,你也可以(不確定這是否可以幫助你,但它是一個選項):

$data = array(); // create the array 
while ($row=mysql_fetch_array($query)){  
    // use the $viewid as the key and $total_views as the value 
    $data[ trim($row['id']) ] = trim($row['views']); 
} 
+0

......或者你可以'eval(「\ $ data = array($ dat);」);'。但是不要。 – DaveRandom

+0

我gotcha ...感謝兄弟 –