Using Multiple Pages for Navigation - Displaying links
(Page 4 of 5 )
Now it gets a little more complicated. We don't want a "previous" link if we're on page 1. We also know anything less than page 1 has been given a value of 1 by notepad's script. If we are on page 2 or higher, we want a "previous" link. So let's take a look at the script and then break it down:
<?php if ($page > 1) { ?> <a href="<?=$PHP_SELF;?>?page=<?=($page - 1);?>">previous</a> || <?PHP } ?> |
What are we looking at? First we're saying to only run our script if the page is above 1, since we don't want a previous link on page 1. If the page is greater than 1, though, we're going to make a link called "previous" than point to this page, or $PHP_SELF, where the page is equal to the $page variable minus one. If $page was set to 2 and the file was, say, news.php, the output would be <a href="news.php?page=1">previous</a>. I like to add a little << before the link to make it graphically pleasing.
Now we do the same for the "next" link. Instead of subtracting 1, we're going to add 1. But when should we run it? Since we've set the number of pages total to $newspages, we should run it only when $page is less than $newspages.
<?php if ($page < $newspages) { ?> <a href="<?=$PHP_SELF;?>?page=<?=($page + 1 );?>">next</a> >> <?php } ?> |
Notice I added a >> as mentioned above. Between these links, typically, I like to see what items I'm viewing, so we need another line:
<?php echo "displaying items " . $realstart . " through " . $finish; ?> |
We've already defined what $realstart is. One page 1, it's going to be 1 and on page 2, (if your $display is set to 5) it will be 6. Sometimes, you might want to add the following line for good measure:
<?php echo "<br />" . $keys . " items total"; ?> |
$keys, of course, is the total number of items in the news file.
Next: Final Script >>
More Navigation Usability Articles
More By Codewalkers