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 trialDelroy Lucan Muyambo
5,445 PointsCRUD 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
<?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
Adam Ridgley
12,255 PointsI'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>';
}
jlampstack
23,932 PointsThis 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>";
Aaron Carpenter
14,223 PointsI 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!
Lars Hansen
Front End Web Development Techdegree Graduate 19,694 PointsI passed without the try/catch statement. It is probably good practice to include it either way though.
Rafael Felipe
6,058 PointsRafael Felipe
6,058 PointsYes, Thanks! he just accuses a simple error, solving only put a echo"<ul>" before and after than foreach.
Delroy Lucan Muyambo
5,445 PointsDelroy Lucan Muyambo
5,445 PointsThanks @Adam. it passed when i removed the <li> tags