Multiple Pages of Data from a Text File - Navigating the Data
(Page 3 of 4 )
To create the navigation links for your pagination we will make a function called paginate(). The function should be available to every page which requires pagination, but doesn't need to be hard-coded. You could, for example, put the function in a file called "my_functions.php" and then include() it when needed.
The paginate function will be called with 3 variables that we've already defined in the previous section: $display, $pg and $total. The first thing we want to do inside the function is check whether or not any pre-existing query string variables exist and if so, retain them. For example you might have a news script which divides news items into categories, so you're already passing a catid=2 (which you need to keep). The following will setup our new query string:
<?php if (isset($_SERVER['QUERY_STRING']) && trim($_SERVER['QUERY_STRING']) != '') { if(stristr($_SERVER['QUERY_STRING'], 'pg=')) $query_str = '?'.preg_replace('/pg=\d+/', 'pg=', $_SERVER['QUERY_STRING']); else $query_str = '?'.$_SERVER['QUERY_STRING'].'&pg='; } else $query_str = '?pg='; ?> |
We first check if a pre-existing query string is available and actually has a value. If not, then our query string will simply be: ?pg=. If we do have a pre-existing value however, we then check to see if "pg" is already defined using the stristr() function. If "pg" is defined then we need to replace it (done with the regular expression, or preg_replace() function). We are just replacing it with a blank or empty value because we know that "pg" will always be the last (or only) variable in the query string. If "pg" is not defined (but another query string exists), then we simply append it to whatever value we're working with using the ampersand instead of a question mark: &pg=.
Now if we have:
news.php news.php?catid=2 news.php?catid=2&pg=4 |
Our $query_str variable will end up with:
?pg= ?catid=2&pg= ?catid=2&pg= |
Now that we've got our query string figured out, we need to know how many total pages of data we have to display. Using our variables from earlier, a little math, and rounding off with the ceil() function we end up with this:
<?php $pages = ($total <= $display) ? 1 : ceil($total / $display); ?> |
We're almost ready to start drawing the links, but before we do I'm going to create some variables for them so they are easier to work with later:
<?php $first = '<a href="'.$_SERVER['PHP_SELF'].$query_str.'1">&#171; </a>'; $prev = '<a href="'.$_SERVER['PHP_SELF'].$query_str.($pg - 1).'"> &#139;</a>'; $next = '<a href="'.$_SERVER['PHP_SELF'].$query_str.($pg + 1).'"> &#155;</a>'; $last = '<a href="'.$_SERVER['PHP_SELF'].$query_str.$pages.'"> &#187;</a>'; ?> |
Nothing complicated here. Each link references the current page using PHP_SELF, the first page is 1, the previous page is pg - 1, next page is pg + 1, and the last page is our total number of pages. The entity references are just prettier versions of the greater-than / less-than symbols.
Now, finally we can begin our navigation:
<?php echo '<div><p align="center">'; echo ($pg > 1) ? "$first : $prev :" : '&#171; : &#139; :'; ?> |
Above we open with a div tag, and draw the previous and next links only if we're not on the first page (otherwise we don't need them linked). Below is where we actually draw the page numbers. I've hard-coded my version to display a minimum of 5 links (if we're at the beginning or end of the total number of pages), or a maximum of 10 links (if we're somewhere in the middle of the total number of pages).
<?php $begin = $pg - 4; while($begin < 1) $begin++; $end = $pg + 4; while($end > $pages) $end--;
for($i=$begin; $i<=$end; $i++) echo ($i == $pg) ? ' ['.$i.'] ' : ' <a href="'. $_SERVER['PHP_SELF'].$query_str.$i.'">'.$i.'</a> '; ?> |
I'm basically just setting a $begin and $end variable ($pg + and - 4, plus the current page = 10 page links). I use the while() loops to make sure I'm not creating links to pages that don't exist. In the last line of code above, I draw links for every page in my range of page numbers, except for the current page which is instead placed inside brackets. Now we can close off the div:
<?php echo ($pg < $pages) ? ": $next : $last" : ': &#155; : &#187;'; echo '</p></div>'; ?> |
The only thing left to do is bundle everything we've covered into a function, and review.
Next: The Final Code >>
More Database Articles Articles
More By notepad