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 CRUD Operations with PHP Creating Records Reading Data

Looping through the output Challenge Task 2 of 2

getting the podcast 'title' and 'website'.

index.php
<?php
function get_podcasts() {
    include 'connection.php';

    //add code here
    try {

    return $db->query('SELECT * FROM podcasts');
    } catch (PDOException $e) {
        echo "Error! " . $e->getMessage() . "</br>";
        return array();
        }   
}
Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Please include all your code including the foreach loop and we'll see if we can find the issue.

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

One way to go would be to:

  1. Call the get_podcasts() function and capture the returned PDO query object (result set) that you created in step 1.
  2. Loop through the object (from step 1) using the php foreach method
  3. inside the loop display the title and website column values for the current row.

Are you having difficulty with one of these steps?

Yes, when i do go through the foreach loop it still gives a faulty result.

Hi Dave,

I have already found the problem which is very obvious. The variable made $podcast = get_podcasts();

and the foreach loop

foreach(podcast as $item) { echo "<ul> <li>" . $item['title'] . " " . $item['website'] . "</i></ul>"; }

al this code should be outside the function. The function must be called upon from outside itself.