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 trialFabian Pijpers
Courses Plus Student 41,372 PointsLooping through the output Challenge Task 2 of 2
getting the podcast 'title' and 'website'.
<?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();
}
}
2 Answers
Dave StSomeWhere
19,870 PointsOne way to go would be to:
- Call the get_podcasts() function and capture the returned PDO query object (result set) that you created in step 1.
- Loop through the object (from step 1) using the php foreach method
- inside the loop display the title and website column values for the current row.
Are you having difficulty with one of these steps?
Fabian Pijpers
Courses Plus Student 41,372 PointsYes, when i do go through the foreach loop it still gives a faulty result.
Fabian Pijpers
Courses Plus Student 41,372 PointsHi 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.
Dave StSomeWhere
19,870 PointsDave StSomeWhere
19,870 PointsPlease include all your code including the foreach loop and we'll see if we can find the issue.