T-XU.COM      
Home |
  Home>Computer Programming>CGI>
An Extensive Examination of the PHP:DataGrid Component: Part 1
By Dennis Pallett        [Hits: 1767]



An Extensive Examination of the PHP:DataGrid Component: Part 1

Introduction One of the most common tasks in PHPis retrieving data from a database table, and creating a HTMLtable to output that data. It's done in almost every project,and it's usually a really boring task, because the code isalways nearly the same, but not just same enough to be able tocopy it.

It often looks some like this (in pseudo-code):

Create database connection Get data from a table Outputtable header (<table) Loop through each records ... outputtr's and td's ... Output table footer (</table>)


It's a sad fact, but we've already written code like the abovehundreds of times. And for every project and script, you have todo it again, again and again.

But why not use a solution that can do it for us? That's exactlywhat PHP:DataGrid is.

What isPHP:DataGrid? PHP:DataGrid is the answer to the above problem. It'sbasically a PHP component, that's very similar to the ASP.NETDataGrid control. PHP:DataGrid will take care of all the boringtasks leaving you the easy and interesting parts. Very littlePHP code is actually necessary for PHP:DataGrid, and you canchange its looks and layout using simple HTML tags.

The only downside of PHP:DataGrid is that it's not free. Youhave to purchase it from TPGPHP Scripts, but it's $24.99 for a Developer license, whichgrants you permission to use it in all your personal projects,and I certainly believe that the advantages far outweigh thecost. Even only the time saved by PHP:DataGrid is already worththe cost for me. (editor's note: use coupon code phpitfor a 10% discount!).

Let's have an actual look at PHP:DataGrid. If you don't want topurchase the component yourself, then you can always have a lookat the demo's only.

TheBasics To create a new datagrid, we must use thephp:datagrid tag. This tells the PHP:DataGrid component that adatagrid must be shown. The only thing that we must set is thename of the datagrid. This is a required attribute, and cannotbe left out. A simple datagrid looks like this:

<php:datagridname="test"></php:datagrid>


That's the only thing necessary to display a datagrid. But we'reforgetting one thing - we haven't binded any data to thedatagrid yet. If you forget to do this, nothing will bedisplayed, except for an error.

BindingData Binding data to a datagrid is really easy, andrequires only one line of real PHP code. The PHP:DataGridcomponent automatically creates a variables called $_DATAGRID(not a superglobal,unfortunately). To bind data, you have to call the bind() methodon the $_DATAGRID variable, like so:
$_DATAGRID->bind('test', $data);
That's all! Thetest datagrid will now be shown, with the data contained in the$data variable. The $data variable must be an array that wasretrieved using mysql_fetch_array() and a loop (see the datagridexample below if you're unsure about this) or similar format. Inany case, it should look like this:

Array ( [0] => Array ( [id] => 1 [title] => Item 1[category] => 4 )

[1] => Array ( [id] => 2 [title] => Item 2 [category]=> 7 )

[2] => Array ( [id] => 3 [title] => Item 3 [category]=> 3 ) )


The above is a valid $data array. It won't accept any otherformat, and an error will be shown if you do bind a differentformat.

AnExample The below code is a working example of a simpledatagrid. It retrieves the 10 latest tutorials from thePHPit.net database, and shows it in a datagrid.
<?php

// Include PHP:DataGrid include ('/path/to/phpdatagrid.php');

// Connect to database $link = mysql_connect ('localhost', 'sa','[db-pass]');

// Select database mysql_select_db('phpit', $link);

// Do query and Get data $result = mysql_query ("SELECTtitle, description, author, datetimestamp, filename FROMtutorial ORDER BY datetimestamp DESC LIMIT 0, 10"); $data =array(); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {array_push ($data, $row); }

// Bind data (THIS IS IMPORTANT) $_DATAGRID->bind ('test',$data);

?> <html> <head> <title>PHP:DataGrid Demo1</title> </head>

<body> <h1>PHP:DataGrid Demo 1</h1><p>Demonstrating a simple PHP:DataGrid, and nothingmore.</p>

<php:datagrid name="test"></php:datagrid>

<br /> <ahref="http://www.phpit.net/article/datagrid-1/"><strong>« Return to thearticle</strong></a> </body></html>
[ View live demo ]

As you can see little code is used for the datagrid. Most of thecode is actually spent on connecting to the MySQL database, andgetting the data. If you use any kind of database class, thiswill be significantly easier.

If you have a look at the datagrid, you will notice that itlooks ugly, and pretty bad. That's because we haven't added anystyling at all. But that will have to wait until Part 2 of ourDataGrid series.

SummaryIn this part of our DataGrid series, we've looked at the basicsof the PHP:DataGrid component: what it is, and how to put it onour website. But it doesn't look pretty yet, and in the nextparts we'll be looking at creating a pretty datagrid, and talkabout more of its functions (e.g. templates, inline editing,sorting and more!).

Click here to view the PHP:DataGrid Product Page Don'tforget - use coupon code phpit for a 10% discount!
  Top Articles
*CGI Security Issues
*Clever Profit Growth Software
*Why Aren't You Using CGI
*How to Stop Digital Thieves wi
*Open Source Scripts
*5 CGI Scripts You Must Use to
*Quick Intro to PHP Development
*PHP and Cookies; a good mix!
*PHP On-The-Fly!
*this is a test
*Screen scraping your way into
*ASP, CGI and PHP Scripts and R
  Related Articles
*this is a test
*Open Source Scripts
*PHP:Form Series, Part 1: Valid
*ASP, CGI and PHP Scripts and R
*Design an Online Chat Room wit
*Mastering Regular Expressions
*Screen scraping your way into
*PHP and Cookies; a good mix!
*PHP On-The-Fly!
*Track your visitors, using PHP
*Autoresponders With PHP
*Password Protection and File I


Prev: Top Seven Ideas For Success   Next: this is a test



Home | Site Map | Bookmark this site | T-XU RSS
Copyright 2007 T-XU.com - All Rights Reserved Worldwide.