而if循环没有正确执行

Can someone help me with the following code? The output works fine starting with the "categories" tag but the if loop only returns the last row from the db.

$strXML = "<chart> 
";


$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";
$query3 = mysql_query($strQuery2); //call string query
$agency = null;
while ($ds = mysql_fetch_array($query3))
{
    if( $ds['agency'] != $agency )
  {
    $K5 = "<dataset seriesName='" . $ds['agency'] . "' /> 
"; //create dataset
    $agency = $ds['agency'];
  }
    $K5 .= "<set value='" . $ds['cnt'] . "' /> 
"; //display value of dataset
    $K5 .= "</dataset> 
";
}


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


echo $strXML;

The problem is here:

$K5 = "<dataset seriesName ...

You rewrite the $K5 every cycle of the iteration. Suggested solution:

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

I finally got it working. For anyone interested, this is a fusionchart php file grabbing mysql data for a multiseries bar chart. Below is my new code:

$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);