This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
In the last video, we refactored our index page to pull only 4 random items from the database instead of pulling all items and allowing php to narrow down the list. This can really speed up our response time as our database grows. We never want to pull more information than you need. Let’s apply this same principle to the category pages. Instead of pulling all the items from the database, let’s pull only the items in a specific category.
Example Code
function category_catalog_array($category) {
include("connection.php");
$category = strtolower($category);
try {
$results = $db->prepare(
"SELECT media_id, title, category,img
FROM Media
WHERE LOWER(category) = ?
ORDER BY
REPLACE(
REPLACE(
REPLACE(title,'The ',''),
'An ',
''
),
'A ',
''
)"
);
$results->bindParam(1,$category,PDO::PARAM_STR);
$results->execute();
} catch (Exception $e) {
echo "Unable to retrieved results";
exit;
}
$catalog = $results->fetchAll();
return $catalog;
}
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
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