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 trial

PHP Integrating PHP with Databases Using Relational Tables Fetching Many Relationships

Fetching 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.

index.php
<?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"];
}

Tagging Alena Holligan to review if this challenge needs more testing

Alena Holligan
Alena Holligan
Treehouse Teacher

close :) 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
Adebayo Ojo
23,661 Points

Please help me with this challenge. I followed your code above but still giving me a Bummer!

Alena Holligan
Alena Holligan
Treehouse Teacher

Adebayo Ojo check out the code in the "best answer" from this post. Directly below this comment.

3 Answers

Almost. 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"];
}

I knew I was close! haha thanks!

Hi 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"];

Was this what you had in mind, Alena Holligan?

while($row = $results->fetch(PDO::FETCH_ASSOC)) {
  $item["genres"][$row["genre_id"]] = $row["genre"];
}

Ups, sorry... hadn't realized that Jason Anello had already posted the same answer... I would upvote yours if I could, Jason.

Hi 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
Dan Avramescu
11,286 Points

Way 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"]; 

?