![]() | |
| Submit your article Contact us | |
An Extensive Examination Of The Php:datagrid Component: Part 1Posted by Dennis Pallett on: 2005-08-14 15:22:48
Introduction 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. It often looks some like this (in pseudo-code): Create database connection It's a sad fact, but we've already written code like the above hundreds of times. And for every project and script, you have to do it again, again and again. But why not use a solution that can do it for us? That's exactly what PHP What is PHP PHP The only downside of PHP Let's have an actual look at PHP The Basics To create a new datagrid, we must use the php:datagrid tag. This tells the PHP <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->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:Array The above is a valid $data array. It won't accept any other format, and an error will be shown if you do bind a different format. 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[ View live demo ] As you can see little code is used for the datagrid. Most of the code is actually spent on connecting to the MySQL database, and getting the data. If you use any kind of database class, this will be significantly easier. If you have a look at the datagrid, you will notice that it looks ugly, and pretty bad. That's because we haven't added any styling at all. But that will have to wait until Part 2 of our DataGrid series. Summary In this part of our DataGrid series, we've looked at the basics of the PHP Click here to view the PHP Don't forget - use coupon code phpit for a 10% discount!
Dennis Pallett is a young web developer, currently still studying. He owns several websites, including PHPit (http://www.phpit.net) and WebDev-Articles (http://www.webdev-articles.com). If you would like to reach him, you can contact him at writing@nocertainty.com
|
|