Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Getting Specific with Queries 4:38
- Filtering Data by Category 6:28
- Filtering Data 3 objectives
- Setting Up Pagination Variables 7:43
- Calculations and Links 5:32
- Variable and Calculations 5 questions
- Setting LIMITs 4:52
- LIMIT and OFFSET 2 objectives
- Adding Pagination Links 7:05
- Pagination Function 2 objectives
- Challenge 1:08
- Simplifying with a Function 10:24
- Pagination 5 questions

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Now that you’ve had a chance to try this for yourself, I’m going to show you how I solved this challenge. When it comes to programming, there is alway more than one way to do something. If you solved this challenge in a different way than what I am going to show you, GREAT! Now you’ll have two ways to accomplish this and will be able to compare those two options when you run into other similar situations.
Example Code
functions.php
function genre_array($category = null) {
$category = strtolower($category);
include("connection.php");
try {
$sql = "SELECT genre, category"
. " FROM Genres "
. " JOIN Genre_Categories "
. " ON Genres.genre_id = Genre_Categories.genre_id ";
if (!empty($category)) {
$results = $db->prepare($sql
. " WHERE LOWER(category) = ?"
. " ORDER BY genre");
$results->bindParam(1,$category,PDO::PARAM_STR);
} else {
$results = $db->prepare($sql . " ORDER BY genre");
}
$results->execute();
} catch (Exception $e) {
echo "bad query";
}
$genres = array();
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$genres[$row["category"]][] = $row["genre"];
}
return $genres;
}
suggest.php
<select name="genre" id="genre">
<option value="">Select One</option>
<?php
$genre_array = genre_array();
foreach ($genre_array as $category=>$options) {
echo "<optgroup label=\"$category\">";
foreach ($options as $option) {
echo "<option value=\"$option\"";
if (isset($genre) && $genre==$option) {
echo " selected";
}
echo ">$option</option>";
}
echo "</optgroup>";
}
?>
</select>
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Marcin Lipski
9,947 Points0 Answers
-
Paula Farrugia
12,923 Points0 Answers
-
Matt Nickolls
9,895 Points0 Answers
-
Liz Leith
4,886 Points1 Answer
-
Ksenia Breitenmoser
20,874 Points0 Answers
-
Martin Park
12,792 Points0 Answers
-
Zack Ball
4,958 Points1 Answer
-
Eric Schroeder
19,894 Points1 Answer
View all discussions for this video
Related 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