Created
September 30, 2010 18:16
-
-
Save opengeek/605043 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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