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

CRUD Operations with php

Challenge Task 2 of 2

Loop through the PDOStatement Object returned by the function and display a list item containing both the podcast 'title' and podcast 'website'.

need help guys, i have been stuck on this one for a while now

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

3 Answers

I've not watched the video but you probably want to do something like this

<?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();
        }
}

$podcasts = get_podcasts();

foreach($podcasts as $podcast) {
echo '<li>' . $podcast['title'] . ' ' . $podcast['website'] . '</li>';
}
Rafael Felipe
Rafael Felipe
6,058 Points

Yes, Thanks! he just accuses a simple error, solving only put a echo"<ul>" before and after than foreach.

Thanks @Adam. it passed when i removed the <li> tags

This worked for me. This second part of this challenge was confusing because the way the question was worded I thought they wanted us to create the loop within the function, but later realized we can loop outside of the function.

function get_podcasts() {
    include 'connection.php';

    //add code here
    $podcasts = $db->query('SELECT * FROM Podcasts');
    return $podcasts;
}

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

I think this challenge has some difficulty recognizing li tags, and ul tags. When they were added, the exercise wasn't responsive. The following code passed:

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

    //add code here
        try{

        return $db->query('SELECT * FROM podcasts');

        } catch(Exception $e) {

            echo "Error!: " . $e->getMessage() ."<br />";

            return array();

        }

    $podcasts = get_podcasts();

    foreach($podcasts as $podcast) {
    echo $podcast['title'] . ' ' . $podcast['website'];
    }


}

Good luck!