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
Pagination allows us to split our results across multiple pages, making our results easier to read. In this way, we give the data to our visitors in bite size chunks instead of overwhelming them with items.
Example Code
catalog.php
$items_per_page = 8;
if (isset($_GET["pg"])) {
$current_page = filter_input(INPUT_GET,"pg",FILTER_SANITIZE_NUMBER_INT);
}
if (empty($current_page)) {
$current_page = 1;
}
$total_items = get_catalog_count($section);
functions.php
function get_catalog_count($category = null) {
$category = strtolower($category);
include("connection.php");
try {
$sql = "SELECT COUNT(media_id) FROM Media";
if (!empty($category)) {
$result = $db->prepare(
$sql
. " WHERE LOWER(category) = ?"
);
$result->bindParam(1,$category,PDO::PARAM_STR);
} else {
$result = $db->prepare($sql);
}
$result->execute();
} catch (Exception $e) {
echo "bad query";
}
$count = $result->fetchColumn(0);
return $count;
}
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
sorting the results in alphabetical order,
it's time to add pagination.
0:00
Pagination allows us to split our
results across multiple pages,
0:05
making our results easier to read.
0:09
In this way, we give the data to
our visitors in bite sized chunks.
0:12
Instead of overwhelming them with items.
0:16
The 36 items in our current
database may not be overwhelming.
0:18
But imagine hundreds or
even thousands of items.
0:23
Although the prospect of so
many items can be exciting, It can also be
0:26
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