//JavaScript
var data = [
['Heavy Industry', 12],['Retail', 9], ['Light Industry', 14],
['Out of home', 16],['Commuting', 7], ['Orientation', 9]
];
to achieve above data format(example : 1) programmers use a loops. and iterate a record set and generate jqplot data in the form of string type. (as i mentioned in example 2)
example : 2
//PHP
$string.='["'.$row['name'].'",'.$row['id'].'],';
This is not good practice.So i have written a class to represent a mysql data in jqplot. this class helps to programmers to achive (example : 1) format easily.
Here is the code
//class jqplot
class jqplot {
function __construct() {
//constructor
}
function create_jqplot_data_array() {
//call db connection function
//ex : $db->connect();
//execute the query fetch table data
//$sql = "select name,avg from 'table_name'"
//create an array
$container = array();
// loop a recordset and pass the field value to array ($container)
while ($record = mysql_fetch_object($result)) {
//passing value to '$container' array.
//intval() : Returns integer value.
//for more details about intval() http://php.net/manual/en/function.intval.php
$container[] = array($record->name, intval($record->avg));
}
//json_encode() : Returns the JSON representation of a value
//for more details about json_encode() http://php.net/manual/en/function.json-encode.php
$json_data = json_encode($container);
// returns json data.
return $json_data;
}
}
//create a object using jqplot class
$obj_jqplot = new jqplot();
//call a function : create_jqplot_data_array()
//it returns the encoded json data from the function
$json_data = $obj_jqplot->create_jqplot_data_array();
//display the json encoded data.
echo $json_data;
?>
Hope this helps.







0 comments:
Post a Comment