请原谅我的新手问题。
我正在尝试将mysql表中的数据显示到图表中(phpChart http://phpchart.net) 我需要的是显示区域'CA'的所有现场成本值
E.g
ID Cost Region
----------
1 500 CA
----------
2 100 DP
----------
3 280 CA
----------
4 40 ST
----------
5 80 CA
----------
<?php
include 'config.php';
$query = "SELECT Cost From tblForecast where Region='CA' =";
$rs = mysql_query($query);
if (!$rs) {
echo "Could not execute query: $query";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Query: $query executed\n";
}
$nrows = mysql_num_rows($rs);
for ($i = 0; $i < $nrows; $i++) {
$row = mysql_fetch_row($rs);
echo $row[0];
echo "<br/>";
}
$pc = new C_PhpChartX(array(array($row[0])),'basic_chart');
\\"This where i'm stuck as to how display the other values within the array"
$pc->set_title(array('text'=>'Basic Chart with Bar Renderer'));
$pc->set_series_default(array('renderer'=>'plugin::BarRenderer'));
$pc->draw();
mysql_close();
?>
我只能在图表中显示第一行值。
任何人的帮助将不胜感激 提前致谢
答案 0 :(得分:4)
您不断将查询结果提取到单个变量中,并在循环的每次迭代中覆盖该数据。
试试这个:
$data = array();
while ($row = mysql_fetch_array($rs)) {
$data[] = $row[0];
}
$pc = new C_PhpChartX($data, 'basic_chart');
另外,虽然这很可能是剪切/粘贴类型,但您的查询中仍然存在语法错误:
$query = "SELECT Cost From tblForecast where Region='CA' =";
^---syntax error
答案 1 :(得分:0)
您可以使用GROUP_CONCAT()
include 'config.php';
$query = "SELECT GROUP_CONCAT(Cost) cost
From tblForecast
where Region='CA'";
$rs = mysql_query($query);
$row = mysql_fetch_row($rs);
echo $row['cost']