WebFrameWorker

gennaio 4, 2007

PEAR Structures_DataGrid: come mostrare efficacemente dati tabulari

Filed under: datagrid,Pear,PHP — stablum @ 1:22 pm

E’ frequente che un programmatore perda un sacco di tempo nel creare cicli per fare il fetching dei dati e la consecutiva stampa in HTML, magari corredata da un sistema ultra-complicato di paging che da molte frustrazioni. Sembra che una soluzione sia stata trovata grazie a un ottimo sistema di astrazione OO fornitoci da quei geniacci di pear.

DataGrid è un pacchetto PEAR che permette di gestire in modo assolutamente modulare flussi di dati tabulari provenienti dalle più svariate sorgenti (DataBase, CSV, XML…) per mostrarli nei più svariati formati: HTML, XML, MsExcel…

Le sorgenti dati sono rappresentate dai pacchetti PEAR Structures_DataGrid_DataSource_* e i formati di output sono nei pacchetti denominati Structures_DataGrid_Renderer_*

Dopo la creazione dell’oggetto DataGrid si usa il metodo bindDataSource() per “agganciare” la sorgente dei dati (ad esempio un DB_DataObject) e il metodo setRenderer() per impostare il tipo di output.

Molto interessante è l’utilizzo di funzioni callback nel Column Formatter per creare una sorta di post-filtro che va a modificare il dato da visualizzare; ad esempio, in una tabella HTML, invece che mostrare semplicemente un dato, crea un link che conduce al form di modifica di quel dato stesso.

Ecco un esempio che sfrutta la paginazione automatica:
<?php
require 'Structures/DataGrid.php';
// 10 records per page
$datagrid =& new Structures_DataGrid(10);
// Setup your datasource
$options = array('dsn' => 'mysql://user:password@host/db_name');
$test = $datagrid->bind("SELECT * FROM my_table", $options);
if (PEAR::isError($test)) {
echo $test->getMessage();
}
// Print the DataGrid with the default renderer (HTML Table)
$test = $datagrid->render();
if (PEAR::isError($test)) {
echo $test->getMessage();
}
// Print the HTML paging links
$test = $datagrid->render(DATAGRID_RENDER_PAGER);
if (PEAR::isError($test)) {
echo $test->getMessage();
}
?>

per tutto il resto: qui c’è il manuale

Lascia un commento »

Non c'è ancora nessun commento.

RSS feed dei commenti a questo articolo. TrackBack URI

Lascia un Commento

Fill in your details below or click an icon to log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Log Out / Modifica )

Foto Twitter

You are commenting using your Twitter account. Log Out / Modifica )

Foto di Facebook

You are commenting using your Facebook account. Log Out / Modifica )

Connecting to %s

Theme: Rubric. Blog su WordPress.com.

Follow

Get every new post delivered to your Inbox.