calculate($howmany, $i_want, $page+ or - 1); # # 4) $where - This is the file that the next an previous buttons point to. # # 5) $how - What method to use. Get or Post. # # 6) $SQL - To internalized first, then written out to the form second. This way, you don't have # to do it. # # 7) $debug - Passed to the constructor, it controls debugging output. # # # The Variables in the calculate function (what and why) # # $page - Explained above # $next_views - Used mainly by the header() function, it is the high number # displayed. In this example, "Displaying 10 through 44 of 62...", # the number 44 is the variable $next_views. # $first_view - Used by the limit() function. It is the first number telling the # db where to fetch from in a result set (Based on my understanding # and use of SQL). # $low_num - The exact opposite of $next_views and used by the same function. # $left_overs - Used in conjunction with $penultimate, it helps the class determine # the last page. # $penultimate - Ah yes, I remember this one one well. You'll find it referenced in # the header() and buttons() functions both. Used by both to help # determine wether you are indeed on the page before the last, or on the # last page if $left_overs equals 0. # $last_page - Used by header() and buttons(), it represents the number of results to display # on the last page. If $last_page equals 0, then $i_want obviously # divided up $howmany with no remainders making $penultimate the last # page. # $howmany - Explained above. # $i_want - Explained above. # $total_pages - If you can't figure this one out, are you sure you should be programming? :) # class PreNext { # This is the heart of of the code controlling next and previous access # to results returned from a database. # setup result listing controls # Member vars. See above for description of each. var $page; var $next_views; var $first_view; var $low_num; var $left_overs; var $penultimate; var $last_page; var $howmany; var $i_want; var $total_pages; var $where; var $how; var $payload; ######################################################################################### # PreNext() # # This is the obviously the constuctor. At this point in time, it requires a variable. # That being $debug. What is easiest is to call the class as such # # $nav = new PreNext(yes); # # Instead of adding the variable, we added the value we wanted passed. In this case, # yes, in the interest of debugging. In the future, a second arg may be introduced to # to ascertain the database in use. There is no output. ######################################################################################### function PreNext($debug) { # Used for debugging. If yes, debugging is on. $this->debug = $debug; unset($debug); } # End Constructor ######################################################################################### # calculate() # # This function takes the information provided and calculates what is needed to # continue showing results in the desired fashion. There is no ouptput. ######################################################################################### function calculate($howmany, $i_want, $page) { $this->page=$page; // You're kidding right!? $this->next_views=$i_want * $page; $this->first_view=$this->next_views - $i_want; $this->low_num=$this->first_view + 1; $this->left_overs=$howmany - $this->next_views; $this->penultimate=floor($howmany / $i_want); $this->last_page=$howmany % $i_want; $this->howmany=$howmany; $this->i_want=$i_want; # Get how many pages there are going to be. $this->total_pages=ceil($this->howmany/$this->i_want); if($this->debug=='yes') { # Echo for debugging echo ("

Output of the calculate() function

