PDA

View Full Version : PHP & JSON & Pie Chart problem



fusionplus
15 Oct 2010, 1:20 PM
Hey,
I am new to JS so I do not know much about it. I have a problem with my pie chart as I am using php to pass a json to js which would use to data in pie chart. The problem is that I don't see the chart on the webpage :(.

Here's my php code:



<?php
include 'json.php';
$post = $_POST['task'];
if (mysql_connect("localhost", "root", "123456")) {

@mysql_select_db("data") or die ("CANNOT CONNECT TO DB");
$result = mysql_query("SELECT * FROM `json_store`");
$n = mysql_num_rows($result);

while ($obj = mysql_fetch_array($result)) {
$arr[] = $obj;
}
$jresult = json_encode($arr);
echo '({"total":'.$n. '","results":'.$jresult.'})';
}

mysql_close();
?>
This shoots out JSON like this:


({"total":1","results":[{"0":"1","id":"1","1":"Arsalan","category":"Arsalan","2":"150","value":"150"}]})My DB table has "id", "category" and "value" fields.

Here is my Javascript code:





/**
*
*/

Ext.chart.Chart.CHART_URL = 'ext/resources/charts.swf';

Ext.onReady(function(){

var myStore = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: 'test.php',
method: 'POST'
}),
baseParams:{task: "show"},

reader: new Ext.data.JsonReader(
{
root: 'results',
id: 'id',
totalProperty: 'total'
},
[
{name: 'category'},
{name: 'value'}
]
),
}
);
myStore.load();



new Ext.Panel({

width: 400,
height: 400,
title: 'Testing Pie charts',
renderTo: 'container',
collapsible: true,
items: {
store: myStore,
xtype: 'piechart',
dataField: 'value',
categoryField: 'category',
extraStyle:
{
legend:
{
display: 'bottom',
padding: 5,
font:
{
family: 'Tahoma',
size: 13
}
}
}
}
});
});
If I use sencha's default pie chart example, I see the pie chart. However, this code doesnt display the pie chart on the webpage. It does show the panel. I can see the panel but no pie chart.

Any help?

Thanks

winklerd
15 Oct 2010, 2:12 PM
At first glance, I think your JSON reader is going to fail because the text you're returning is not proper JSON (which should not have any parentheses, I believe). You are better off building the whole array and then returning the json rather than trying to build properly formatted text.


<?php
include 'json.php';
$post = $_POST['task'];
if (mysql_connect("localhost", "root", "123456")) {

@mysql_select_db("data") or die ("CANNOT CONNECT TO DB");
$result = mysql_query("SELECT * FROM `json_store`");
$n = mysql_num_rows($result);

while ($obj = mysql_fetch_array($result)) {
$arr[] = $obj;
}
echo json_encode(array('total'=>$n,'results'=>$arr));
}

mysql_close();
?>

I can't guarantee that's your only problem but it's a start.

fusionplus
15 Oct 2010, 8:29 PM
At first glance, I think your JSON reader is going to fail because the text you're returning is not proper JSON (which should not have any parentheses, I believe). You are better off building the whole array and then returning the json rather than trying to build properly formatted text.


<?php
include 'json.php';
$post = $_POST['task'];
if (mysql_connect("localhost", "root", "123456")) {

@mysql_select_db("data") or die ("CANNOT CONNECT TO DB");
$result = mysql_query("SELECT * FROM `json_store`");
$n = mysql_num_rows($result);

while ($obj = mysql_fetch_array($result)) {
$arr[] = $obj;
}
echo json_encode(array('total'=>$n,'results'=>$arr));
}

mysql_close();
?>

I can't guarantee that's your only problem but it's a start.



Oh my god it worked!!!!

Thanks alot :D

I'd kiss you if I wasn't straight hahaha

Awesome