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

WordPress How to Build a WordPress Plugin Connecting WordPress Plugins with 3rd Party APIs Parsing JSON Code Challenge: Part 2

Zander Curtis
Zander Curtis
10,634 Points

What's Wrong With This Code?

Treehouse Community rocks! Can you help me on this one. I think I'm close :-)

plugin.php
<?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 $plugin_profile->{'courses'}[$i]->{'names'}; ?>
  }

  <?php endfor; ?>
</ul>

2 Answers

There's a few issues with your code.

  1. There's no need to have the curly braces where you have them. In that setup they would just be displayed as is and would not be doing anything.
  2. You are using $plugin_profile as your variable when it is actually $my_plugin_profile, so you are referencing the wrong variable.
  3. The challenge wants you to output each course name within a list item element, you can do this by echoing out the opening and closing li tags and concatenate them with the rest of your code.
  4. You are referencing the course names as "names" instead of "name"
 <?php echo '<li>'.$my_plugin_profile->{"courses"}[$i]->{"name"}.'</li>'; ?>
Henrik Hansen
Henrik Hansen
23,176 Points

Thanks! I tried:

<ul>
  <?php for( $i = 0; $i < count($my_plugin_profile->courses); $i++ ): ?> 

     <li><? echo $my_plugin_profile->courses[$i]->name; ?></li>

  <?php endfor; ?>
</ul>

Witch did not work in the challenge, but works in "real" code.

Zander Curtis
Zander Curtis
10,634 Points

mike02,

Thank you so much for taking the time to help me through this exercise. It means a lot :-)

ZanderCurtis