An Extensive Examination of the PHP:DataGrid Component: Part 1
By Dennis Pallett
2005-09-14
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
// 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 ("SELECT title, description, author, datetimestamp, filename FROM tutorial 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 Demo 1</title> </head>
<body> <h1>PHP:DataGrid Demo 1</h1> <p>Demonstrating a simple PHP:DataGrid, and nothing more.</p>
<php:datagrid name="test"></php:datagrid>
<br /> <a href=http://www.developertutorials.com/tutorials/php/examining-php-datagrid-component-050914/page5.html;><strong>« Return to the article</strong></a> </body> </html> [ 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.
Tutorial Pages:
»
Introduction
»
What is PHP:DataGrid?
»
The Basics
»
Binding Data
» An Example
»
Summary
|

|