Zend Certified Engineer

phpguru.org

Quality PHP, Javascript and C# code

Firefox 3.5

RGrid for PHP: Datagrid class for PHP & MySQL

Introduction

This is a PHP 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 RGrid code
    */
    require_once('RGrid.php');
    
    $params['hostname'] = 'localhost';
    $params['username'] = 'root';
    $params['password'] = '[PASSWORD]';
    $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 = RGrid::Create($params, $sql);
    
    $grid->Display();
?>

This will show a bare bones list of comments from this website. 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 all browsers: Internet Explorer, Firefox, Chrome, Safari and Opera
  • 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.


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

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.

Download

The latest version as an archive is here: RGrid.zip

Counter