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 Querying the Database with PHP Working with Results

How do you loop through the results array in "Working with Results" Challenge?

I am very lost here any help would be appreciated.

index.php
<?php
include "helper.php";

try {
    $results = $db->query(
        "SELECT member_id, email, fullname, level FROM members"
    );
} catch (Exception $e) {
    echo $e->getMessage();
}
//add code below this line
$members = $results->fetchAll(PDO::FETCH_ASSOC);
foreach($members as $key) {
  $member_id;
  $email;
  $fullname;
  $level;
  foreach($key as $v2) {
    foreach($v2 as $v3) {
      if ($v2 == 'member_id') {
        $member_id = $v3;
      } else if ($v2 == 'email') {
        $email = $v3;
      } else if ($v2 === 'fullname') {
        $fullname = $v3;
      } else if ($v2 == 'level') {
        $level = $v3;
      }
    }
  }
  send_offer($member_id, $email, $fullname, $level);
}

2 Answers

Amber Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Amber Stevens
Treehouse Project Reviewer

So you're overthinking it just a little bit. You're on the right track initially with the foreach loop, but then inside of it you declare the variables $member_id, $email, $fullname and $level but you don't ever set them to equal anything. What you need to do is set them each to equal a value - one that you would get just like if it was a regular array so for instance using the foreach loop you already started with:

<?php

foreach ($members as $key) {
      $member_id = $key['member_id'];
//and then so on for the other variables and THEN you'd pass them
// all into the send_offer function at the end :

              send_offer($member_id, $email, $fullname, $level);
}

Thank you Amber! You are the PHP G.O.A.T.