Want to have a pagination script so your next links display like Google? Look no further..
<?php
// quickLink
# writes out page links
function quickLink ($linkHref, $desc, $accessKey, $linkTitle) {
$theLink = '<a href="'. $linkHref .'#pagination" title="'. $desc .'" accesskey="'.
$accessKey .'">'. $linkTitle .'</a>';
return $theLink;
}
// google_like_pagination.php
function pagination($number, $show, $showing, $firstlink, $baselink, $seperator) {
$disp = floor($show / 2);
if ( $showing <= $disp) :
if ( ($disp - $showing) > 0 ):
$low = ($disp - $showing);
else:
$low = 1;
endif;
$high = ($low + $show) - 1;
elseif ( ($showing + $disp) > $number) :
$high = $number;
$low = ($number - $show) + 1;
else:
$low = ($showing - $disp);
$high = ($showing + $disp);
endif;
// next / prev / first / last
if ( ($showing - 1) > 0 ) :
if ( ($showing - 1) == 1 ):
$prev = quickLink ($firstlink, 'Previous', '', 'Previous');
else:
$prev = quickLink ($baselink . $seperator . ($showing - 1),
'Previous', 'z', 'Previous');
endif;
else:
$prev = 'Previous';
endif;
$next = ($showing + 1) <= $number ?
quickLink ($baselink . $seperator . ($showing + 1),
'Next', 'x', 'Next') : 'Next';
if ( $_SERVER['REQUEST_URI'] == $firstlink ):
$first = '<span class="sel">First Page</span>';
else:
$first = quickLink ($firstlink, 'First Page', '', 'First Page');
endif;
if ( $showing == $number ):
$last = '<span class="sel">Last Page</span>';
else:
$last = quickLink ($baselink . $seperator . $number, 'Last Page', '', 'Last Page');
endif;
$navi = '<div id="pagination">
<ul>'."\n";
// start the navi
$navi .= '<li>'. $prev ."</li>\n";
// loop through the numbers
foreach (range($low, $high) as $newnumber):
$link = ( $newnumber == 1 ) ? $firstlink :
$baselink . $seperator . $newnumber;
if ($newnumber > $number):
$navi .= '';
elseif ($newnumber == 0):
$navi .= '';
else:
$navi .= ( $newnumber == $showing ) ?
'<li class="sel">'. $newnumber .'</li>'."\n" :
'<li>'. quickLink ($link, 'Page '. $newnumber, '', $newnumber) ."</li>\n";
endif;
endforeach;
// end the navi first line
$navi .= '<li>'. $next ."</li>\n";
$navi .= '</ul>'."\n";
// second line
$navi .= '<p>'. $first ."\n | Showing page ". $showing .' of '. $number ."\n | ".
$last .'</p>
</div>';
return $navi;
}
/*
## fake content example
foreach (range(1, 301) as $newnumber):
## fake content example
$content []= 'this is page '. ($newnumber - 1);
endforeach;
## Settings
$showing = !isset($_GET["view"]) ? 1 : $_GET["view"];
$firstlink = $_SERVER['PHP_SELF'];
$seperator = '?view=';
## $baselink can be used if for example when the main script was index.php not the filename
$baselink = $firstlink;
## $show must be an odd number for this to work correctly
## This is the number of links between next and previous links
$show = 3;
echo $content[$showing];
echo pagination(count($content), $show, $showing, $firstlink, $baselink, $seperator);
*/
Note that this does need a bit of tweaking and there may well be a few bugs :)
Comments
some changes in your script
Hi, I used your script, and it really saved me some time.
I transformed it to a class, and also made some changes.
Hope that helps a little!
Thanks!
Matias Paterlini
Thanks for your input
Hi Matias,
Thanks for your input, will give it a go :)
Sure others will find it useful too...
Br
Jamie
Unraveling the mysteries of the web
http://www.skiffie.com
Bug into your script
Hi,
just to alert you about a bug I've found, but not fixed yet:
it's about displaying more than 5 links.
when you put for example, $show=6, the 1st link is not displayed.
things become normal when u crawl page #2;
i've test to put $show=8, things become normal on page #3.
Regards.
I know
Hi Gewix,
I know there are a few bugs - but thanks for the heads up anyway.
For example I also know that if you chose an "even" number then this script doesn't work at all.
I wrote this snippets over a year ago and have managed to implement this OK on a few of my other sub-sites.
I will be trying to address these issues again shortly and will post the updates soon.
Have you tried the snippet posted by Matias below the original??
Thanks again for the feedback.
Br
Jamie
Making Drupal Mobile
Bug into your script
not yet implemented Matias contrib because, I've just learned about managing php classs and can't figure how to do.