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 trialMichael Whalen
4,282 PointsCompletely 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" } } }
<?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
?>
1 Answer
Kyle McCullen
16,639 PointsThe 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.
Michael Whalen
4,282 PointsMichael Whalen
4,282 PointsThanks a lot.