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 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>
Now that you've had a chance
to try this for yourself,
0:00
I'm going to show you how
I solved this challenge.
0:02
When it comes to programming, there's
always more than one way to do something.
0:06
If you solve this challenge
in a different way than what
0:10
I'm going to show you, great.
0:13
Now you'll have two ways
to accomplish this.
0:16
And, we'll be able to compare those
two options when you run into another
0:18
similar situation.
0:22
>> In the functions.php file,
0:23
I'm going to start by writing a new
function to retrieve an array of genres.
0:26
Let's add this just after
our single item array.
0:32
Then we can reference that
function when we needed.
0:34
We'll name this genre_array.
0:44
For this challenge we want to
pull all genre group by category.
0:50
But let's make this function
a little more flexible.
0:54
We may want to use this to
retrieve a single category.
0:56
So let's set up an optional parameter for
category.
1:00
Then, if we want to select only
the genre in a certain category, we can.
1:10
It's helpful to write things in such
a way that their use can be extended
1:15
in the future.
1:18
This helps you out later on.
1:19
We want to make sure that our
category variable is lowercase.
1:22
So let's add that to
the top of our function.
1:25
Next, we need to include
our connection.php file.
1:38
Now we're ready to start
our try catch block.
1:48
We'll start with the sequel variable.
1:52
This will be our main select.
1:58
We'll select genre and
2:00
category from genres.
2:08
We also need to join this.
2:15
We'll join our genre's
category on genre_id.
2:27
Now let's get the results for
all the genre.
2:41
We also want to order by genre,
so let's add that here.
2:48
When concatenating a string make
sure there are spaces where needed.
2:56
You can always add more spaces
without causing an issue, but
3:00
no spaces will run words together and
cause things not to work.
3:04
Finally we need to execute our results.
3:10
Now, we can add our conditional for
the category.
3:18
If (!empty($category)) {.
3:21
We'll duplicate this prepare line for
our else statement.
3:30
Now we can add our where clause.
3:39
We now need our bind statement to replace
our placeholder with our category.
4:02
This is a string, so
we'll use the string-filter.
4:21
We also have our else.
4:24
And our execute is down below our else.
4:26
So we're ready to finish off our try and
at our catch block.
4:29
Now I want to create an associative array.
4:48
With the key being the category and
4:50
the value set to an inner
array of category genre.
4:53
We'll use the fetch within a while loop so
we can format the results the way we want.
4:57
Let's start by initializing our array.
5:02
Then we can start the while loop.
5:11
We'll use our row to pull our category.
5:36
This will be our key.
5:44
Now we need to add an inner array.
5:46
This will add each genre to
the appropriate category.
5:55
We're now ready to return our genres.
5:59
Now we can use this function
to build our drop down.
6:06
So let's open suggest.php.
6:09
First we need to include
our function.php file.
6:13
Now we're ready to modify
our genre drop down.
6:24
Let's remove all the items except
one opt group and one option.
6:34
We'll use those as templates.
6:40
Before our optgroup,
let's open a php block.
6:59
We'll move this back a little.
7:07
Now let's start by creating a genre array.
7:12
We'll use our genre_array function.
7:18
These can have the same name because one
is a variable and one is a function.
7:23
Next we'll start looping
through the array with a for
7:27
each loop making sure that we
grab our key for the category.
7:31
Let's move this back so
it's easier to read.
7:51
We can then open our opt group,
using our category.
7:56
We need to escape our double quotes and
change books to category.
8:06
We need another for
each loop to loop through the options.
8:17
This time we don't care about the key.
8:20
Now we can use option
in our option template.
8:28
We don't need to open a php block,
because we're already in a php block.
8:45
So let's close this line and
we're ready for our if.
8:50
Instead of action here.
8:57
We also want to use our option.
8:59
So if our genre is set and
9:02
if genre equals our option then
we want to show this as selected.
9:04
We're ready to close our inner for each.
9:25
We can then end our opt group.
9:30
And finally we'll finish our outer for
each and end our php block.
9:39
Now let's preview this in the browser.
9:49
Our genre drop down is there and
if we choose a genre and
9:56
hit send our genre is selected.
10:01
Fantastic.
10:05
We've moved from all this code
10:07
to this code using a function
that connects to the database.
10:09
Although this didn't change
the functionality of our site,
10:14
now when we add or
10:17
update genres in our database, our suggest
form will be updated automatically.
10:18
You need to sign up for Treehouse in order to download course files.
Sign up