<?php
    
/**
    * A datagrid class. The appearance can be customised with CSS
    */
    
class Datagrid
    
{
        
/**
        * Properties;
        */
        
private $properties;

        
/**
        * Getter
        */
        
public function __get($name)
        {
            return
$this->properties[$name];
        }

        
/**
        * Setter
        */
        
function __set($name, $value)
        {
            
$this->properties[$name] = $value;
        }
    
        
/**
        * The constructor
        */
        
function __construct($connection, $resultset)
        {
            
$this->showHeader    = true;
            
$this->cellpadding   = 0;
            
$this->cellspacing   = 0;
            
$this->connection    = $connection;
            
$this->resultset     = $resultset;
            
$this->numresults    = mysql_num_rows($resultset);
            
$this->startnum      = @(int)$_GET['start'];
            
$this->perPage       = 20;
            
$this->hiddenColumns = array();
            
$this->colnum        = mysql_num_fields($this->resultset) - count($this->hiddenColumns);
            
            
// Don't allow startnum to be lower than zero
            
if ($this->startnum < 0) {
                
$this->startnum = 0;
            }
            
            
// Don't allow startnum to be greater than the number of rows in the result set,
            // well, one less to allow for zero indexing
            
if ($this->startnum >= $this->numresults) {
                
$this->startnum = 0;
            }

            
// Check the MySQL connection is valid
            
if (!$connection OR !is_resource($connection)) {
                die(
'<p /><span style="color: red">Error - the MySQL connection you have passed to the datagrid constructor is not valid</span>');
            }

            
// Check the MySQL result set is valid
            
if (!$resultset OR !is_resource($resultset)) {
                die(
'<p /><span style="color: red">Error - the MySQL result set you have passed to the datagrid constructor is not valid</span>');
            }
        }
        
        
/**
        * Sets the displayed header names for the columns
        *
        * @param array $cols The column names
        */
        
public function SetDisplayNames($cols)
        {
            
$this->colnames = $cols;
        }
        
        
/**
        * Hides a particular column, or multiple columns
        *
        * @param ... strings One or more column names
        */
        
function HideColumn()
        {
            
$args = func_get_args();
            
            foreach (
$args as $column) {
                
$this->hiddenColumns[] = $column;
            }
            
            
$this->hiddenColumns = array_unique($this->hiddenColumns);
        }
        
        
/**
        * Sets the column names (not using the display names) that
        * don't get htmlspecialchars() applied to them
        *
        * @param string ... One or more column names
        */
        
public function NoSpecialChars()
        {
            
$this->noSpecialChars = func_get_args();
        }
        
        
/**
        * Shows the datagrid.
        */
        
function Display()
        {
            
/**
            * Seek to the correct place in the result set
            */
            
mysql_data_seek($this->resultset, $this->startnum);

            
/**
            * Initialise the row number
            */
            
$rownum = 0;

            
/**
            * Get the headers from the first row, then seek back to zero
            */
            
$row = mysql_fetch_array($this->resultset, MYSQL_ASSOC);
            
$this->headers     = array_keys($row);
            
$this->initialcols = count($row);
            
mysql_data_seek($this->resultset, $this->startnum);

            
?>
<script language="javascript" type="text/javascript">
<!--
    /**
    * The row mouseover function
    */
    function MouseOver(rownum)
    {
        var tags = document.getElementsByTagName('td')

        for (var i=0; i<tags.length; i++) {
            if(tags[i].className.indexOf('row_' + rownum + ' ') != -1) {
                tags[i].className = tags[i].className += ' mouseover';
            };
       }
    }
    
    /**
    * the row mouseout function
    */
    function MouseOut(rownum)
    {
        var tags = document.getElementsByTagName('td')

        for (var i=0; i<tags.length; i++) {
            if(tags[i].className.indexOf('row_' + rownum) != -1) {
                tags[i].className = tags[i].className.replace(/ mouseover/, '');
            };
        }
    }
// -->
</script>
<table border="0" cellspacing="<?=$this->cellspacing?>" class="datagrid">

    <?if($this->showHeader):?>
        <thead>
            <tr>
                <?foreach($this->headers as $k => $h):?>
                    <?if(in_array($h, $this->hiddenColumns)) continue?>

                    <th title="<?=($h = htmlspecialchars(!empty($this->colnames[$h]) ? $this->colnames[$h] : $h))?>"><?=$h?></th>
                <?endforeach?>
            </tr>
        </thead>
    <?endif?>

    <tbody>
        <?while($row = mysql_fetch_array($this->resultset, MYSQL_ASSOC)):?>
            <?$colnum = 0; @$rowcount++?>
            <?if($this->rowcallback):?>
                <?call_user_func($this->rowcallback, &$row)?>
            <?endif?>

            <tr onmouseover="MouseOver(<?=intval($rownum)?>)" onmouseout="MouseOut(<?=intval($rownum)?>)">
                <?foreach($row as $k => $v):?>

                    <?if(in_array($k, $this->hiddenColumns)) continue?>

                    <td class="row_<?=intval($rownum)?> col_<?=(!empty($colnum) ? $colnum : 0)?> <?if($rownum % 2 == 1):?>altrow<?endif?>  <?if($colnum % 2 == 1):?>altcol<?endif?>">
                        <?=(in_array($k, $this->noSpecialChars) ? $v : htmlspecialchars($v))?>
                    </td>
                    
                    <?$colnum++?>
                <?endforeach?>
            </tr>

            <?if($rownum++ == 19) break?>
        <?endwhile?>
    </tbody>
    
    <tfoot>
        <tr>
            <td colspan="<?=$this->colnum?>">
                <?if(@$this->startnum > 0):?>
                    <span style="float: left">
                        <a href="?start=<?=($this->startnum - 20)?>">
                            &laquo; Prev
                        </a>
                    </span>
                <?endif?>


                <?if($this->numresults > (@$this->startnum + 20)):?>
                    <span style="float: right">
                        <a href="?start=<?=$this->startnum + 20?>">
                            Next &raquo;
                        </a>
                    </span>
                <?endif?>
            </td>
        </tr>
        
        <tr>
            <td align="center" colspan="<?=$this->colnum?>">
                <?=intval($this->startnum) + 1?>-<?=(intval($this->startnum) + $rowcount)?> of <?=intval($this->numresults)?> results
            </td>
        </tr>
    </tfoot>
</table>
            <?php
        
}
    }
?>