Using Multiple Pages for Navigation - Setting up variables
(Page 3 of 5 )
Now let's jump right into navigation. The variable $start is a place marker in the array. Since we know PHP arrays start with "0," and we really don't want to call the first item "0," we have to get a number your users might feel more comfortable with. We can't assign the variable $start, since it's used further in the script, so since we really start counting at "1," let's just add 1 to the start point:
<?php $realstart = ($start + 1); ?> |
We'll use $realstart later, but first, we need to set some other variables. We know each page is going to display $display items, so we also know the last item on each page is going to be $start + $display. Remember, $realstart is just the number we are showing our users, not its actual value in the array, therefore we use $start instead of $realstart.
<?php //set last item number on this page $finish = ($start + $display); ?> |
Now we find out how many items exist in the file. In order to do this, we use the count() function. It's one of the easiest PHP functions and also one of the must useful.
<?php //find out how many items are in the array $keys = count($data); ?> |
Now that we know how many items are in the array ($keys), we can find out how many pages there should be. We can determine this by dividing the number of items by the number per page. Since there are $display items on each page, we can write this:
<?php //determine number of pages $newspages = ($keys / $display); ?> |
Now for some error prevention. If there are, say 28 items, page 3 will show 21-30. In order to fix this, we'll simply check the $finish variable. Since we know how many items are in the array, if $finish is greater than the count of the array, well reset $finish.
<?php //display max number correctly. if ($finish > $keys) { $finish = $keys; } ?> |
Next: Displaying links >>
More Navigation Usability Articles
More By Codewalkers