Reputation: 568
I'm trying to get data from my database into a chart. The charts i'm using are from fusioncharts. If i print the row_cnt's they give me the correct information but in the table it gives me the wrong/no information.
$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,
)
);
}
in the chart it only gives me the total registered emails (row_cnt) and when i want to try to add the other 2 in the chart its not giving me any information. The bar with the total registered emails is also display twice. Someone know what im doing wrong or how to do this?
I want to display 3 different bars 1 needs to be the total registered emails bar number 2 needs to be the active ones and bar number 3 the inactive ones.
Upvotes: 1
Views: 655
Reputation: 3710
I think, you should simply push the three values $row_cnt
, $row_cnt2
and $row_cht3
one after another.
Look at this:
// 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
));
But maybe you need to loop, as the number of processed values could change and you need to be flexible. In that case I recommend to collect the values in a separate array first and then loop the separate array. Like this:
...
// 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;
}
Either way, iterating over $result
as in your question does not seem to be the right way.
Also please note the syntax with []
instead of array_push()
, which sometimes is more readable.
Hope this helps :-)
Upvotes: 1