Forked from umutakturk/php_mongodb_simple_pagination.php
Last active
August 29, 2015 14:17
-
-
Save usmanghani599/98bb538f13445a5390a2 to your computer and use it in GitHub Desktop.
This file contains 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 | |
$mongodb = new Mongo("mongodb://username:password@localhost/database_name"); | |
$database = $mongodb->database_name; | |
$collection = $database->collection; | |
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1; | |
$limit = 12; | |
$skip = ($page - 1) * $limit; | |
$next = ($page + 1); | |
$prev = ($page - 1); | |
$sort = array('createdAt' => -1); | |
$cursor = $collection->find()->skip($skip)->limit($limit)->sort($sort); | |
foreach ($cursor as $r) { | |
echo sprintf('<p>Added on %s. Last viewed on %s. Viewed %d times. </p>', $r['createdAt'], $r['lastViewed'], $r['counter']); | |
} | |
$total = $cursor->count(); | |
if($page > 1){ | |
echo '<a href="?page=' . $prev . '">Previous</a>'; | |
if($page * $limit < $total) { | |
echo ' <a href="?page=' . $next . '">Next</a>'; | |
} | |
} else { | |
if($page * $limit < $total) { | |
echo ' <a href="?page=' . $next . '">Next</a>'; | |
} | |
} | |
$mongodb->close(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment