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 trialSam Thompson
3,368 PointsStuck on wordpress plugin code challenge: Using the JSON feed in the comments and the existing loop, echo out each of the course names inside list items
On this challenge:
"Using the JSON feed in the comments and the existing loop, echo out each of the course names inside list items"
Im stuck, heres my code but it doesn't work:
<?php
/*
{
"name": "Username",
"profile_name": "profile_username",
"profile_url": "http://myapi.net/profile_username"
"courses": [
{ "name": "WordPress Plugin Development" },
{ "name": "WordPress Theme Development" },
{ "name": "Introduction to PHP" }
]
}
*/
?>
<ul>
<?php for( $i = 0; $i < count($my_plugin_profile->courses); $i++ ): ?>
<?php echo "<li> . $my_plugin_profile->{"name"} . </li>"; ?>
<?php endfor; ?>
</ul>
2 Answers
Chris Akers
2,527 PointsHi Sam Thompson,
In this challenge you'll be using the $i variable to loop through the courses array. So your echo statement should contain
->courses[$i]->
inside the list item. If you need a refresher on arrays or PHP we have: http://teamtreehouse.com/library/introduction-to-programming/objects-and-arrays/arrays
Jeremy Josey
15,941 PointsYou are looping through the courses array and then need to pull the name item in that array Try this<br/><br/>
<?php echo $my_plugin_profile->{'courses'}[$i]->{'name'}; ?>