2011-11-07 94 views
0

有人可以幫我用下面的代碼嗎?輸出工作正常,從「categories」標記開始,但if循環只返回數據庫中的最後一行。而如果循環執行不正確

$strXML = "<chart> \n"; 


$strQuery = "select inc_type, sum(num_of_occur) as cnt from inc_detail 
group by inc_type"; 

$query2 = mysql_query($strQuery); //call string query 

$strCategories = "<categories>\n"; //create categories 
while ($cat = mysql_fetch_array($query2)) 
{ 
$strCategories .= "<category label='" . $cat['inc_type'] . "' /> \n"; //display categories 
}; 
$strCategories .= "</categories> \n"; 

$strQuery2 = "select agency, inc_type, sum(num_of_occur) as cnt from inc_detail group by inc_type, agency order by agency"; 
$query3 = mysql_query($strQuery2); //call string query 
$agency = null; 
while ($ds = mysql_fetch_array($query3)) 
{ 
    if($ds['agency'] != $agency) 
    { 
    $K5 = "<dataset seriesName='" . $ds['agency'] . "' /> \n"; //create dataset 
    $agency = $ds['agency']; 
    } 
    $K5 .= "<set value='" . $ds['cnt'] . "' /> \n"; //display value of dataset 
    $K5 .= "</dataset> \n"; 
} 


$strXML .= $strCategories . $K5 . "</chart>"; //end of XML 


echo $strXML; 

回答

4

的問題是在這裏:

$K5 = "<dataset seriesName ... 

你改寫$ K5迭代的每個週期。 建議解決方案:

$K5 = ""; 
while (... 
... 
    $K5 .= "<dataset seriesName ... 
+0

感謝您的回覆,但我不明白你的解決方案。我瞭解while循環,但我是編程新手。如果你有一個非常有用的例子。 – user1033720

0

我終於得到了它的工作。對於任何感興趣的人來說,這是一個融合圖表php文件,用於抓取多數據條形圖的mysql數據。以下是我的新代碼:

$strXML = "<chart labelDisplay='Rotate' slantLabels='1' caption='Test' subCaption='By Quantity' decimalPrecision='0' showValues='0' showNames='1' numberSuffix=' Incidents' formatNumberScale='10'>"; 

$strQuery = "select inc_type, sum(num_of_occur) as cnt from inc_detail 
group by inc_type"; 

$query2 = mysql_query($strQuery); //call string query 

$strCategories = "<categories>"; //create categories 
while ($cat = mysql_fetch_array($query2)) 
{ 
$strCategories .= "<category label='" . $cat['inc_type'] . "' />"; //display categories 
} 
$strCategories .= "</categories>"; 

$strQuery2 = "select agency, inc_type, sum(num_of_occur) as cnt from inc_detail group by inc_type, agency order by agency"; 
$query2 = mysql_query($strQuery2); //call string query 
    $K5 = $blank; 
     while ($ds2 = mysql_fetch_array($query2)) 
     { 
     $K5 .= "<dataset seriesName='" . $ds2['agency'] . "' />"; //create dataset 

       //create dataset values 
         $strQuery4 = "select inc_type, sum(num_of_occur) as cnt from inc_detail where agency = '$ds2[agency]' group by inc_type"; 
         $query4 = mysql_query($strQuery4); 
         while ($ds4 = mysql_fetch_array($query4)) 
         { 
          $K5 .= "<set value='" . $ds4['cnt'] . "' />"; //display value of dataset 
       } 
    $K5 .= "</dataset>"; 
    } 


$strXML .= $strCategories . $K5 . "</chart>"; //end of XML 
echo renderChart("fc/MSColumn3D.swf", "", $strXML, "productSales", 900, 600);