Advertisment

A DataGrid for PHP

author-image
PCQ Bureau
New Update

In our article Call it PHP.NET (December 2004, page 98), we talked about a project named Prado which brings ASP.NET functionality-server-side components, events, validators-to PHP. Prado was the award-winning project in a coding contest held by Zend, the company behind the PHP engine. Henceforth, Prado is under constant development and the package is now hosted at SourceForge-the most popular portal for open-source projects. With Prado, now PHP programmers can incorporate a fully functional DataGrid in their pages. A DataGrid is a tabular view of data, retrieved usually from a database. What makes it exciting is, with a few lines of code, you can display and sort the database's data, update it, set up paging and select rows from the tabular display. DataGrid became popular with ASP and ASP.NET technologies from Microsoft. Let's see a glimpse of Prado's DataGrid. We assume that you know the basics of PHP and have worked with

ASP.NET.

Advertisment

Installation



We will install and set up Prado on PCQLinux 2005 . PCQLinux 2005 ships with PHP version 4 while Prado requires version 5. Download the latest PHP 5 package (5.0.3 as of this writing) from

www.php.net. Extract the archive, and compile and install. 

Direct Hit!
Applies to:

ASP.NET and PHP developers
USP: With a few lines of code, display and sort database's data, update it and select rows from the tabular display
Links:

www.php.nethttp://www.xisc.com 

./configure --prefix=/usr --with-mysql --with-zlib -with-apxs2



make


make install

Advertisment

Fire up a text editor and type in the following.

AddType application/x-httpd-php .php



DirectoryIndex index.php

Save the file as php.conf in /etc/httpd/conf.d. In case you have installed PHP that comes with PCQLinux 2005, you will already have a file named php.conf in this directory. In this case, open the file and comment the following line by prefixing a #.

Advertisment

LoadModule php4_module modules/libphp4.so

Restart Apache Web server.

/etc/rc.d/init.d/httpd restart

Advertisment

Now download the zipped package of Prado from http://prdownloads.sourceforge.net/prado/prado-2.0rc.zip?

download and unzip it. This will produce a directory named prado. Create a directory named 'datagriddemo' under /var/www/html.

Copy the directory named 'framework' found under the prado directory to /var/www/html/datagridtest.

Code a DataGrid



To write a Web application using Prado, you have to write a PHP class for each Web page in your application. For example, for a Web page named datagrid, you need to write a file named datagrid.php containing a class definition as:

class datagrid extends TPage

Advertisment

The actual content (HTML, datagrid component) for the page will be in a file, datagrid.tpl. This class must be specified in an application.spec file, which can be thought of as similar to a web.config file for ASP.NET. Following is the application.spec file that we will use.





























Save the above content in a file named application.spec in the datagriddemo directory. 

Advertisment

The DataGrid page



We start with a page containing a minimalistic DataGrid as shown below.

























Note that the ID of the DataGrid is grid. We will use this ID subsequently to populate the

DataGrid.

Advertisment

Bind data to the grid



We will retrieve data from a MySQL database and bind the data to the datagrid. To set up MySQL on PCQlinux 2005, refer to the our article JDBC Drivers, (March 2005, page 76). The code to bind data with the DataGrid will go into the datagrid.php file. 



class datagrid extends TPage



{


function onLoad($param)


{


if(!$this->IsPostBack)


{


$adodb = new TAdodb;


$adodb->DataSourceName


="mysql://: @localhost/";


$adodb->SetFetchMode(TAdodb::FETCH_ASSOCIATIVE);


$rs = $adodb->Execute('select * from

');



$this->grid->setDataSource($rs);


$this->grid->dataBind();


}


}


}


?>














Substitute , , and

with MySQL's user (say root, user's password, MySQL database and its table that you want to query, respectively). Save the above file as datagrid.php in the directory

datagriddemo. 

See it working



Now write an index.php file with the following code.



require_once(dirname(__FILE__).'/framework/prado.php';



pradoGetApplication('application.spec')->run();


?>

Save this file in datagriddemo. Load the following URL http://127.0.0.1/datagriddemo/index.php

You should see an HTML table with columns corresponding to the columns in the database table. You can make the columns sortable by adding the attribute AllowSorting to the Datagrid tag in datagrid.tpl as:

You can also do paging, row selection, data updation and beautifying the rows. Refer to the DataGrid example in the prado/examples/datagrid and /prado/ docs for the API documentation.

Shekhar Govindarajan



IT4Enterprise

Advertisment