Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Integrating PHP with Databases Using Relational Tables Fetching Many Relationships

Completely confused on how to add a secondary array for this exercise.

Below is output from a series of var_dumps I added to the program.

ROW: array(8) { ["media_id"]=> string(1) "3" ["title"]=> string(50) "Refactoring: Improving the Design of Existing Code" ["img"]=> string(25) "img/media/refactoring.jpg" ["genre_id"]=> string(2) "17" ["format"]=> string(9) "Hardcover" ["year"]=> string(4) "1999" ["category"]=> string(5) "Books" ["genre"]=> string(4) "Tech" }

GENRES array(1) { [17]=> array(1) { [0]=> string(4) "Tech" } }

ROW: array(8) { ["media_id"]=> string(1) "3" ["title"]=> string(50) "Refactoring: Improving the Design of Existing Code" ["img"]=> string(25) "img/media/refactoring.jpg" ["genre_id"]=> string(2) "51" ["format"]=> string(9) "Hardcover" ["year"]=> string(4) "1999" ["category"]=> string(5) "Books" ["genre"]=> string(8) "Business" }

GENRES array(2) { [17]=> array(1) { [0]=> string(4) "Tech" } [51]=> array(1) { [0]=> string(8) "Business" } }

ITEM array(8) { ["title"]=> string(50) "Refactoring: Improving the Design of Existing Code" ["category"]=> string(5) "Books" ["img"]=> string(25) "img/media/refactoring.jpg" ["format"]=> string(9) "Hardcover" ["year"]=> string(4) "1999" ["publisher"]=> string(27) "Addison-Wesley Professional" ["isbn"]=> string(14) "978-0201485677" [0]=> array(2) { [17]=> array(1) { [0]=> string(4) "Tech" } [51]=> array(1) { [0]=> string(8) "Business" } } }

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.
 */

$genres = array();
while($row = $results->fetch(PDO::FETCH_ASSOC)) {

echo ("ROW:");
?> <br> <?php
var_dump($row);
?> <br><br> <?php

  $genres[$row["genre_id"]][] = $row["genre"];
  /*$item["genres"][] = $row["genres"]["genre_id"];*/
  /*$item["genres"][] = $genres;*/
  /*$item["genres"[$row["genre_id"]]][] = $row["genre"]; */
  /*$item[$genres[$row["genre_id"]]][] = $row["genre"]; */

echo ("GENRES");
?> <br> <?php
var_dump($genres);
?> <br><br> <?php

}

$item[] = $genres;

echo ("ITEM");
?> <br> <?php
var_dump($item);
?> <br><br> <?php

?>

Thanks a lot.

1 Answer

The challenge wants you to use the the variable specified ($item). All you need to do is the following:

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

I hope this helps.