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 to pass method in a statement?

I am on the quiz, and i cannot understand how to do it. from my understanding, if you want to get method of a object, you do a php $results->method(); but it is not happening. maybe i am doing something wrong or not understood the question.

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

/* 
 * helper.php contains
 * $results->query("SELECT member_id, email, fullname, level FROM members");
 */

2 Answers

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

You need a foreach statement & member assignments:

<?php
include "helper.php";

$members = $results->fetchAll(PDO::FETCH_ASSOC);
foreach ($members as $member) {
  $member_id = $member[member_id];
  $email = $member[email];
  $fullname = $member[fullname];
  $level = $member[level];

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

That is so high level, she didn't show this in the video or that example. can you explain to me with comment, what is going on?

Filipe Pacheco
seal-mask
.a{fill-rule:evenodd;}techdegree
Filipe Pacheco
Full Stack JavaScript Techdegree Student 21,416 Points

Thank you for the input @cynthiaarnow.

She kinda went through all of that. In the video before the challenge, Alena shows that you need to do something like the $members = $results->fetchAll(PDO::FETCH_ASSOC); to fetch the results as an array.

The challenge explanation is saying that you need to loop though the results, which means that now you need a foreach loop. When you do foreach($members as $member), the $member will become the array fetched from the sql query. It will look kinda like this:

$member = [
    "member_id" => "info", 
    "email" => "info", 
    "fullname" => "info", 
    "level" => "info" 
]

The member_id, email, fullname and level are now keys and their info are the values. We need to get their values, so for that you do $member["member_id"], $member["email"], $member["fullname"], $member["level"].

Since we already have the function and know what the parameters are, we just need to give the the values needed.

  $member_id = $member["member_id"];
  $email = $member["email"];
  $fullname = $member["fullname"];
  $level = $member["level"];

And now we just call the function.

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

So it becomes like Cindy showed.

<?php
include "helper.php";

$members = $results->fetchAll(PDO::FETCH_ASSOC);
foreach ($members as $member) {
  $member_id = $member["member_id"];
  $email = $member["email"];
  $fullname = $member["fullname"];
  $level = $member["level"];

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

Really good explanation Filipe, thanks. You explain it better than the teacher.