Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that we have all our basic parameters setup, we’ll need to do a couple calculations.
Example Code
$total_pages = ceil($total_items / $items_per_page);
//limit results in redirect
$limit_results = "";
if (!empty($section)) {
$limit_results = "cat=" . $section . "&";
}
// redirect too-large page numbers to the last page
if ($current_page > $total_pages) {
header("location:catalog.php?"
. $limit_results
. "pg=".$total_pages);
}
// redirect too-small page numbers to the first page
if ($current_page < 1) {
header("location:catalog.php?"
. $limit_results
. "pg=1");
}
//determine the offset (number of items to skip) for the current page
//for example: on page 3 with 8 item per page, the offset would be 16
$offset = ($current_page - 1) * $items_per_page;
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we have all our
basic parameters set up,
0:00
we'll need to do a couple calculations.
0:03
First, we want the total number of pages.
0:05
So we'll take the total number and
divide it by the items per page.
0:08
We want this to return the next
highest integer value,
0:17
rounding up value if necessary.
0:21
There's a built in function to do this,
and it's called ceil.
0:23
So will surround our calculation
with a function ceil.
0:27
The final calculation we need is to
find out how many items to skip for
0:32
the current page.
0:36
Let's start with some notes.
0:38
Remember to start comment lines
with double forward slashes.
0:40
We want to determine the offset which is
0:45
the number of items to skip for
the current page.
0:50
For example: [BLANK AUDIO]
1:00
on page 3 with 8 items per page,
1:05
the offset would be 16.
1:10
So let's calculator offset.
1:15
We'll use current_page- 1
1:18
multiplied by the items per page.
1:24
Make sure you add parentheses to
be sure your calculations are done
1:30
in the correct order.
1:33
So if our items_per_page is 8 and
we're on page 2,
1:40
we want to skip the first 8 items
that we already show on page 1.
1:44
2- 1 =1, multiplied by 8 = 8.
1:49
If we were on page 1,
we would see 1- 1 = 0,
1:56
multiplied by 8 still = 0.
2:01
So on our first page,
we don't want to skip any items.
2:05
If we have less items left
than the items per page,
2:09
it will show only the remaining items.
2:12
For example, if we have 15 items and
2:15
we show 10 per page, on the second
page we would only see 5 items.
2:17
Before we move on to using these
new variables to set our limits,
2:23
let's set up a couple redirects
if our page numbers aren't valid.
2:27
We're going to do two things,
right after we set our total pages.
2:31
Let's start with our comments again,
2:36
[BLANK AUDIO] redirect too-large
page numbers to the last page.
2:41
We also want to redirect too-small
page numbers to the first page.
2:50
Let's set a conditional for
our first redirect.
3:03
If the current page is
greater than our total pages,
3:10
[BLANK AUDIO] let's
change these to plural.
3:15
Total pages instead of total page.
3:23
Then we want to redirect
them to our catalog page.
3:25
We're going to pass our
page number of total pages.
3:33
For the second redirect,
3:45
if our ($current_page < 1)
3:49
we're going to redirect again.
3:54
This time setting our page to 1.
4:06
We also need to know if
we're on a category page.
4:09
So let's add a new
variable to limit results.
4:12
We'll start with a comment.
4:18
By default,
we don't want to limit our results.
4:25
So we'll initialize limit
results using an empty string.
4:28
Then, we add a conditional to check
if we are in a category page.
4:35
Using if (!empty($section)).
4:39
If we're on a category page, then we
wanna redirect them to a category page.
4:46
$limit._results = Don't forget
the ampersand at the end.
4:50
This will allow us to pass
the page number as well.
4:59
Now, we can add the limit_results
variable to our redirects.
5:03
Great.
5:27
All our variables are set and
we're ready to start limiting our pages.
5:28
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up