Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Integrating PHP with Databases!
You have completed Integrating PHP with Databases!
Preview
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
So we'll take the total number and
divide it by the items per page.
0:00
We want this to return the next
highest integer value,
0:08
rounding up value if necessary.
0:12
There's a built in function to do this,
and it's called ceil.
0:14
So will surround our calculation
with a function ceil.
0:18
The final calculation we need is to
find out how many items to skip for
0:23
the current page.
0:27
Let's start with some notes.
0:29
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