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>';
}
?>
Quickly retrieve data from a database table (Previous) How to write your own DataSource driver. (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: DearNewbie@example.com
Dear Newbie,
http://pear.php.net/manual/en/package.database.db-dataobject.intro-purpose.php
Note by: bhende19@gmail.com
I agree - working COMPLETE examples are the only way a newbie can really learn this stuff.

Currently I am having problems with the case where we select only a few columns from a table. i.e., doing a "SELECT col1, col2, col2 FROM $table ORDER BY col3" . I can get "SELECT * FROM $table" to work easily - no problem. Don't get how to add / subtract columns.

Thanks
Note by: user@example.com
For newbies like me, a disclosure of the myclasses ("require_once 'myclasses/User.php';") would be MOST helpful. I can't get the example to run and have no way to know what is in that class. Please publish the listing or make a download link. Thanks.