gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:36:01 25 4
gpt4 key购买 nike

我正在尝试将数据库中的数据放入图表中。我使用的图表来自 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 是非事件的。

最佳答案

我认为,您应该简单地将三个值 $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
));

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

...

// 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() 的语法,这有时更具可读性。

希望这有帮助:-)

关于php - 图表中来自数据库的 fusionCharts 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35721923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com