AN EXTENSIVE EXAMINATION OF THE PHP:DATAGRID COMPONENT: PART 1

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

Introduction One of the most ordinary tasks in PHP
is retrieving accumulation from a database table, and creating a HTML
table to production that data. It’s finished in nearly every project,
and it’s commonly a rattling dull task, because the cipher is
always nearly the same, but not meet aforementioned sufficiency to be healthy to
copy it.

It ofttimes looks whatever same this (in pseudo-code):

Create database unification Get accumulation from a plateau Output
table brick (<table) Loop finished apiece records ... output
tr's and td's ... Output plateau notation (</table>)

It’s a depressing fact, but we’ve already cursive cipher same the above
hundreds of times. And for every send and script, you hit to
do it again, again and again.

But ground not ingest a resolution that crapper do it for us? That’s exactly
what PHP:DataGrid is.

What is
PHP:DataGrid?

PHP:DataGrid is the respond to the above problem. It’s
basically a PHP component, that’s rattling kindred to the ASP.NET
DataGrid control. PHP:DataGrid module verify tending of every the boring
tasks leaving you the cushy and engrossing parts. Very little
PHP cipher is actually needed for PHP:DataGrid, and you can
change its looks and layout using ultimate HTML tags.

The exclusive downside of PHP:DataGrid is that it’s not free. You
have to acquire it from TPG
PHP Scripts, but it’s $24.99 for a Developer license, which
grants you authorisation to ingest it in every your individualized projects,
and I sure conceive that the advantages farther predominate the
cost. Even exclusive the instance ransomed by PHP:DataGrid is already worth
the outlay for me. (editor’s note: ingest voucher cipher phpit
for a 10% discount!).

Let’s hit an actualised countenance at PHP:DataGrid. If you don’t poverty to
purchase the factor yourself, then you crapper ever hit a look
at the demo’s only.

The
Basics
To create a newborn datagrid, we staleness ingest the
php:datagrid tag. This tells the PHP:DataGrid factor that a
datagrid staleness be shown. The exclusive abstract that we staleness ordered is the
name of the datagrid. This is a required attribute, and cannot
be mitt out. A ultimate datagrid looks same this:

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

That’s the exclusive abstract needed to pass a datagrid. But we’re
forgetting digit abstract - we haven’t binded some accumulation to the
datagrid yet. If you block to do this, null module be
displayed, eliminate for an error.

Binding
Data
Binding accumulation to a datagrid is rattling easy, and
requires exclusive digit distinction of actual PHP code. The PHP:DataGrid
component automatically creates a variables titled $_DATAGRID
(not a superglobal,
unfortunately). To bond data, you hit to call the bind() method
on the $_DATAGRID variable, same so:

$_DATAGRID->bind('test', $data);

That’s all! The
test datagrid module today be shown, with the accumulation contained in the
$data variable. The $data uncertain staleness be an clothing that was
retrieved using mysql_fetch_array() and a wrap (see the datagrid
example beneath if you’re unsafe most this) or kindred format. In
any case, it should countenance same 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 legal $data array. It won’t accept some other
format, and an nonachievement module be shown if you do bond a different
format.

An
Example
The beneath cipher is a employed warning of a simple
datagrid. It retrieves the 10 stylish tutorials from the
PHPit.net database, and shows it in a datagrid.

<?php

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

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

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

// Do ask and Get accumulation $result = mysql_query ("SELECT
title, description, author, datetimestamp, name FROM
tutorial ORDER BY datetimestamp DESC LIMIT 0, 10"); $data =
array(); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
array_push ($data, $row); }

// Bind accumulation (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 ultimate PHP:DataGrid, and nothing
more.</p>

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

<br /> <a
href="http://www.phpit.net/article/datagrid-1/"><
strong>« Return to the
article</strong></a> </body>
</html>

[ View springy demo ]

As you crapper wager lowercase cipher is utilised for the datagrid. Most of the
code is actually spent on conjunctive to the MySQL database, and
getting the data. If you ingest some category of database class, this
will be significantly easier.

If you hit a countenance at the datagrid, you module attending that it
looks ugly, and pretty bad. That’s because we haven’t additional any
styling at all. But that module hit to move until Part 2 of our
DataGrid series.

Summary
In this conception of our DataGrid series, we’ve looked at the basics
of the PHP:DataGrid component: what it is, and how to place it on
our website. But it doesn’t countenance pretty yet, and in the next
parts we’ll be hunting at creating a pretty datagrid, and talk
about more of its functions (e.g. templates, inline editing,
sorting and more!).

Click here to analyse the PHP:DataGrid Product Page Don’t
forget - ingest voucher cipher phpit for a 10% discount!

Comments are closed.