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

https://teamtreehouse.com/library/integrating-php-with-databases/querying-the-database-with-php/working-with-results

I am stuck at this question. I have no idea how to answer it and altyhough it says the previous video can help, it doesnt seem to address this question at all.

I have skipped past this question but now find its raising its head again further on.

codeschool have a feature where you progressively receive tips leading you towards the correct answer. this way you get some sort of idea whats required.

at present I am totally stumped and have to rely on a forum to help.

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

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

4 Answers

Joel Bardsley
Joel Bardsley
31,249 Points

Hi John,

It was a tricky one compared to other challenges leading up to it, and the question probably makes it sound more intimidating than it really is. Hopefully this will help you get your head around it:

We already know that $results is a PDOStatement object, so that means we have access to the PDOStatement Methods by using $results->methodName - the one Alena used to retrieve the results in the previous videos was fetchAll, which had a fetch style parameter of PDO::FETCH_ASSOC to retrieve the results indexed by column name instead of 0-index.

You’ll want to store these results in a new variable. Since the results are all members, you could do:

$members = $results->fetchAll(PDO::FETCH_ASSOC);

From there you could use a foreach loop on the $members array and create the necessary variables to be passed into the send_offer() function, i.e. $member_id = $member[‘member_id’]

You don’t need to worry about anything else to do with the send_offer() function - just call it within the loop with the four parameters as shown in the question.

Hopefully that’s helped you become unstuck, but if you’re still struggling let me know.

ok, I've retyped this in so many times that it might just stick!

thanks heaps, this was well worth while.

<?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);
}

ah ha! brilliant. yes that makes perfect sense. I was on the money with the pdo statement but totally at a loss how to extract all the necessary fields from the array. but from what you are suggesting, the foreach will allow me to extract more than one field at a time. what a powerful feature that is.

and yes, i can place that call to the send offer function in the loop.

Thank you so much Joel for taking the time to offer such a well written response.

Cheers, John

hmmm, i'm not having much success with the syntax of the foreach

<?php include "helper.php"; $members = $results->fetchAll(PDO::FETCH_ASSOC); foreach( $member_id = $member[‘member_id’], $email = $member[‘$email’], $fullname = $member[‘$fullname’] , $level = $member[‘$level’]) { send_offer($member_id, $email, $fullname, $level); }

Joel Bardsley
Joel Bardsley
31,249 Points

Here's a foreach reminder

Note that in the parentheses of the foreach loop you just need the array_expression as value i.e. $members as $member in order to iterate over the $members array. Then inside the loop you can set the variables you want and pass them into the send_offer() function.