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

Jason Dittmer
Jason Dittmer
8,273 Points

There is an an associative array in the variable $results. I can’t figure out how to loop through the $results array and

Here is the code challenge questions: The company you work for wants to send an email to its member base. They want the email to contain a complementary offer based on their membership level.

There is a send_offer function ready for you to use that accepts the following arguments: $member_id, $email, $fullname, $level.

The data from the database is currently in a PDOStatement object named $results. Loop through those results and pass them to the send_offer function.

I can’t figure out how to loop through the $results array and pass the $member_id, $email, $fullname, $level to the send_offer function.

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

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

5 Answers

Simon Coates
Simon Coates
28,694 Points

It kinda defeats the purpose if i just post the code. There's no trick to it. get the associative array from the $results object (one of the fetch methods), use a foreach loop to iterate on each record with the function call for send in the foreach body. the parameters for the function call are just using array notation with the SQL fields as keys.

Simon Coates
Simon Coates
28,694 Points
<?php
include "helper.php";

foreach($results as $result){
 send_offer( $result['member_id'], $result['email'], $result['fullname'], $result['level']);  
}

the contents of $results are iterable allowing use of the foreach loop directly. Otherwise, can use the foreach on the array you get back via fetchAll:

<?php
include "helper.php";
$rows = $results->fetchAll();
foreach($rows as $result){
 send_offer( $result['member_id'], $result['email'], $result['fullname'], $result['level']);
}

or use a for loop:

<?php
include "helper.php";
$rows = $results->fetchAll();
for($i = 0; $i < count($rows); $i++){  
 send_offer( $rows[$i]['member_id'], $rows[$i]['email'], $rows[$i]['fullname'], $rows[$i]['level']);
}
Jason Dittmer
Jason Dittmer
8,273 Points

Thank you! Once I realized that I needed a foreach loop I was able to figure out the rest pretty quick.

John Lukacs
John Lukacs
26,806 Points

This is what I tried did not work Can you help here

<?php
include "helper.php";


$result->fetch(PDO::FETCH_ASSOC);
for(i=0; i < $results, i++){
  $results->query("SELECT member_id, email, fullname, level FROM members");
}
/* 
 * helper.php contains
 * $results->query("SELECT member_id, email, fullname, level FROM members");
 */
John Lukacs
John Lukacs
26,806 Points
<?php
include "helper.php";
$result->fetch(PDO::FETCH_ASSOC);
foreach($results as $result){

}//Loop through those results and pass them to the send_offer function.
//HOW do I pass them to the send offer funtion??

Help