An Extensive Examination of the PHP:DataGrid Component: Part 1by: Dennis PallettIntroduction One of the most common tasks in PHP is retrieving data from a database table, and creating a HTML table to output that data. It's done in almost every project, and it's usually a really boring task, because the code is always nearly the same, but not just same enough to be able to copy it. What is PHP:DataGrid? PHP:DataGrid is the answer to the above problem. It's basically a PHP component, that's very similar to the ASP.NET DataGrid control. PHP:DataGrid will take care of all the boring tasks leaving you the easy and interesting parts. Very little PHP code is actually necessary for PHP:DataGrid, and you can change its looks and layout using simple HTML tags. The only downside of PHP:DataGrid is that it's not free. You have to purchase it from TPG PHP Scripts, but it's $24.99 for a Developer license, which grants you permission to use it in all your personal projects, and I certainly believe that the advantages far outweigh the cost. Even only the time saved by PHP:DataGrid is already worth the cost for me. (editor's note: use coupon code phpit for a 10% discount!). Let's have an actual look at PHP:DataGrid. If you don't want to purchase the component yourself, then you can always have a look at the demo's only. The Basics To create a new datagrid, we must use the php:datagrid tag. This tells the PHP:DataGrid component that a datagrid must be shown. The only thing that we must set is the name of the datagrid. This is a required attribute, and cannot be left out. A simple datagrid looks like this: <php:datagrid name="test"></php:datagrid> That's the only thing necessary to display a datagrid. But we're forgetting one thing - we haven't binded any data to the datagrid yet. If you forget to do this, nothing will be displayed, except for an error. Binding Data Binding data to a datagrid is really easy, and requires only one line of real PHP code. The PHP:DataGrid component automatically creates a variables called $_DATAGRID (not a superglobal, unfortunately). To bind data, you have to call the bind() method on the $_DATAGRID variable, like so: $_DATAGRID->bind('test', $data); That's all! The test datagrid will now be shown, with the data contained in the $data variable. The $data variable must be an array that was retrieved using mysql_fetch_array() and a loop (see the datagrid example below if you're unsure about this) or similar format. In any case, it should look like this: An Example The below code is a working example of a simple datagrid. It retrieves the 10 latest tutorials from the PHPit.net database, and shows it in a datagrid. <?php <html> <head> <title>PHP:DataGrid Demo 1</title> </head> [ View live demo ] Summary In this part of our DataGrid series, we've looked at the basics of the PHP:DataGrid component: what it is, and how to put it on our website. But it doesn't look pretty yet, and in the next parts we'll be looking at creating a pretty datagrid, and talk about more of its functions (e.g. templates, inline editing, sorting and more!). Click here to view the PHP:DataGrid Product Page Don't forget - use coupon code phpit for a 10% discount! © 2008 NetVisits, Inc. All rights reserved. |