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

The company you work for wants to send an email to its member base. They want the email to contain a complementary offer

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.

Help please

index.php
<?php
include "helper.php";
$results->fetchaLL(PDO::FETCH_NUM);
/* 
 * helper.php contains
 * $results->query("SELECT member_id, email, fullname, level FROM members");
 */
David Negron
David Negron
Courses Plus Student 3,669 Points

Depending on the fetch mode, you can loop through the results, which come back as an array, like so:

$members = $results->fetchAll(PDO:FETCH_ASSOC);
foreach ($members as $member) {
    // do something here with $member['email'], etc.
}

The structure of your data depends on the fetch mode. If you use FETCH_NUM, you'll get a numeric array. If you use FETCH_ASSOC, you'll get an array, with the column names from the db as keys.

7 Answers

simhub
simhub
26,543 Points

Hi J,

you could try this:

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

foreach ($user as $key)
{
    echo send_offer($key['member_id'],$key['email'],$key['fullname'],$key['level']);
}

Thanks - Just used this and it worked fine.

So annoying that the question says "Tip: we used this method in the last video", when all we did in the previous video was $catalog = $results->fetchAll();

All the code that successfully answered this isn't covered in this course :/

Ogün Murat Kara
Ogün Murat Kara
3,726 Points

treehouse why dont you stop asking questions you don't teach?

Alain Readman Valiquette
Alain Readman Valiquette
14,547 Points
$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);
}
Nikifor Nikolov
Nikifor Nikolov
8,593 Points

Actually this is manipulating with arrays. You should know that. If not you could check again Basic PHP course (https://teamtreehouse.com/library/php-arrays-and-control-structures)

I understand what the loop is doing and how the send_offer() function works, but I just don't understand the syntax. for $member[id], $member[email], $member[fullname], $member[level]. I understand what is happening and that [id], [email], [fullname], [level] point to the columns we've selected in the SELECT statement. I just don't understand the syntax nor recall learning this?

  1. Why the square [ ] brackets ?
  2. Why is these text in the square brackets not not in quotations? ie.) [" "]

Revisiting this a year later with a better understanding, it's really quite simple. Try not to overthink it.

  1. $members is an associative array filled with objects.
  2. Each $member object is an Associative Array containing key / value pairs
  3. Square brackets [] are how we access values of indexed array items

In other words....

$member['email'] would access a value of the particular instance of the object it is looping thru. It would return something like "myname@email.net"

Alain Readman Valiquette
Alain Readman Valiquette
14,547 Points

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

Alain Readman Valiquette
Alain Readman Valiquette
14,547 Points

''' $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); } '''