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

MingChiao Wu
MingChiao Wu
5,547 Points

oooo

???

index.php
<?php

include "helper.php";
echo '<pre />';
var_dump($item);
$gogo = $results->fetch();
var_dump($gogo);

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

//$result.bindParam(1, )
/*
 * 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.
 */

2 Answers

Algirdas Lalys
Algirdas Lalys
9,389 Points

Hi MingChiao Wu,

This challenge might be complicated. But I tryed to comment out what's happening.

<?php
print_r($item);
// We loop through every record as long as there is one and we will store it in temporary variable $row
while($row = $results->fetch(PDO::FETCH_ASSOC)) {
  // We add associative array key 'genres' and inside we add new array with key 'genre_id' and value 'genre'
  $item['genres'][$row['genre_id']] = $row['genre'];
}
// Then we can check how our $item array looks now in the preview.
print_r($item);
?>

If you want to understand deeper, you can check out this answer . Happy Coding:)

MingChiao Wu
MingChiao Wu
5,547 Points

Thanks Algirdas, I just want to access the discussion panel, but forgot to remove the question, somehow I don't get the meaning of question. Thanks a lot!