Hi everyone! I'm new to MODx Revolution (and to the Forum as well) and although I managed to learn a lot these days, sometimes I get stuck with things that I'm normally able to do using, for example, only PHP and MySql outside MODx. One of such things is to paginate a table.
After a lot of search I first followed this page:
https://gist.github.com/605043/646a47095c26b2788f66aaa42f597bb37ce91dea and then I could see the right output (with so many beautiful arrays) on my screen based on my query.
Hier my snippet (dataBaseQuery):
<?php
$output = array();
$total = 0;
$limit = isset($limit) ? (integer) $limit : 0;
$offset = isset($offset) ? (integer) $offset : 0;
$totalVar = !empty($totalVar) ? $totalVar : 'total';
// determine the total number of records for your criteria
$totalStmt = $modx->query("SELECT COUNT(*) FROM table WHERE code in (574512,37770812,37770814,37770865,37770887,37770919,37770988,37772219)");
if ($totalStmt) {
$total = (integer) $totalStmt->fetch(PDO::FETCH_COLUMN);
$totalStmt->closeCursor();
}
// set a placeholder for getPage to get the total for it's calculations
$modx->setPlaceholder($totalVar, $total);
// now execute your query with limit/offset if the total > 0
if ($total > 0) {
// NOTE: always order by a unique column when using LIMIT/OFFSET
$sql = "SELECT auth, nr, kid, code FROM table WHERE code in (574512,37770812,37770814,37770865,37770887,37770919,37770988,37772219) ORDER BY code";
if (!empty($limit)) {
$sql.= " LIMIT {$limit}, {$offset}";
}
$pdoStmt = $modx->query($sql);
if ($pdoStmt) {
while ($row = $pdoStmt->fetch(PDO::FETCH_ASSOC)) {
$placeholders = array_merge($scriptProperties, $row);
if (!empty($tpl)) {
$output[] = $modx->getChunk($tpl, $placeholders);
}
else {
$output[] = "<pre>" . print_r($placeholders, true) . "</pre>";
}
}
}
}
return implode("\n", $output);
?>
Then I create a chunk and added following line on the top of my snippet:
$tpl = $modx->getOption('tpl',$scriptProperties,'tablePagination');
Hier the code of the chunk (tablePagination):
<tr>
<td>[[+auth]][[+nr]][[+kid]]</td><td>[[+code]]</td>
</tr>
Now when I use the snippet as follow on the page I receive the expect result, i.e. a table with my values:
<table>
[[dataBaseQuery]]
</table>
Now I would like to paginate it, but everything I do gives me no result.
Reading the documentation I thought I should use this:
[[!getPage? &element=`getResources` &tpl=`tablePagination`]]
But doing so, I receive no result and the site stays empty. What I'm doing wrong?
Many thanks!