Using PHP to return JSON data to Jquery Ajax

JSON is syntax for storing and exchanging text information. Much like XML. JSON is smaller than XML, and faster and easier to parse. I have decided to try to use PHP to return data in JSON format to Jquery Ajax . After doing some research , i found two tutorials which i am going to try when i am free.

1) Tutorial 1
2) Tutorial 2

I would update you this post again when i have tried it out

Finally after what seems like months , i have returned to this question
4/9/13

I was thinking of how to convert a string to a JSON object in PHP , it is done by using the json_decode method . Please refer here .

To convert array to JSON object , it is done by the json_encode method . Please refer here

I then thought how to iterate over the json object once i got it , Please refer here

Now i have a problem , everytime i stmt->fetch , how do i store it inside it in an associative array .

5/9/13

Qn : everytime i stmt->fetch , how do i store it inside it in an associative array .
Ans: retrning an associative array from mysqli statement result
while ($stmt->fetch())
{
$data[]=array(“code”=>$code,”title”=>$title,”lecturer”=>$lecturer,”description”=>$description);

}

I would have to use json_encode to convert the associative array into an JSON object

echo json_encode($data);

Once an JSON object was returned i knew i had to iterate over each item in the JSON object using .each as shown by my previous day research but the SO ans didnt really show how to iterate over a associative array so i went to google for an example and found jquery loop on Json data using $.each which really taught me everything i needed to know

The last part was just .append the html and .empty() the table body everytime a user selects a new category. During the .append portion , i made a really stupid mistake with my string concatenation where i forgot to the + sign and had syntax error . I even asked the qn on SO and deserving got downvoted :(

I have a new problem now , if there are too many words for the column , the data wouldnt render at all and give a null instead . I wonder why????

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s