而if循环没有正确执行

时间:2011-11-07 12:32:40

标签: php if-statement while-loop

有人可以帮助我使用以下代码吗?输出正常,从“categories”标签开始,但if循环只返回db的最后一行。

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

2 个答案:

答案 0 :(得分:4)

问题在于:

$K5 = "<dataset seriesName ...

您在迭代的每个循环中重写$ K5。 建议的解决方案:

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

答案 1 :(得分:0)

我终于开始工作了。对于任何感兴趣的人,这是一个fusionchart 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);