I am trying to convert an array obtained using the code below using non obsolete methods with php pdo:
$stm = $conn->prepare("SELECT * FROM mysqltable");
$stm->execute();
$results = $stm->fetchAll(PDO::FETCH_ASSOC);
print_r($results);
to the following format required to use fusion graphics cards
[
{
label: "CJ Anderson",
value: "25"
},
{
label: "Imran Tahir",
value: "25"
},
...
...
]
The source array is as follows:
Array (
[0] => Array (
[Id] => 6
[Number] => 1234567890
[Visits] => 1
[Name] => John
)
[1] => Array (
[Id] => 7
[Number] => 1236549871
[Visits] => 9
[Name] => Jerry
)
[2] => Array (
[Id] => 8
[Number] => 2147483647
[Visits] => 3
[Name] => Jane
)
)
Any help would be appreciated, thanks.
EDIT: As I said below. I have a complete php file that works if you put data manually. I can't get it to work, though, when I put $ jsonEncodedData, though. Thoughts?
<html>
<head>
<title>FusionCharts XT - Column 2D Chart - Data from a database</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<!-- You need to include the following JS file to render the chart.
When you make your own charts, make sure that the path to this JS file is correct.
Else, you will get JavaScript errors. -->
<script src="fusioncharts/js/fusioncharts.js"></script>
</head>
<body>
<?php
try {
$mysql_host = 'host';
$mysql_database = 'table';
$mysql_username = 'user';
$mysql_password = 'pass';
$conn = new PDO("mysql:host=$mysql_host; dbname=$mysql_database", $mysql_username, $mysql_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET CHARACTER SET utf8");
}
catch(PDOException $e) {
echo $e->getMessage();
}
$stm = $conn->prepare("SELECT Name, Visits FROM mysqltable");
$stm->execute();
$results = $stm->fetchAll(PDO::FETCH_ASSOC);
include("fusioncharts.php");
$jsnarray = array();
foreach($results as $k => $v){
$jsnarray[] = array('label' => $results[$k]['Name'], 'value' => $results[$k]['Visits']);
};
$jsonEncodedData=json_encode($jsnarray);
new FusionCharts("type of chart",
"unique chart id",
"width of chart",
"height of chart",
"div id to render the chart",
"type of data",
"actual data");
$columnChart = new FusionCharts(
"column2d",
"ex1" ,
"600",
"400",
"chart-1",
"json",
'{
"chart":
{
"caption":"Harry\ SuperMart",
"subCaption":"Top 5 stores in last month by revenue",
"numberPrefix":"$",
"theme":"ocean"
},
"data": //$jsonEncodedData}'); <---I tried to insert this after "data":but no results unlike if you put raw data**
[
{
"label":"Bakersfield Central",
"value":"880000"
},
{
"label":"Garden Groove harbour",
"value":"730000"
},
{
"label":"Los Angeles Topanga",
"value":"590000"
},
{
"label":"Compton-Rancho Dom",
"value":"520000"
},
{
"label":"Daly City Serramonte",
"value":"330000"
}
]
}');
// Render the chart
$columnChart->render();
?>
<div id="chart-1"><!-- Fusion Charts will render here--></div>
</body>
</html>
=============== Edit 12/28/15 ===========
Tried the following code without any results. Question: I should not end with "}" as they require that:
$columnChart = new FusionCharts(
"column2d",
"ex1" ,
"600",
"400",
"chart-1",
"json",
'{
"chart":
{
"caption":"Harry\ SuperMart",
"subCaption":"Top 5 stores in last month by revenue",
"numberPrefix":"$",
"theme":"ocean"
},
"data": ' . $jsonEncodedData);
print_r($columnChart);
$columnChart->render();
?>
<div id="chart-1"><!-- Fusion Charts will render here--></div>
</body>
</html>
"" "" ( ).
:
FusionCharts ([constructorOptions: FusionCharts: private] = > Array ( > [type] = > column2d [id] = > ex1 [width] = > 600 [height] = > 400 [renderAt] = β chart- 1 [dataFormat] = > json [dataSource] = > { "chart": { > "caption": "Harry SuperMart", "subCaption": " 5 > ", "numberPrefix": "$", "theme": "ocean" }, "data": > [{ "label": "John", "value": "125" }, { "label": "Jerry", "value": "125" }, { "label": "Jane", "value": "125" }]) [constructorTemplate: FusionCharts: private] = β [renderTemplate: FusionCharts: private] = > )
( ):
FusionCharts ([constructorOptions: FusionCharts: private] = > Array ( > [type] = > column2d [id] = > ex1 [width] = > 600 [height] = > 400 [renderAt] = β chart- 1 [dataFormat] = > json [dataSource] = > { "chart": { > "caption": "Harry SuperMart", "subCaption": " 5 > ", "numberPrefix": "$", "theme": "ocean" }, "data": [{ > "label": "Bakersfield Central", "value": "880000" }, { "label": "Garden Groove > ", "": "730000" }, { "label": "Los Angeles Topanga", > "value": "590000" }, { "label": "Compton-Rancho Dom", "value": "520000" }, { > "label": "Daly City Serramonte", "value": "330000" }]}) > [constructorTemplate: FusionCharts: private] = β [renderTemplate: FusionCharts: private] = > )
, "" .