PHP Snippets
make_page_links
Information
(PHP Version >= 3)Functions used in this snippet: intval, ceil, min, max.
Description
str make_page_links( int page, int num_pages, str link [, int find [, int offset ]])
make_page_links will make numbered links for multiple pages of data. The function uses the current page of the pages that are available. The pages are split up by how many total entries there are (num_pages) and how many entries per page should be shown (find defaults to 10).
offset is the offset of numbering to show for the links (optional).
Snippet
<?php
function make_page_links($page,$num_pages,$link,$find=10,$offset=3){
$num_pages = intval(ceil($num_pages / $find));
if($num_pages == 1){
return '';
}
if($page < 1){
$page = 1;
}
if($page > $num_pages){
$page = $num_pages;
}
$ret = '';
if($page > $offset){
$ret = '<a href="' . $link . '">1</a> ... ';
}
if($page > 1){
$ret .= '<a href="' . $link . ($page - 1) . '/">«</a> ';
}
$max = min($page + ($offset - 1), $num_pages);
for($i = max(1, $page - ($offset - 1)); $i <= $max; $i++){
$ret .= ($i == $page ? '<strong>' . $i . '</strong>' : '<a href="' . $link . ($i != 1 ? $i . '/' : '') . '">' . $i . '</a> ');
}
if($page < ($num_pages)){
$ret .= '<a href="' . $link . ($page + 1) . '/">»</a> ';
}
if($page < ($num_pages - $offset + 1)){
$ret .= '... <a href="' . $link . $num_pages . '/">' . $num_pages . '</a>';
}
return $ret;
}
?>
Example
make_page_links Can be used in the following way:
<?php
// be on page 3 of 300 entries with a link to index.php
echo make_page_links('3', 300, '/index.php?page=');
// <12345> ... 30
echo make_page_links(7, 300, '/index.php?page=');
// 1 ... <56789> ... 30
echo make_page_links(1, 300, '/index.php?page=', 30, 5);
// 12345> ... 10
?>
0 Responses to make_page_links