|
|
|
Offer pages function
1 2 3 4 5 6 7 200
Click on a page number in the list above to go to that hypothetical page.
The first number is always Page 1, while the final one is always the final page.
Here is a function that generates the above pages list effect:
<?php
function offerpages($thispage, $leeway, $nopages)
{
//
// $thispage = Current page number, between 1 and $nopages
// $leeway = Left and right range of page numbers to display
// $nopages = Number of pages in all
//
$prefix = ""; // For example, you could set this to "P." or "P"
$php_self = $_SERVER['PHP_SELF'];
if ($thispage < 1) $thispage = 1; /* Too small - fix by forcing to 1 */
if ($thispage > $nopages) $thispage = $nopages; /* Too large - fix by forcing final page */
$page1 = $thispage - $leeway;
$pagen = $thispage + $leeway;
if ($page1 < 1)
{
$pagen = $pagen - $page1 + 1; /* Adjust top of the range */
$page1 = 1; /* Force to 1 */
}
if ($pagen >= $nopages)
{
$page1 = $page1 + $nopages - $pagen;
$pagen = $nopages;
if ($page1 < 1) $page1 = 1;
}
if ($page1 > 1)
{
// Always give the first page
echo "<a href=\"$php_self?thispage=1\">${prefix}1</a> ";
}
for ($i = $page1; $i <= $pagen; $i ++)
{
if ($i != $thispage)
{
// Hyperlink to the next page
echo "<a href=\"$php_self?thispage=$i\">$prefix$i</a> ";
}
else
{
// Current page: don't hyperlink it
echo "$prefix$i ";
}
}
if ($pagen < $nopages)
{
echo "<a href=\"$php_self?thispage=$nopages\">$prefix$nopages</a>";
}
echo "<br />\n";
}
?>
|
Call it as follows:
<?php
$thispage = <current page number>
$leeway = <Number of pages 'left' and 'right' to offer>
$nopages = <Number of pages in total>
offerpages($thispage, $leeway, $nopages);
?>
|
In the example above, a hardcoded call could be as follows:
<?php
$thispage = (isset($_GET['thispage'])) ? $_GET['thispage'] : 1;
offerpages($thispage, 3, 200);
?>
|
|