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 02-04-2004, 09:51 PM   #1
Nate
2+2=5
 
Join Date: Aug 2002
Location: Washington
Posts: 169
My first class... MySQL next/prev paging

very interested to hear opinions! classes have always confused me but i think i MAY have finally grasped the concept. please take a look. (Sorry for the lack of comments... I need to work on that)

I put the following code in its own file...

PHP Code:
<?php

class MySQLPagedResults {

   var
$total_results_sql;
   var
$current_page;
   var
$results_per_page;
   var
$links_per_page;
   var
$previous_link_text;
   var
$next_link_text;

   function
MySQLPagedResults($total_results_sql,$page_name,$url_string,$results_per_page,$links_per_page,$first_link_text,$previous_link_text,$next_link_text,$last_link_text,$seperator) {
      
$this->total_results_sql = $total_results_sql;
      if(!isset(
$_GET[$page_name])) {
         
$this->current_page = 1;
      } else {
         
$this->current_page = $_GET[$page_name];
      }
      
$this->results_per_page = $results_per_page;
      
$this->links_per_page = $links_per_page;
      
$this->previous_link_text = $previous_link_text;
      
$this->next_link_text = $next_link_text;
      
$this->first_link_text = $first_link_text;
      
$this->last_link_text = $last_link_text;
      
$this->page_name = $page_name;
      
$this->url_string = $url_string;
      
$this->seperator = $seperator;
   }

   function
totalResults() {
      
$query = mysql_query($this->total_results_sql);
      
$result = mysql_fetch_array($query);
      return
$result[0];
   }

   function
totalPages() {
      return
ceil($this->totalResults()/$this->results_per_page);
   }

   function
currentOffset() {
      return (
$this->current_page-1)*$this->results_per_page;
   }

   function
isFirstPage() {
      return (
$this->current_page <= 1);
   }

   function
isLastPage() {
      return (
$this->current_page >= $this->totalPages());
   }

   function
getPrevNav() {
      
$nav='';
      
//Deal with previous link
      
if(!$this->isFirstPage()) {
         
$nav.='<a href="?'.$this->page_name.'='.($this->current_page-1).''.$this->url_string.'">'.$this->previous_link_text.'</a>';
      } else {
         
$nav.=$this->previous_link_text;
      }
      return
$nav;
   }

   function
getNextNav() {
      
//Deal with next link
      
if(!$this->isLastPage()) {
         
$nav.='<a href="?'.$this->page_name.'='.($this->current_page+1).''.$this->url_string.'">'.$this->next_link_text.'</a>';
      } else {
         
$nav.=$this->next_link_text;
      }
      return
$nav;
   }

   function
getFirstNav() {
      
$nav='';
      
//Deal with first page nav link
      
if(!$this->isFirstPage()) {
         
$nav.='<a href="?'.$this->page_name.'=1'.$this->url_string.'">'.$this->first_link_text.'</a>';
      } else {
         
$nav.=$this->first_link_text;
      }
      return
$nav;
   }

   function
getLastNav() {
      
$nav='';
      
//Deal with last page nav link
      
if(!$this->isLastPage()) {
         
$nav.='<a href="?'.$this->page_name.'='.$this->totalPages().''.$this->url_string.'">'.$this->last_link_text.'</a>';
      } else {
         
$nav.=$this->last_link_text;
      }
      return
$nav;
   }

   function
getStartNumber() {
      
$links_per_page_half = $this->links_per_page/2;
      if(
$this->current_page<=$links_per_page_half) {
         return
1;
      } elseif(
$this->current_page>=$this->totalPages()-$links_per_page_half) {
         return
$this->totalPages()-$this->links_per_page+1;
      } else {
         return
$this->current_page-$links_per_page_half;
      }
   }

   function
getEndNumber() {
      if(
$this->totalPages() < $this->links_per_page) {
         return
$this->totalPages();
      } else {
         return
$this->links_per_page;
      }
   }

   function
getPagesNav() {
      
$nav='';
      for(
$i=$this->getStartNumber(); $i<$this->getStartNumber()+$this->getEndNumber(); $i++) {
         if(
$i!=$this->current_page) {
            
$nav.="<a href='?".$this->page_name."=$i".$this->url_string."'>$i</a>";
         } else {
            
$nav.="$i";
         }
         if(
$i!=$this->getStartNumber()+$this->getEndNumber()-1) { $nav.=$this->seperator; }
      }
      return
$nav;
   }

}

?>
Then i used the following to display my next/prev links.

PHP Code:
<?php

//Paging class
include("MySQLPagedResults.class.php");

//Database connection
include("db.inc.php");

//Instantiate a new instance of the class
$paging_results = new MySQLPagedResults("SELECT count(*) FROM livetronix_emails","page","",25,10,"<<","Previous","Next",">>"," | ");

//Make the variables from the class easier to read/use
$first_nav = $paging_results->getFirstNav();
$prev_nav = $paging_results->getPrevNav();
$next_nav = $paging_results->getNextNav();
$last_nav = $paging_results->getLastNav();
$pages_nav = $paging_results->getPagesNav();
$offset = $paging_results->currentOffset();
$results_per_page = $paging_results->results_per_page;

//Loop through a table of our database using the $offset and $results_per_page variables from our class.
$sql = "SELECT * FROM livetronix_emails LIMIT $offset,$results_per_page";
$query = mysql_query($sql);
while(
$data = mysql_fetch_array($query)) {
   echo
$data[0] . ", ";
}

//echo out other variables from our class
echo "<p>$first_nav $prev_nav $pages_nav $next_nav $last_nav</p>";
?>

Last edited by Nate; 02-04-2004 at 10:15 PM.
Nate is offline   Reply With Quote
Old 02-04-2004, 10:20 PM   #2
planetsim
code | beer > sleep
 
Join Date: Sep 2002
Location: aus
Posts: 4,826
Havent tested but that doesnt look bad, ill test it later tonight to see if its portable for other users, if its portable its good.

Nice to see a constructor used, some new users to OOP dont use it. Its a little big however. But nice first work
__________________
Dont be lazy Search
And use the Manual
Webmobo - Open Source News Script | Portfolio / Blog | Against TCPA
planetsim is offline   Reply With Quote
Old 02-04-2004, 10:33 PM   #3
Nate
2+2=5
 
Join Date: Aug 2002
Location: Washington
Posts: 169
Thanks for the comments. Will be eager to hear your results tonight.
Nate 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 12:15 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.