Click to See Complete Forum and Search --> : My Pagination Script


planetsim
10-17-2003, 11:15 PM
Probably the most ugliest looking code ever

I did it in 5-10mins its tested and it works for what i need it for.

Im sure there are plenty of improvements that can be made.


function pagination($limit,$pages,$numrows,$pageinfo){
global $HTTP_GET_VARS; //damn its not superglobal.
$pagecode = '';

##Goto FirstPage
if($HTTP_GET_VARS['page'] != 0){
$pagecode .= " <a href=\"".$pageinfo."page=0\">&lt;&lt; First Page</a> ";
}
##Check for Previous

if(($HTTP_GET_VARS['page']-$limit) < $numrows && $HTTP_GET_VARS['page'] != 0){
$pagecode .= "<a href=\"".$pageinfo."page=".($HTTP_GET_VARS['page']-$limit)."\">&lt;</a> ";
}else{
$pagecode .= "&lt; ";
}

#Work Out the Number to start in the for loop

if($HTTP_GET_VARS['page'] == "0"){
$startpage = 1;
}else{
$start = $HTTP_GET_VARS['page'] / $limit - 1;
if($start == "0"){
$startpage = 1;
}else{
$startpage = $start;
}
}

##Max Show Workout
if($startpage == "1"){
if($startpage + 3 > $pages){
$page = $pages - $startpage;
$maxshow = $startpage + $page;
}else{
$maxshow = $startpage + 3;
}
}else{
if($startpage + 2 < $pages){
$page = $pages - $startpage;
$maxshow = $startpage + $page;
}else{
if($startpage + 2 == $pages){
$maxshow = $pages;
}else{
$maxshow = $pages-2;
}
}
}

##DO the 1 2 3 4 etc
for($i = $startpage; $i<=$maxshow; $i++){
$b = $i - 1;
if($HTTP_GET_VARS['page'] / $limit + 1 == $i){
$pagecode .= "[".$i."]";
}else{
$pagecode .= " <a href=\"".$pageinfo."page=".($limit*$b)."\">".$i ."</a> ";
}
}

##Next
if(($HTTP_GET_VARS['page']+$limit) < $numrows){
$pagecode .= " <a href=\"".$pageinfo."page=".($HTTP_GET_VARS['page']+$limit)."\">&gt;</a>";
}else{
$pagecode .= " &gt;";
}

##Goto LastPage
if(($pages-1)*$limit != $HTTP_GET_VARS['page']){
$pagecode .= " <a href=\"".$pageinfo."page=".(($pages-1)*$limit)."\">Last Page &gt;&gt;</a>";
}

return $pagecode;
}

Moonglobe
10-18-2003, 07:26 PM
Why are you using HTTP_GET_VARS? _GET is a lot shorter & its a superglobal.

planetsim
10-18-2003, 07:35 PM
My Host doesnt like $_GET for some reason and also its set up so it can work for older php versions

Moonglobe
10-18-2003, 07:36 PM
well i have to say that if someone's running something less than 4.1.0 they should REALLY upgrade........

and what do you mean they dont like $_GET? they dont like you usign it?

planetsim
10-18-2003, 07:41 PM
more like it doesnt work not too sure why i did a phpinfo(); on it and checked it all seemed ok.

yes people using 4.1.0 should upgrade but remember the hosts may not want to upgrade or not sure how. Although they should move hosts of course.