Re: newbie in datagrid
| From: | brian | Date: | Fri, 10 Nov 2006 20:57:03 +0000 |
| Subject: | Re: newbie in datagrid | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-25762@lists.php.net to get a copy of this message | ||
Alain Roger wrote:
Hi, I'm new to the world of PEAR. I've seem an example of what could be done with PEAR packages and it was great. Now i would like to use it on my website by i'm quite confused. I would like to display a simple table - with 10 rows per page. - if there are more records (than 10), some arrows images should be displayed somewhere to allow user to switch among pages - to allow sorting on columns - to customize the render - to connect to postgreSQL to do that i was thinking to use : - structures/datagrid however, i have some problem to know what will be the good DB package for me. what can i tell, it's that i have a global variabl $db which represents the pg_connect to postgreSQL database. so if $db exists, therefore we are already connected to db. i would like to use a package like that : package_db = $db; package_query = $query; and get the output to a render like HTML table with paging and customization of colors,... So, which DB object/package do you advise me and how to use it ? moreover, which render could be the most interesting for me ? thanks a lot, Al.Try Pear::Pager http://pear.php.net/manual/en/package.html.pager.php I haven't used Structures_DataGrid, as the app is finished and runs well. Perhaps the next time i do some maintenance, i might look into changing that. Anyhoo, Pager is really simple to use. # pear install Pager Once it's installed copy the wrapper class that is included in examples: # cd /usr/local/lib/php # cp doc/Pager/examples/Pager_Wrapper.php Pager/ (adjust path to Pear directory if required) I'm using the MDB2 database interface with Postgres. http://pear.php.net/package/MDB2 # pear install MDB2 MDB2_Driver_pgsql In my scripts that require a DB connection, i include a short script that handles the connection and creates a handle $db. When you want to create your paged DB SELECT: -- snip -- // note that Pager adds the LIMIT to your query for you $sql = 'SELECT foo, bar FROM whatever'; $limit = (!empty($_GET['limit']) ? intval($_GET['limit']) : 20); $pagerOptions = array (
'mode' => 'Sliding','delta' => 2, 'perPage' => $limit, ); /* now pass your DB connection handle to Pager * along with the query and options */ $pager = Pager_Wrapper_MDB2($db, $sql, $pagerOptions); /* You should test here in case there was an error. * DEBUG is defined in my application when on development machine */ if (PEAR::isError($pager)) { $error = (DEBUG ? $pager->getUserInfo() : 'Could not fetch data.'); } -- snip -- Note that, for some reason, Pager's variables are array values. These really should be accessed through class methods, IMHO. For instance, the docs say there's a getLinks method but it doesn't appear to be present. I never got around to investigating that. $pager['totalItems'] will show if your SELECT returned anything. $pager['links'] holds a string that is the HTML links to the other "pages". You can edit the Pager class to customise the output. -- snip -- if (0 == $pager['totalItems']) { ?> <h4>There are no items.</h4> <?php } else { ?> <table> <thead class="PagerLinks">
<tr>
<td colspan="2"><?= $pager['links'] ?> <span>(<?= $pager['totalItems'] ?> items)</span></td>
</tr>
</thead>
<tfoot class="PagerLinks">
<tr>
<td colspan="2"><?= $pager['links'] ?> <span>(<?= $pager['totalItems'] ?> items)</span></td>
</tr>
</tfoot>
<tbody>
<?php
while ($row = each($pager['data']))
{
/* NOTE: use $row[1]['foo'], not $row['foo']
* Another oddity of this class
*/
?>
<tr>
<td><?= $row[1]['foo'] ?></td>
<td><?= $row[1]['bar'] ?></td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
Also note that in my database access script, i have:
$db->loadModule('Extended');
$db->setFetchMode (MDB2_FETCHMODE_ASSOC);
Without this, i'm not sure if $row[1]['foo'] will work. You might need to use $row[1][0] instead.
brian