Skip to content

Instantly share code, notes, and snippets.

@opengeek
Created September 30, 2010 18:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opengeek/605043 to your computer and use it in GitHub Desktop.
Save opengeek/605043 to your computer and use it in GitHub Desktop.
<?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