i am working with JSON to send data to and from my server , but normally i work with sending one data at a time , now i want to :
retrieve all the rows from a table (mysql database) –> php put it in a JSON array + callback —> javascript retrives it and display it out by looping through the data.
Here’s my javascript(jQuery):
$.getJSON(domain_path + 'generate.php?table=' + tbname + '&callback=?', function(data) {
});
As you can see this has table= the table name. which is for php to know which table to extract the data from.
But for the php part im not sure what to use to produce a JSONP array.
<?php
//connects to database
include 'connect.php';
$tbname = mysql_real_escape_string($_GET['table']);
$callback = $_GET['callback'];
//some mysql commands
//after mysql commands
//i would use this to output data but this is only for one line of data.
$output = array('error'=>'0');
$out_string = json_encode($output);
echo $callback.'('.$out_string.');';
?>
Mysql table structure:
Table name : users
name , link , email
How can i get all the rows from users table which contians their name , link and email and out it into a JSON array.
And how would i display it out using javascript(jquery)?
Is it using the for function in javascript
Print out the JSON array in PHP:
Catch and loop over result in jQuery:
First each loop goes over your rows/objects; second loop goes over your attributes/columns.