Ext JS - Learning Center

Tutorial:Creating JSON Data in PHP

From Learn About the Ext JavaScript Library

Revision as of 18:34, 23 July 2007 by Jonwhitcraft-3 (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Summary: A simple way to create JSON encoded data using PHP.
Author: Shea Frederick
Published: March 13, 2007
Ext Version: 1.1
Languages: en.png English

So you need to encode JSON data for use with Ext? If you are lucky enough to be running PHP 5.2.0 or greater, then you have the optimal environment for encoding JSON data. But all is not lost if you don't have this setup, there are plenty of libraries you can use to take care of this task.

For our examples we are using a MySQL database as the data source.

Contents

PHP 5.2.0 or higher

As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default, which makes life for you extremely easy. This is also a highly optimized implementation of JSON for PHP, so if you have this available to you, then use it.

Eleven lines of code and you have data ready to be used by many of the Ext objects.

$link = mysql_pconnect("server", "user", "password") or die("Could not connect");
mysql_select_db("database") or die("Could not select database");
 
$arr = array();
$rs = mysql_query("SELECT id, url, text FROM sample");
 
while($obj = mysql_fetch_object($rs)) {
	$arr[] = $obj;
}
 
echo '{"sample":'.json_encode($arr).'}';

Less than PHP 5.2.0 and no root access (*NIX)

The PEAR JSON package will be the way to go if you cant use PHP 5.2.0 or greater and you do not have root access to install php extensions. There are other libraries available, but this librarys inclusion in PEAR makes it the clear choice for reliability.

http://pear.php.net/pepr/pepr-proposal-show.php?id=198

Just two more lines of code to include the library, create an instance of the JSON service, and change the call to encode (line 14).

include_once('JSON.php');
$json = new Services_JSON();
 
$link = mysql_pconnect("server", "user", "password") or die("Could not connect");
mysql_select_db("database") or die("Could not select database");
 
$arr = array();
$rs = mysql_query("SELECT id, url, text FROM sample");
 
while($obj = mysql_fetch_object($rs)) {
	$arr[] = $obj;
}
 
Echo '{"sample":'.$json->encode($arr).'}';

Less than PHP 5.2.0 with root access (*NIX)

For those of you that need to keep your current version of PHP but want the speed gained by using the PHP JSON extension can download and install it yourself, granted you have root access to the box your on. The installation is very simple, just follow the directions on the web site and you can have this extension installed and working in 10 minutes.

http://www.aurore.net/projects/php-json/

This way you can use the native PHP JSON functions, so the example code for PHP 5.2.0 or higher will work.

NOTE: FreeBSD users can install the devel/pecl-json port.

JSON extension for Windows

If you are on windows, installing the JSON extension is even easier. Download the precompiled extension for your version of PHP from http://pecl4win.php.net/ext.php/php_json.dll . Place it in your PHP extension directory, and add extension=php_json.dll in your php.ini file. Restart your server and you're done!