by
faheem on July 27th, 2009
I have already written about json in one, actually some of my articles. Cool functionality can be achieved using json. Thanks to http://www.json.org, http://www.zend.com and http://www.dojotoolkit.org for providing easier solutions.
I’m not gonna talk what these three are all about, but instead share a little secret about these.
Json creation in dojo is as simple as this.
var arr = new Array();
arr[0] = "apple";
arr[1] = "ball";
arr[2] = "cat";
dojo.toJson(arr);
Using Zend to convert array into json took only one line of code.
Zend_Json::encode($arr);
And decoding- converting json to array, is as simple as this
Zend_Json::decode(json_string);
by
faheem on July 13th, 2009
While working with AJAX, you will need to get data after sending request to the server. You can use three ways to get the data form the server.
1. In xml Format
2. HTML Format
3. Json Format
Though xml has several advantages, however processing data at the client will put extra burden.
HTML, can easily be handle, is however more resource or you can say bandwidth extensive.
So JSON fall in the middle of both.
PHP provide a very easy way to create JSON response. Once you get data at client side you will need much later code to process response than xml document.
Let’s first look at the server side.
For example we have the following array.
$data = array(
'items'=>array(
'firstname'=>'faheem',
'lastname'=>'abbas',
'address'=>'pakistan'
)
);
To create JSON response, simply write
echo json_encode($data);
This will give the following result
{"items":{"firstname":"faheem","lastname":"abbas","address":"pakistan"}}
Now at the client side to send AJAX request, you will need to create the following code.
Keep in mind that we are using prototype for making AJAX request.
That’s it.
by
faheem on July 12th, 2009
Well, while working with ajax, it better to have an xml as response. Xml, however need more coding and parsing on client side than json.
Zend framework provides very easy way to generate json response.
In this article I’m going to discuss how to create Ajax request using prototype and how to use Zend_Json to generate response.
For generating Ajax request, you will need to write
If response is the form of this
[
{"firstName":"Faheem","lastName":"Abbas","username":"fahim","email":"faheem.abbas@example.com"},
{"firstName":"Shazil","lastName":"Hassan","username":"shazil","email":"shazil.hassan@yahoo.com"}
]
Then to acess the values of the first row, write
var firstName = results[0].firstName;
var lastName = results[0].lastName;
and the values of the second row can be accessed as
var firstName = results[1]. firstName;
var lastName = results[1].lastName;
To create Json response, you will need to write the following in your controller action
$users = new Users();
$username = $this->_getParam('username');
$select = $users->select();
$data = $users->fetchAll($select);
$results = $data->toArray();
echo Zend_Json::encode($results);
Keep in mind that “users” is our model, having the following code.
class Users extends Zend_Db_Table
{
protected $_name = "users";
}
That’s it. cheers.