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

not sure

not sure how they want me to loop through the array generated by the fetchAll

index.php
<?php
include "helper.php";
$results = $results-> fetchAll(PDO::FETCH_ASSOC);
var_dump($results);
for ($i = 0; $i < sizeof($results); $i++){
    foreach($results[$i] as $key => $notKey){
      send_offer($key);
    }
}
/* 
 * helper.php contains
 * $results->query("SELECT member_id, email, fullname, level FROM members");
 */

1 Answer

Once you get your results it should be a multi-dimensional array with the format as follows:

[0]=> array(4) {
    ["member_id"]=> string(1) "1"
    ["email"]=> string(23) "randy@teamtreehouse.com"
    ["fullname"]=> string(10) "Randy Hoyt"
    ["level"]=> string(6) "bronze"
}

After that iterating over it is up to you. I prefer foreach loops because there's one less operation to call (a count of the array to figure out where to stop). So for me the end result looks like:

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