Example - Complex

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.

Example 55-1. User Management System Example

<?php
require 'Structures/DataGrid.php';
require 'myclasses/User.php';

// Determine sort order and direction as well as the page to display
$orderBy = isset($_GET['orderBy']) ? $_GET['orderBy'] : 'lname';
$dir = isset($_GET['direction']) ? $_GET['direction'] : 'ASC';

// Define the Data Source for the DataGrid
$user = new User_DataObject();

// Create the DataGrid, bind it's Data Source and define it's columns
$dg =& new Structures_DataGrid(20); // Display 20 per page
$dg->sortRecordSet($orderBy, $dir);
$dg->bind($user);
$dg->addColumn(new Structures_DataGrid_Column(null, null, null, array('width' => '10'), null, 'printCheckbox()'));
$dg->addColumn(new Structures_DataGrid_Column('Name', null, 'lname', array('width' => '40%'), null, 'printFullName()'));
$dg->addColumn(new Structures_DataGrid_Column('Username', 'username', 'username', array('width' => '20%')));
$dg->addColumn(new Structures_DataGrid_Column('Role', null, null, array('width' => '20%'), null, 'printRoleSelector()'));
$dg->addColumn(new Structures_DataGrid_Column('Edit', null, null, 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 table
$table = new HTML_Table($tableAttribs);
$tableHeader =& $table->getHeader();
$tableBody =& $table->getBody();

// Pass the table instance to the DataGrid and set the options
$dg->fill($table, $rendererOptions);

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

// Set alternating row attributes
$tableBody->altRowAttributes(0, $evenRowAttribs, $oddRowAttribs, true);

// Output table and paging links
echo $table->toHtml();
$renderer =& $dg->getRenderer();
echo $renderer->getPaging();

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>';
}
?>
© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.