This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
We have our people and roles in the results object and we need to add them to the item variable so that we can return them back to our function call. We could use the fetchAll method here to retrieve all the authors, but they wouldn't be in the exact format we need. Because each author is a row in the database, fetchAll would give us a whole array for each author, instead of just a simple variable. If you want the results to be anything besides a simple array, you'll need to use a while loop to go through the results one at a time and format them as you go.
Example Code
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$item[$row["role"]][] = $row["fullname"];
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up