On the other hand, I will try to show you how do I use PHP/Json to
query MySQL and parse it to Google API.
So, the PHP must echo a json string, for this example I will show you
how do I create a Table with users thru Google Api, must be the same
for any graph type.
PHP:
//Make a function
function ulist(){
... I first connect to the DB ...
$query = mysql_query("SELECT * FROM `security` ;"); //query for the
values
$values = array(); //build an empty array
// do a while loop to fetch all the data I want, here I read all the
rows of the db.
while ( $row = mysql_fetch_assoc($query) ){
if ( $row['attempt'] < 3 ){ $state = "Active" ; }else{ $state =
"Inactive" ; }
$row = array('c' => array(
array( 'v' => $row['username'] , 'f' =>
NULL ),
array( 'v' => $row['name'] , 'f' =>
NULL ),
array( 'v' => $row['email'] , 'f' =>
NULL ),
array( 'v' => $row['level'] , 'f' =>
NULL ),
array( 'v' => $row['lastlogin'] , 'f'
=> NULL ),
array( 'v' => $state , 'f' => NULL ),
array( 'v' => '<a
href="deluser.php?uid='.$row['id'].'&o=1"><img
src="pic/user-edit-icon.png" /></a>' , 'f' => NULL ),
array( 'v' => '<a
href="deluser.php?uid='.$row['id'].'&o=0"
onclick="return confirm(\'You are about to delete '.$row['name'].'
from the system !\');"><img src="pic/user-delete-icon.png" /></a>' ,
'f' => NULL )
));
array_push($values,$row); // I push all the arrays from the result to
the empty array created above
}
return $values ; // I return the whole array created.
} //close the function
// I make in the while loop a check to convert the numbers to strings
for a better output, I got my system set up so that a user can be
locked or not, this depends if he has more than 3 failed attempt on
login, just forget this, it is not important.
//Then, lets build the colums
$cols = array(
array( 'id' => '', 'label' => 'Username', 'type' =>
'string'),
array( 'id' => '', 'label' => 'Name', 'type' =>
'string'),
array( 'id' => '', 'label' => 'e-mail', 'type' =>
'string'),
array( 'id' => '', 'label' => 'Level', 'type' =>
'string'),
array( 'id' => '', 'label' => 'Last Login', 'type' =>
'string'),
array( 'id' => '', 'label' => 'State', 'type' =>
'string'),
array( 'id' => '', 'label' => 'Edit', 'type' =>
'string'),
array( 'id' => '', 'label' => 'Delete', 'type' =>
'string')
);
//This is simple! every value from the row above must have a "header".
This is easy if you understand it like a Table (practice with Google
Table)
$rows = ulist(); //the variable $rows contains all the rows built
above.
//and the final part is to echo out he result.
echo '{ "cols": '.json_encode($cols).', "rows":
'.json_encode($rows).'}';
The PHP will output the JSON string ready to be processed by Google
API.
Now for the JavaScript source code:
I make a var called jsonData like this:
var jsonData = $.ajax({
url: "FILEFROMABOVE.php", //file name from the above php
code !!!
dataType:"json",
async: false
}).responseText;
//this will query the PHP file and get the JSON string
var data = new google.visualization.DataTable(jsonData);
//from here everything is the same, options,
google.visualization.motionchart/table/linechart etc etc.
Last but not least you must have jquery included in the HTML head tag.
I downloaded it and make a folder called js in the root and included
it like this:
<script type="text/javascript" src="js/jquery-1.6.4.js"></script>
I hope this helps!
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-visualization-api?hl=en.