图表中来自数据库的 fusionCharts 数据

fusionCharts data from database in charts

我正在尝试将数据库中的数据放入图表中。我使用的图表来自 fusioncharts。如果我打印 row_cnt,它们会给我正确的信息,但在 table 中,它会给我 wrong/no 信息。

$dbhandle = new mysqli($hostdb, $userdb, $passdb, $namedb);

if ($dbhandle->connect_error) {
exit("There was an error with your connection: ".$dbhandle->connect_error);
}
?>
<html>
   <head>
    <title>FusionCharts XT - Column 2D Chart - Data from a database</title>
    <script src="includes/fusioncharts.js"></script>
    <script src="includes/fusioncharts.charts.js"></script>
  </head>
 <body>
<?php
$result = $dbhandle->query("SELECT * FROM email");
$result2 = $dbhandle->query("SELECT * FROM email WHERE status='1'");
$result3 = $dbhandle->query("SELECT * FROM email WHERE status='0'");

$row_cnt = $result->num_rows;
$row_cnt2 = $result2->num_rows;
$row_cnt3 = $result3->num_rows;

// If the query returns a valid response, prepare the JSON strin

if ($result) {

// The `$arrData` array holds the chart attributes and data

$arrData = array(
  "chart" => array
  (
    "caption" => "Aantal geregistreerde emails",
    "paletteColors" => "#0075c2",
    "bgColor" => "#ffffff",
    "borderAlpha"=> "20",
    "canvasBorderAlpha"=> "0",
    "usePlotGradientColor"=> "0",
    "plotBorderAlpha"=> "10",
    "showXAxisLine"=> "1",
    "xAxisLineColor" => "#999999",
    "showValues" => "0",
    "divlineColor" => "#999999",
    "divLineIsDashed" => "1",
    "showAlternateHGridColor" => "0"
  )
);

$arrData["data"] = array();

// Push the data into the array

while($row = mysqli_fetch_array($result)) {
array_push($arrData["data"], array(
  "label" => 'active emails',
  "value" => $row_cnt,
  )
);
}
  1. row_cnt 给我电子邮件总数
  2. row_cnt2 给出了活动电子邮件总数
  3. row_cnt3 给我总共 inactive/signedoff 封电子邮件

在图表中它只给我注册的电子邮件总数(row_cnt),当我想尝试在图表中添加其他 2 个时它没有给我任何信息。注册电子邮件总数的栏也显示两次。有人知道我做错了什么或如何做到这一点?

我想显示 3 个不同的栏,第 1 栏是注册电子邮件总数,第 2 栏是活跃邮件,第 3 栏是不活跃邮件。

我认为,你应该简单地将$row_cnt$row_cnt2$row_cht3这三个值依次压入。

看看这个:

// The `$arrData` array holds the chart attributes and data

// First, add the chart array that describes the chart itself.

$arrData = array(
  "chart" => array
  (
    "caption" => "Aantal geregistreerde emails",
    ...
    ...
    "showAlternateHGridColor" => "0"
  )
);

// Second, we add the values displayed by the bars.
$arrData["data"] = array();

// In this code example, we add the values each by each as they are available in three single variables.

// 1. Total:
array_push($arrData["data"], array(
  "label" => 'total emails',
  "value" => $row_cnt
));

// 2. Active:
array_push($arrData["data"], array(
  "label" => 'active emails',
  "value" => $row_cnt2
));

// 3. Active:
array_push($arrData["data"], array(
  "label" => 'inactive emails',
  "value" => $row_cnt3
));

但您可能需要循环,因为处理值的数量可能会发生变化,您需要保持灵活性。在那种情况下,我建议先将值收集在一个单独的数组中,然后循环单独的数组。像这样:

...

// Second, we add the values displayed by the bars.
$arrData["data"] = array();

// In this code example, we for an array first and are able to loop over it.
$myBarArray = array();

// 1. Total:
$myBarArray[] = array(
  "label" => 'total emails',
  "value" => $row_cnt
);
// 2. Active:
$myBarArray[] = array(
  "label" => 'active emails',
  "value" => $row_cnt2
);
// 3. Active:
$myBarArray[] = array(
  "label" => 'inactive emails',
  "value" => $row_cnt3
);

// Now we can loop this array, or pass it to a further function or whatever:
foreach ($myBarArray as $bar) {
    $arrData["data"][] = $bar;
}

无论哪种方式,按照您的问题迭代 $result 似乎都不是正确的方法。

另请注意使用 [] 而不是 array_push() 的语法,这有时更具可读性。

希望这对您有所帮助:-)