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

    
/**
    * Get the ORDER BY sql clause, defaulting to "cm_id DESC", this does not include the string
    * "ORDER BY " so you will have to deal with that yourself.
    *
    * The first parameter is the column name, and the second is the direction. You can
    * use the constants DATAGRID_ORDERING_ASC and DATAGRID_ORDERING_DESC to specify the
    * direction.
    *
    * You can use the string returned by this function in your SQL query, to produce
    * the correct ordering. Anything you do in a callback (for example) may affect how
    * the rows should be ordered (eg changing a datetime into a more readable format),
    * but that can't really be handled by the datagrid.
    */
    
$orderBy = Datagrid::GetOrderBy('cm_id', DATAGRID_ORDERING_DESC);

    
/**
    * 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
    *
    * Notice how the order by string is plugged into the query - simple string
    * concatenation at its heart.
    */
    
$connection = mysql_connect('localhost', 'root', '') OR die('Failed to connect: ' . mysql_error());
    
$resultset = mysql_query($sql = "SELECT cm_id,
                                     ne_title,
                                     cm_author,
                                     cm_datetime,
                                     cm_status,
                                     cm_neid
                                FROM phpguru.comments,
                                     phpguru.news
                               WHERE cm_status = 'ACTIVE'
                                 AND cm_neid = ne_id
                            ORDER BY {$orderBy}"
, $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 column headers off/on
    */
    
$grid->showHeaders = true;

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

    
/**
    * This simply sets the specified columns not to be passed through htmlspecialchars()
    * Generally any column that shows HTML
    */
    
$grid->NoSpecialChars('ne_title', 'status', 'actions');
    
    
/**
    * Hide the cm_neid column as it's only used for linking purposes
    */
    
$grid->HideColumn('cm_neid', 'cm_status');
    
    
/**
    * Here just for show. This function sets the number of rows to set per page.
    * The default is 20.
    */
    
$grid->SetPerPage(15);
    
    
/**
    * The callback function that adds the extra column
    */
    
$grid->AddCallback('RowCallback');

    function
RowCallback(&$row)
    {
        
$row['actions'] = sprintf('<a href="http://www.phpguru.org/article.php/%d#comment_%d" target="_blank">View</a>', $row['cm_neid'], $row['cm_id']);

        
$length = 25;
        if (
strlen($row['ne_title']) > $length) {
            
$row['ne_title'] = substr($row['ne_title'],0, 25) . '...';
        }

        
$row['ne_title'] = sprintf('<a href="/article.php/%d" target="_blank" title="%s">%s</a>', $row['cm_neid'], $row['ne_title'], $row['ne_title']);
        
        
// Format the datetime field. We do it here so that ordering is OK
        
$row['cm_datetime'] = date('H:i jS F Y', strtotime($row['cm_datetime']));
    }
    
    
/**
    * The HTML. The appearance can be customised using CSS. Naturally you would (should) put
    * all of the datagrid styling in a central CSS file that can be <link>ed to by all of
    * your websites' pages. That way:
    *  o All of your datagrids will look the same
    *  o Changes to the appearance will affect all of your datagrids across your
    *    entire website. You might not want this necessarily, but might.
    *
    * eg. <link rel="stylesheet" type="text/css" media="screen" href="/css/datagrid.css" />
    */
?>
<html>
<head>
    <title>Datagrid example</title>
    
    <style type="text/css">
    <!--
        /* Format the page */
        body {
            background-color: #eee;
        }
        
        /* Format the entire table */
        .datagrid {
            font-family: Arial;
            font-size: smaller;
            width: 600px;
        }

        /* Format the table headers */
        .datagrid thead th {
            background-color: #003;
            color: #fff;
            height: 25px;
            text-align: left;
        }
        
        /* Colour the headers white */
        .datagrid thead th a {
            color: white;
            text-decoration: none;
        }
        
        /* Padding on headers and body cells */
        .datagrid thead th,
        .datagrid tbody td {
            padding-left: 5px;
            padding-right: 5px;
        }
        
        /* Row colours */
        .datagrid tbody td.col_0,
        .datagrid tbody td.col_2        {
            background-color: #fff;
        }
        
        /* Alternate row colours */
        .datagrid tbody td.col_1,
        .datagrid tbody td.col_3        {
            background-color: #cff;
        }
        
        /* Show the left hand border on all table cells */
        .datagrid tbody td {
            border-left: 1px solid #6c9;
        }
        
        /* Don't show the left border on the last column... */
        .datagrid tbody td.col_4 {
            border-left: 0;
        }
        
        /* ...show the right border on the next to last column */
        .datagrid tbody td.col_3 {
            border-right: 1px solid #6c9;
        }
        
        /* Show the bottom border */
        .datagrid tbody td.row_14 {
            border-bottom: 1px solid #6c9;
        }
        
        /* No underline for the view links */
        .datagrid tbody td.row_14 {
            border-bottom: 1px solid #6c9;
        }
        
        .datagrid tbody td.col_1 a,
        .datagrid tbody td.col_4 a {
            text-decoration: none;
        }
        
        .datagrid tbody td.col_4 {
            border-bottom: 0;
        }
    // -->
    </style>
</head>
<body>

    <h1>Example 5</h1>

    <?$grid->Display()?>
</body>
</html>