Created
March 1, 2010 00:08
-
-
Save Burgestrand/317949 to your computer and use it in GitHub Desktop.
A paging example
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 | |
require 'html.php'; | |
// Lite variabler | |
$page = isset($_GET['start']) ? max((int)$_GET['start'], 1) : 1; // nuvarande sida | |
$items_per_page = 25; // antal saker per sida | |
$total_items = 10000; // antal saker (totalt), fås troligtvis från en “SELECT COUNT(*) FROM …” query | |
$paging = paging($page, ceil($total_items / $items_per_page), 3); | |
// Paging innehåller nu all HTML som behövs för din paging, skriv ut den på rätt plats | |
echo $paging; |
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 | |
/** | |
* Calculates the correct paging offsets and returns | |
* the view using them | |
* | |
* @author Kim Burgestrand (http://github.com/Burgestrand/template/blob/master/classes/template/html.php) | |
* @param int current page | |
* @param int total pages | |
* @param int max number of pages shown at a time | |
* @return string | |
*/ | |
function paging($page, $total, $pages = 10) | |
{ | |
$diff = floor($pages / 2); | |
$data = array( | |
'start' => 1, // first page | |
'decrement' => max(1 /*start */, $page - $pages), | |
'low' => max(1 /*start */, min($total - $diff * 2, $page - $diff)), | |
'current' => $page, | |
'high' => min($total, max($pages, $page + $diff)), | |
'increment' => min($total, $page + $pages), | |
'end' => max(1, $total), | |
'pages' => $pages, | |
); | |
return capture('paging.php', $data); | |
} | |
/** | |
* Capture the output of a PHP file (using require) | |
* | |
* @author Kim Burgestrand (http://www.phpportalen.net/viewtopic.php?p=670517#670517) | |
* @param string path to file | |
* @param array variables in scope | |
* @return string | |
*/ | |
function capture($path, array $variables = array()) | |
{ | |
ob_start(); | |
extract($variables, EXTR_SKIP); | |
require $path; | |
return ob_get_clean(); | |
} |
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 if ($start == $end) return; ?> | |
<ol class="nav paging"> | |
<?php | |
$template = '<li><a href="?start=%d">%s</a></li>'; | |
if ($decrement < $low) | |
{ | |
($start < $decrement) AND printf($template, $start, '««'); | |
printf($template, $decrement, '«'); | |
} | |
for ($i = $low; $i <= $high; ++$i) | |
{ | |
$text = $i == $current ? "[{$i}]" : $i; | |
printf($template, $i, $text); | |
} | |
if ($high < $increment) | |
{ | |
printf($template, $increment, '»'); | |
($increment < $end) AND printf($template, $end, '»»'); | |
} | |
?> | |
</ol> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment