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

Intergrating PHP Databases Code Challenge HELP!

Ok so im over complicating this but I'm stuck on the foreach loop , the results I think i have got so any help would be welcomed

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

/* 
 * helper.php contains
 * $results->query("SELECT member_id, email, fullname, level FROM members");
 */
$results->fetchAll(PDO::FETCH_ASSOC);
foreach ($results){
    echo $member_id . $email . $fullname . $level;
};
Simon Coates
Simon Coates
28,694 Points

The code you're likely to see if doing a recap will likely be somewhat different to jcorum's code. As the PHP course is introductory, they'll probably do something like $rows = $results->fetchAll(PDO::FETCH_ASSOC); to use an associative array. What jcorum seemed to do is use the PDOstatement directly in the foreach (the PDOstatement class implements the traversable interface). I'm not sure the introductory course covers why this works. As such, something like the following is equivalent to jcorum's posted code, but maybe clings nearer to what your expecting.

<?php
include "helper.php";

$rows = $results->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $aRow){
  send_offer( $aRow['member_id'], $aRow['email'], $aRow['fullname'], $aRow['level'] );
}

4 Answers

Graham, something else is wrong, then, because I tested the code I sent you just before I sent it. Make sure you don't have some other code above or below what I sent except for this when you try to run it:

<?php
include "helper.php";

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

So it would seem using the $results->fetchAll(PDO::FETCH_ASSOC); threw it out

hmm ok maybe i need to rewatch the video or read the questions a little better as im sure it ask to use that method

Graham, I know how frustrating some of these challenges can be! In this case they say that there's a function you can use to send emails: the send_offer() function, and they tell you what the parameters are. So all you need to do is extract the id, email, name and level from the $results in a loop:

Here's one way:

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

But this is more compact, and saves the expense of creating 4 variables:

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

I just keep keeping the same error

Bummer! You need to pass all 4 arguments: member_id, email, fullname, level

on both examples

Im fetching all on the results using the PDO::FECTH_ASSOC but failing on the display

Simon Coates
Simon Coates
28,694 Points

i tried the first bit of code that jcorum posted and it worked fine. unsure why you'd be still having issues.

Simon Coates
Simon Coates
28,694 Points

foreach($array as $item) is the syntax. then the values used in the {} are on the $item, such as $item['title']. And the fetchall should probably be assigned to something - like $rows = $results->fetchAll(PDO::FETCH_ASSOC); so something like

$rows = $results->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row)
{
echo $row['keyname'];
}

If you need more help, just say and i'll post the exact code. (update: jcorum's code bypassed storing the results in a separate variable - i think the PDOStatement object is traversible and allows direct use in foreach.)

The foreach loop is the best way to go, however here is a for loop for to help understand what's going on:

<?php
$rows = $results->fetchAll();

for ($i = 0; $i < count($rows); $i++){
  $member_id = $rows[$i]['member_id'];
  $email = $rows[$i]['email'];
  $fullname = $rows[$i]['fullname'];
  $level = $rows[$i]['level'];
  send_offer($member_id, $email, $fullname, $level);
}