PEAR Manual
Prev |  | Next

# 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
    $dg->renderer->setTableHeaderAttributes(array('bgcolor' => 'CCCCCC'));
    $dg->renderer->setTableEvenRowAttributes(array('bgcolor' => '#FFFFFF'));
    $dg->renderer->setTableOddRowAttributes(array('bgcolor' => '#EEEEEE'));
    $dg->renderer->setTableAttribute('width', '100%');
    $dg->renderer->setTableAttribute('cellspacing', '0');
    $dg->renderer->setTableAttribute('cellpadding', '4');
    $dg->renderer->setTableAttribute('class', 'datagrid');
    $dg->renderer->sortIconASC = '&uArr;';
    $dg->renderer->sortIconDESC = '&dArr;';

    // Print the DataGrid and the Paging
    $dg->render();
    echo $dg->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>';
    }
    ?>

Prev | Home | Next
Example - Quick | Up | Example - Extending the DataGrid