Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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 `myTable` WHERE `food` = 'beer'");
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 * FROM `myTable` WHERE `food` = 'beer' ORDER BY `myTable_id`";
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);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.