<?php
    
/**
    * This an example script that uses the datagrid and shows it off
    */
    
    /**
    * Include the datagrid code
    */
    
require_once('Datagrid.php');

    
/**
    * Make sure errors are turned on
    */
    
ini_set('display_errors', 1);
    
error_reporting(E_ALL);

    
/**
    * Some sample data. The result from mysql_query() is what you pass to the datagrid
    */
    
$connection = mysql_connect('localhost', 'root', '') OR die('Failed to connect: ' . mysql_error());
    
$resultset = mysql_query("SELECT cm_id,
                                     cm_author,
                                     DATE_FORMAT(cm_datetime, '%H:%i %D %M %Y') AS `cm_datetime`,
                                     cm_status
                                FROM phpguru.comments
                               WHERE cm_status = 'ACTIVE'
                            ORDER BY cm_id DESC"
, $connection);

    
/**
    * Create the datagrid. First argument is the MySQL connection,
    * and the second is the result set (from mysql_query())
    */
    
$grid = new Datagrid($connection, $resultset);

    
/**
    * Turn the header off/on
    */
    
$grid->showHeader = true;

    
/**
    * Sets nice(r) display names instead of the raw column names
    */
    
$grid->SetDisplayNames(array('cm_id'       => 'ID',
                                 
'cm_author'   => 'Author',
                                 
'cm_email'    => 'Email',
                                 
'cm_datetime' => 'Date & time',
                                 
'cm_status'   => 'Status'));

    
/**
    * This hides a column from the result set. It would of course be easier and
    * faster just to not select it, but this isn't always possible.
    */
    #$grid->HideColumn('cm_live');
                                 
    /**
    * This simply sets the specified columns not to be passed through htmlspecialchars()
    * Generally any column that shows HTML
    */
    
$grid->NoSpecialChars('actions', 'ac_id');
    
    
/**
    * A callback function that gets called for each and every row.
    * The callback function should take a single argument by reference,
    * which is an associative array of the row data. It's by reference
    * so that any changes you make will not be lost.
    */
    
$grid->rowcallback = 'RowCallback';
    function
RowCallback(&$row)
    {
        
// Add the actions column
        
$row['actions'] = sprintf('<a href="example.php?ac_id=%d" onmouseover="window.status = \'Edit this row: %1$d\'; return 0" onmouseout="window.status = \'\'" onclick="alert(\'This link doesn\\\'t actually do anything\'); return false">Edit</a>', @$row['ac_id']);
        
// " - Closes off the quotes
    
}
    
    
/**
    * Here just for show. This function sets the number of rows to set per page.
    * The default is 20.
    */
    
$grid->SetPerPage(25);
    
    
/**
    * Again, this is purely here for show. It doesn't do anything, but simply shows
    * how you can use this function. If you change the perPage after you call
    * this function, then obviously the page count will change.
    */
    
echo '<p>Number of pages: ' . $grid->GetPageCount() . '<br />';
    
    
/**
    * Again, here just for show. Gets the number of rows in the grid.
    */
    
echo 'Number of rows: ' . $grid->GetRowCount() . '</p>';
    
    
/**
    * The HTML. The appearance can be customised using CSS
    */
?>
<html>
<head>
    <title>Datagrid example</title>
    
    <style type="text/css">
    <!--
        table.datagrid {
            font-family: Arial;
        }

        /**
        * Format the table headers
        */
        .datagrid thead th {
            color: black;
            border-top: solid 3px #aaa;
            background-color: #ddd;
            font-weight: normal;
            font-size: 10pt;
            font-family: Verdana;
            text-align: left;
            padding-left: 5px;
        }
        
        /**
        * This is here to illustrate how to format the alternative rows
        */
        .datagrid tbody td.altrow {
            background-color: #fff;
        }
        
        /**
        * Format the alternative columns with a light grey background
        */
        .datagrid tbody td.col_0,
        .datagrid tbody td.col_2
        {
            background-color: #ada;
        }
        
        /**
        * Format the actions column
        */
        .datagrid tbody td.col_4 a {
            text-decoration: none;
        }
        
        .datagrid tbody td.col_3 {
            background-color: white;
        }

        /**
        * Format the table headers
        */
        .datagrid tbody td {
            padding-left: 5px;
        }

        /**
        * Format the mouseover
        */
        .datagrid tbody td.mouseover {
            background-color: #FFB7B7;
        }
    // -->
    </style>
</head>
<body>
    This is the datagrid. As you can see my choice of colours is not the best... They can of course be customised, using CSS.
    By studying this example.php file (the .phps anyhoo), you'll see how and what selectors are available.

    <p />

    <?$grid->Display()?>
    
    <?if(preg_match('/^\/datagrid/', $_SERVER['REQUEST_URI'])):?>
        <pre><?print_r($grid)?></pre>
    <?endif?>
</body>
</html>