//
// Generates the pagination
//
function prepare_pagination($item_count, $per_page, $page_now, $action)
{
global $root_path;
if ($item_count == 0)
{
return '';
}
$action = $action2 = $root_path . $action;
$action2 .= (strpos($action2, '?') !== false) ? '&' : '?';
$page_count = ceil($item_count / $per_page);
if ($page_count == 1)
{
return "Seite $page_now von $page_count";
}
else
{
// Which numbers should be shown?
if ($page_count <= 5)
{
$showed_numbers = range(1, $page_count);
}
else if ($page_now == 1 || $page_now == $page_count)
{
$showed_numbers = array(1, 2, $page_count - 1, $page_count);
}
else
{
$showed_numbers = array(1, 2, $page_now - 1, $page_now, $page_now + 1, $page_count - 1, $page_count);
$showed_numbers = array_unique($showed_numbers);
}
// generate pagination
$pagination = '';
$last_number = 0;
foreach ($showed_numbers as $number)
{
if (($last_number + 1) < $number)
{
$pagination .= ' ... ';
}
else
{
$pagination .= ($pagination != '') ? ', ' : '';
}
$this_action = ($number != 1) ? $action2 . "page=$number" : $action;
$pagination .= ($page_now != $number) ? '[
url="' . $this_action . '"]' . $number . '[/
url]' : $number;
$last_number = $number;
}
// add "<" and ">" to browse faster ;-)
if ($page_now != 1)
{
$this_action = ($page_now != 2) ? $action2 . 'page=' . ($page_now - 1) : $action;
$pagination = '[
url="' . $this_action . '"]<[/
url] ' . $pagination;
}
if ($page_now != $page_count)
{
$pagination .= ' [
url="' . $action2 . 'page=' . ($page_now + 1) . '"]>[/
url]';
}
return "Seite $page_now von $page_count: $pagination";
}
}