previousExemple - Simple (Previous) (Next) Custom DataSourcesnext

View this page in Last updated: Sun, 18 Oct 2009
English | Brazilian Portuguese | Chinese | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Turkish

Exemple élaboré

Exemple élaboré – Comment construire une grille de données en utilisant plusieurs fonctionalités

Description

Une interface pour un système de gestion d'utilisateurs.

Cet exemple va vous expliquer comment créer une interface pour un système de gestion d'utilisateurs en utilisant le paquet DB_DataObject pour gérer l'aspects base de données de cet exemple.

Exemple d'un système de gestion d'utilisateurs

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

// Instanciation de l'objet DataObject ; c'est notre conteneur DataSource
$utilisateur = new User_DataObject();

// Création de la grille de données
$datagrid =& new Structures_DataGrid(20); // Display 20 records per page

// Spécifie comment la grille de données doit être triée par défaut
$datagrid->setDefaultSort(array('lname' => 'ASC'));

//et on lie le conteneur DataSource : $utilisateur à la grille
$test $datagrid->bind($utilisateur);
if (
PEAR::isError($test)) {
    echo 
$test->getMessage();
}

// Définition des colonnes
$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()'));

// Définition de l'apparence
$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;'
);

// Création du tableau HTML
$table = new HTML_Table($tableAttribs);
$tableHeader =& $table->getHeader();
$tableBody =& $table->getBody();

// Demande à la grille de données de remplir le tableau HTML avec les données, en utilisant les options d'affichage définies
$test $datagrid->fill($table$rendererOptions);
if (
PEAR::isError($test)) {
    echo 
$test->getMessage();
}

// Définition des attributs pour la ligne d'en-tête
$tableHeader->setRowAttributes(0$headerAttribs);

// Définition des attributs de lignes alternativement
$tableBody->altRowAttributes(0$evenRowAttribs$oddRowAttribstrue);

// Affichage de la table et des liens page par page
echo $table->toHtml();

// Affichage des liens page par page
$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>';
}
?>
previousExemple - Simple (Previous) (Next) Custom DataSourcesnext

Download Documentation Last updated: Sun, 18 Oct 2009
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
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.