Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJohn Shockey
45,061 PointsFetching Many Relationships
Loop through the $results and add an additional "genres" element to $item. This "genres" element should have an internal associative array with the key of genre_id and the value of genre.
I don't know what I am doing wrong here. I was trying to follow the example in the video before the challenge.
<?php
include "helper.php";
/*
* helper contains the following variables:
* $item is an array that contains details about the library item
* $results is a PDOstatement object with our genre results.
*/
while($row = $results->fetch(PDO::FETCH_ASSOC)) {
$item["genres"[$row["genre_id"]]][] = $row["genre"];
}
Alena Holligan
Treehouse Teacherclose :) check you square brackets. They match up with the depth. [genres][genre_id][genre]
The only part you need to change is the square brackets right around the $row["genre_id"]
Adebayo Ojo
23,661 PointsPlease help me with this challenge. I followed your code above but still giving me a Bummer!
Alena Holligan
Treehouse TeacherAdebayo Ojo check out the code in the "best answer" from this post. Directly below this comment.
3 Answers
jcorum
71,830 PointsAlmost. Brackets not quite in the right place. The array is $item["genres"][]:
while($row = $results->fetch(PDO::FETCH_ASSOC)) {
$item["genres"][] = $row["genre_id"] = $row["genre"];
}
John Shockey
45,061 PointsI knew I was close! haha thanks!
Jason Anello
Courses Plus Student 94,610 PointsHi jcorum,
This code:
$item["genres"][] = $row["genre_id"] = $row["genre"];
does pass the challenge but it doesn't seem to meet the requirements. With a double assignment like that you're effectively not using the genre_id. The genre overwrites the genre_id and then the genre gets added to the genres array except not at the correct key. Since the key isn't specified it will be auto-incremented based off the largest integer index that has been used instead of using the genre_id for the key.
The following code is what I used:
$item["genres"][ $row["genre_id"]] = $row["genre"];
Petrov von Petrov
21,916 PointsWas this what you had in mind, Alena Holligan?
while($row = $results->fetch(PDO::FETCH_ASSOC)) {
$item["genres"][$row["genre_id"]] = $row["genre"];
}
Petrov von Petrov
21,916 PointsUps, sorry... hadn't realized that Jason Anello had already posted the same answer... I would upvote yours if I could, Jason.
Jason Anello
Courses Plus Student 94,610 PointsHi Petrov,
Sorry I'm getting to this so late. I believe your code should be correct. I went ahead and upvoted it.
I created this script here to illustrate the difference between the 2 solutions here.
<?php
$item = array("genres" => array(3 => "Comedy", 16 => "Sci-fi"));
echo "<pre>";
var_dump($item);
echo "</pre>";
$row = array("genre_id" => 5, "genre" => "Action");
$item["genres"][$row["genre_id"]] = $row["genre"];
$item["genres"][] = $row["genre_id"] = $row["genre"];
echo "<pre>";
var_dump($item);
echo "</pre>";
This is the output:
array(1) {
["genres"]=>
array(2) {
[3]=>
string(6) "Comedy"
[16]=>
string(6) "Sci-fi"
}
}
array(1) {
["genres"]=>
array(4) {
[3]=>
string(6) "Comedy"
[16]=>
string(6) "Sci-fi"
[5]=>
string(6) "Action"
[17]=>
string(6) "Action"
}
}
With your code the "Action" genre is added with a key of 5. With the double assignment code, the genre is added at an auto-incremented key value of 17, in this case, since 16 was the previous highest key used.
Dan Avramescu
11,286 PointsWay too late but haven't seen my solution posted yet so figured it can't do any harm. In my mind it makes more sense this way:
while($row = $results->fetch(PDO::FETCH_ASSOC)) {
$item["genres"][] += $row["genre_id"["genre"]];
}
@alenaholligan , can you please explain the difference between my solution and :
$item["genres"][ $row["genre_id"]] = $row["genre"];
OR
$item["genres"][] = $row["genre_id"] = $row["genre"];
?
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsTagging Alena Holligan to review if this challenge needs more testing