\n"); echo ("\$this->page = $this->page\n"); echo "
"; echo ("\$this->next_views = $this->next_views\n"); echo "
"; echo ("\$this->first_view = $this->first_view\n"); echo "
"; echo ("\$this->low_num = $this->low_num\n"); echo "
"; echo ("\$this->left_overs = $this->left_overs\n"); echo "
"; echo ("\$this->penulitmate = $this->penultimate\n"); echo "
"; echo ("\$this->last_page = $this->last_page\n"); echo "
"; echo ("\$this->howmany = $this->howmany\n"); echo "
"; echo ("\$this->i_want = $this->i_want\n"); echo "
"; echo ("\$this->total_pages = $this->total_pages\n"); echo "
"; } } # end calculate() ######################################################################################### # header() # # In the even that you wish to see something say "display n through n results", this is # is the member function to do it. Will be extended in time to allow the developer to # the ability to add his own text. But that's later. ######################################################################################### function header() { # Check to see if this is the last page or only page if ($this->page > $this->penultimate) { if($this->last_page == 1) { $header='Displaying the last result'; } elseif($this->howmany < $this->i_want) { $header='Displaying '.$this->howmany.' results'; } else { $header='Displaying the last '.$this->last_page.' results'; } } # Check for the first page elseif(($this->page==$this->penultimate) && ($this->left_overs=0)) { if($this->howmany==$this->i_want) { $header="Display $this->howmany results"; } } # If it is not the last page else { $header="Displaying $this->low_num through $this->next_views of $this->howmany results"; } return $header; } # end header() ######################################################################################### # new_limit() # # This is where the limit portion of the SQL statement is generated. Have this function # return the statement as a variable ($limit = $pre_next->limit()) then tack it onto # end of SQL statement and resend the query. ######################################################################################### function new_limit() { $limit='LIMIT '.$this->first_view.', '.$this->i_want; return $limit; } # end new_limit() ######################################################################################### # action() # # Some of this is information that is moved to the next page. Some if it is used in # existing page. $where and $how are used to build the buttons at the bottom of page. # $payload is tweaked and passed onto the next page. This member function has no output. # It's really just a way of internalizing certain bits of information. # # As can be seen below, it requires three arguments be passed. # 1) $where is what page the information is being sent to. # 2) $how is POST or GET. # 3) $SQL should be obvious. It's the SQL statement. Es no sorpresa grande. ######################################################################################### function action($where, $how, $payload) { $this->where=$where; $this->how=$how; $this->payload=$payload; } # End action() ######################################################################################### # random_access() # # After thinking a little about it, this should be pretty easy. Of course, after # setting out in WRONG direction, I got it figured out. What does this function do in a # nutshell? Well... # 1) Finds out howmany pages will be needed to show all results (Stored as $x) # 2) Prints out those numbers and creates links at the same time while providing all # of the information needed for the recieving page to process the request. # # What don't I like about this way of doing it? Everything is seen in the URL. # Therefore, I'm still game to a different way of doing things. ######################################################################################### function random_access() { # Tweaking this thing is turning out to be harder than I thought. However, I have an # idea that I think is going to work. What we do is create sets. We take the number # of results, the number of links to show, and the number of results to show per page. # With this information, we create a movable "glass" that highlights and displays # only the information we need. # This is the nunber of links to show if($this->i_want>$this->total_pages) { $links_shown=$this->total_pages; } else { $links_shown=$this->i_want; } # Set the window_position vars $window_position=$this->page; $window_position_max=(1+($this->total_pages-$links_shown)); # Here goes nothing if($this->page<$window_position_max) { $window_position_counter=$this->page; $prev_arrow=$this->page; } else { $prev_arrow=$window_position_counter=$window_position_max; } # Check to see if the number of links to show is greater than the total number pages if( ($links_shown*$this->i_want) >= $this->last_page ) { # If on the first page, do not show the "|<--" and "<<<<" arrows. if($this->page>1 && ($this->total_pages>$this->i_want)) { ?> |<--   where?page="; echo $prev_arrow-1; echo "&i_want=$this->i_want&howmany=$this->howmany&payload=$this->payload\" title=\"Previous\">\n"; echo "<<<<   "; } # Now loop through showing the links $counted=(($window_position_counter+$links_shown)-1); for( ; $window_position_counter<=($counted); ++$window_position_counter) { # This line builds the anchor tag (link). echo "where?page=$window_position_counter&i_want=$this->i_want&howmany=$this->howmany&payload=$this->payload\" title=\"Page $window_position_counter\">\n"; # Make it obvious what page we are on. if($window_position_counter==$this->page) { ?>  where?page="; echo $window_position_counter; echo "&i_want=$this->i_want&howmany=$this->howmany&payload=$this->payload\" title=\"Next\">\n"; ?>>>>>    -->|