Zend Certified Engineer

phpguru.org

Free PHP, Javascript and C# code

Datagrid for PHP & MySQL

Synopsis

This is a datagrid class which can show the MySQL result set which you pass to it as a paged list. It can be completely styled using CSS (which looking at the examples, I'm sure you'll be grateful for). A quick example:

<?php    
    /**
    * Include the datagrid code
    */
    require_once('Datagrid.php');
    
    $params['hostname'] = 'localhost';
    $params['username'] = 'root';
    $params['password'] = 'ld29udm2jju8';
    $params['database'] = 'phpguru';

    $sql = "SELECT cm_id,
                   ne_title,
                   cm_author,
                   cm_datetime,
                   cm_status,
                   cm_neid
              FROM comments,
                   news
             WHERE cm_status = 'ACTIVE'
               AND cm_neid = ne_id
          ORDER BY cm_id DESC";

    /**
    * Create the datagrid with the connection parameters and SQL query
    * defined above. You MUST specify an ORDER BY
    * clause (with ASC/DESC direction indicator) - if not then ordering
    * will be disabled.
    */
    $grid = DataGrid::Create($params, $sql);
    
    $grid->Display();
?>

This will show a bare bones list of users and the corresponding hosts they can connect from. You will probably want to customise this quite a bit (appearance wise), which you can do with CSS, and with the various methods available to you (eg. SetDisplayNames()).

Features:

  • Order by any of the columns in both ascending and descending directions
  • Tailored to MySQL
  • Allows any HTML in the columns (eg. Pictures)
  • Easily change the look and feel by using CSS
  • Friendly column names
  • Alternative column or row color for easy reading
  • Customisable mouse over highlight
  • You can hide columns
  • Automatic record paging (defaults to 25 records per page)
  • Fully supports Internet Explorer, Netscape, Firefox, and Mozilla
  • Handles large database recordsets.

Examples

Below is an IFRAME showing one of the example scripts which you can get from the download area. Bear in mind that the example scripts look more complicated than would be typical as they show off everything (or try to) and the HTML and CSS are in the same file as the PHP magic.


Example 1 :: Example 2 :: Example 3 :: Example 4 :: Example 5 :: Example 6 :: Example 7 :: Example 8

Customisation

You can customise the "look and feel" using CSS. The grid uses a single table, which has the class datagrid. The headers are inside a <thead> tag, and the paging correspondingly in a <tfoot> tag. Using CSS you can then create corresponding CSS rules for them:

.datagrid thead th {
    background-color: #ddd;
    border-top: 2px solid #bbb;
    padding-left: 5px;
    text-align: left;
}

Naturally the body of the table uses a <tbody> along with <td> cells.

Structure

The tag structure of the datagrid is explained by this picture. Consider it "nice" structure, as it should be. The table has a class attribute of datagrid, the headers are in <th> cells, which themselves are in a <thead> tag. The body rows are in <td> cells, and a <tbody> tag. And the footer is in a <tfoot> tag.

Doing it this way allows finer grained control over the style. eg: .datagrid thead th is the CSS selector that you would use to change the style of the headers.

Development version

If you really want, you can look at the development version here. This is by no means gauranteed to work though.

Download

From the download area.
Last modified: 18:28 6th May 2008