【问题标题】:fusionCharts data from database in chartsfusionCharts 图表中数据库中的数据
【发布时间】:2016-03-01 11:40:09
【问题描述】:

我正在尝试将数据从我的数据库中获取到图表中。我使用的图表来自 fusioncharts。如果我打印 row_cnt,他们会给我正确的信息,但在表格中它给了我错误/没有信息。

$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 为我提供了非活动/已注销电子邮件的总数

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

我想显示 3 个不同的栏 1 需要是注册的电子邮件总数 栏号 2 需要是活动的,栏号 3 是非活动的。

【问题讨论】:

  • 在您的 while 循环中,您正在迭代 $result3。但是您每次都将$row_cnt 推入数组。这可能是问题吗?试着写"value" =&gt; $row['...something else...'], ...
  • @peter_the_oak 这是问题的一部分,但我想显示 3 个不同的栏,我刚刚编辑了我的问题以使其更清晰。

标签: php mysql mysqli charts fusioncharts


【解决方案1】:

我认为,您应该简单地将三个值 $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() 的语法,这有时更具可读性。

希望这会有所帮助:-)

【讨论】:

  • 感谢这对我帮助很大!
猜你喜欢
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
  • 2016-12-12
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多