Example - Complex

Example - Complex – How to build a datagrid using many of the features

Description

An interface to a User Management System

This example will show you how to create an interface to a User Management System using the DB_DataObject package to handle the database aspects of this example.

User Management System Example

<?php
require_once 'Structures/DataGrid.php';
require_once 
'HTML/Table.php';
require_once 
'myclasses/User.php';

// Instantiate the DataObject; that's our DataSource container
$user = new User_DataObject();

// Create the DataGrid
$datagrid =& new Structures_DataGrid(20); // Display 20 records per page

// Specify how the DataGrid should be sorted by default
$datagrid->setDefaultSort(array('lname' => 'ASC'));

// Bind the DataSource container 
$test $datagrid->bind($user);
if (
PEAR::isError($test)) {
    echo 
$test->getMessage(); 
}

// Define columns
$datagrid->addColumn(new Structures_DataGrid_Column(nullnullnull, array('width' => '10'), null'printCheckbox()'));
$datagrid->addColumn(new Structures_DataGrid_Column('Name'null'lname', array('width' => '40%'), null'printFullName()'));
$datagrid->addColumn(new Structures_DataGrid_Column('Username''username''username', array('width' => '20%')));
$datagrid->addColumn(new Structures_DataGrid_Column('Role'nullnull, array('width' => '20%'), null'printRoleSelector()'));
$datagrid->addColumn(new Structures_DataGrid_Column('Edit'nullnull, array('width' => '20%'), null'printEditLink()'));

// Define the Look and Feel
$tableAttribs = array(
    
'width' => '100%',
    
'cellspacing' => '0',
    
'cellpadding' => '4',
    
'class' => 'datagrid'
);
$headerAttribs = array(
    
'bgcolor' => '#CCCCCC'
);
$evenRowAttribs = array(
    
'bgcolor' => '#FFFFFF'
);
$oddRowAttribs = array(
    
'bgcolor' => '#EEEEEE'
);
$rendererOptions = array(
    
'sortIconASC' => '&uArr;',
    
'sortIconDESC' => '&dArr;'
);

// Create a HTML_Table
$table = new HTML_Table($tableAttribs);
$tableHeader =& $table->getHeader();
$tableBody =& $table->getBody();

// Ask the DataGrid to fill the HTML_Table with data, using rendering options
$test $datagrid->fill($table$rendererOptions);
if (
PEAR::isError($test)) {
    echo 
$test->getMessage(); 
}


// Set attributes for the header row
$tableHeader->setRowAttributes(0$headerAttribs);

// Set alternating row attributes
$tableBody->altRowAttributes(0$evenRowAttribs$oddRowAttribstrue);

// Output table and paging links
echo $table->toHtml();

// Display paging links
$test $datagrid->render(DATAGRID_RENDER_PAGER);
if (
PEAR::isError($test)) {
    echo 
$test->getMessage(); 
}


function 
printCheckbox($params)
{
    
extract($params);
    return 
'<input type="checkbox" name="idList[]" value="' $record['id'] . '">';
}
function 
printFullName($params)
{
    
extract($params);
    return 
$record['fname'] . ' ' $record['lname'];
}
function 
printRoleSelector($params)
{
    global 
$roleList;

    
extract($params);
    
    
$html '<select name="role_id">';
    foreach (
$roleList as $roleId => $roleName) {
        
$html .= "<option value=\"$roleId\">$roleName</option>\n";
    }
    
$html .= '</select>';
    
    return 
$html;
}
function 
printEditLink($params)
{
    
extract($params);
    return 
'<a href="edit.php?id=' $record['id'] . '">Edit</a>';
}
?>