To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-17-2003, 11:15 PM   #1
planetsim
code | beer > sleep
 
Join Date: Sep 2002
Location: aus
Posts: 4,826
My Pagination Script

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.

PHP Code:
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;
}
__________________
Dont be lazy Search
And use the Manual
Webmobo - Open Source News Script | Portfolio / Blog | Against TCPA
planetsim is offline   Reply With Quote
Old 10-18-2003, 07:26 PM   #2
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
Why are you using HTTP_GET_VARS? _GET is a lot shorter & its a superglobal.
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-18-2003, 07:35 PM   #3
planetsim
code | beer > sleep
 
Join Date: Sep 2002
Location: aus
Posts: 4,826
My Host doesnt like $_GET for some reason and also its set up so it can work for older php versions
__________________
Dont be lazy Search
And use the Manual
Webmobo - Open Source News Script | Portfolio / Blog | Against TCPA
planetsim is offline   Reply With Quote
Old 10-18-2003, 07:36 PM   #4
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
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?
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-18-2003, 07:41 PM   #5
planetsim
code | beer > sleep
 
Join Date: Sep 2002
Location: aus
Posts: 4,826
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.
__________________
Dont be lazy Search
And use the Manual
Webmobo - Open Source News Script | Portfolio / Blog | Against TCPA
planetsim is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 10:58 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.