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 with PHP: Part 1

Chris Andruszko
Chris Andruszko
18,392 Points

JSON index has changed?

This is less of a question and more of something I noticed. I thought I'd post it in case anyone else has this problem.

This is the code Zac wrote in this video:

<?php echo $wptreehouse_profile->{'badges'}[$i]->{'courses'}[2]->{'title'}; ?>

We're accessing the third item inside the "courses" array which displayed the course titles just fine in the video (around the 9 minute mark). But maybe something has changed to the JSON file because this wasn't working for me. I inspected the JSON and found that accessing the second item in the "courses" array properly displayed the course titles:

<?php echo $wptreehouse_profile->{'badges'}[$i]->{'courses'}[1]->{'title'}; ?>

In this case, to display the project name, I have to access the "courses" array's first item:

<?php echo $wptreehouse_profile->{'badges'}[$i]->{'courses'}[0]->{'title'}; ?>
Patrizia Lutz
Patrizia Lutz
1,449 Points

It seems that the JSON file structure has changed. There are now only 2 elements in the badges array.

It also looks like the badge->name and badge->url are the same as badge->courses[1]->name and badge->courses[1]->name.

stdClass Object
(
    [name] => The Name
    [profile_name] => misfist
    [profile_url] => http://teamtreehouse.com/misfist
    [gravatar_url] => https://uploads.teamtreehouse.com/production/profile-photos/3728252/thumb_my_censored_icon_red.png
    [gravatar_hash] => 55bdd3fc07d591629dda7496520628a3
    [badges] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 49
                    [name] => Newbie
                    [url] => http://teamtreehouse.com/misfist
                    [icon_url] => https://achievement-images.teamtreehouse.com/Generic_Newbie.png
                    [earned_date] => 2015-06-12T17:18:50.000Z
                    [courses] => Array
                        (
                        )

                )

            [1] => stdClass Object
                (
                    [id] => 2122
                    [name] => Introducing JavaScript
                    [url] => http://teamtreehouse.com/library/javascript-basics/introducing-javascript
                    [icon_url] => https://achievement-images.teamtreehouse.com/badges_JavaScript_Basics_Stage1.png
                    [earned_date] => 2015-06-12T18:04:58.000Z
                    [courses] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [title] => JavaScript Basics
                                    [url] => http://teamtreehouse.com/library/javascript-basics
                                    [badge_count] => 1
                                )

                            [1] => stdClass Object
                                (
                                    [title] => Introducing JavaScript
                                    [url] => http://teamtreehouse.com/library/javascript-basics/introducing-javascript
                                    [badge_count] => 1
                                )

                        )

                )

        )

    [points] => stdClass Object
        (
            [total] => 955
            [HTML] => 0
            [CSS] => 0
            [Design] => 0
            [JavaScript] => 685
            [Ruby] => 0
            [PHP] => 24
            [WordPress] => 215
            [iOS] => 0
            [Android] => 0
            [Development Tools] => 0
            [Business] => 0
            [Python] => 0
            [Java] => 0
            [Digital Literacy] => 0
        )

)

What I'm using is:

<?php foreach ($wptreehouse_profile->badges as $badge) { ?>
<li>
    <ul>
        <li>
            <img class="wptreehouse-gravatar" width="120px" src="<?php echo $badge->icon_url; ?>">
        </li>
        <li class="wptreehouse-badge-name">
            <a href="<?php echo $badge->url; ?>"><?php echo $badge->{'name'}; ?></a>
        </li>
        <li class="wptreehouse-project-name">
            <a href="<?php echo $badge->{'courses'}[0]->{'url'}; ?>"><?php echo $badge->{'courses'}[0]->{'title'}; ?></a>
        </li>
    </ul>
</li>
<?php }?>

1 Answer

Andrea R
Andrea R
10,520 Points

I noticed this too and took a slightly different approach

<ul class="wptreehouse-badges">
    <?php for( $i = 0; $i < 20; $i++ ): ?>
    <li>
        <ul>
            <li>
                <img width="120px" src="<?php echo $wptreehouse_profile->{'badges'}[$i]->{'icon_url'};?>">
            </li>
            <li class="wptreehouse-badge-name">
                <a href="<?php echo $wptreehouse_profile->{'badges'}[$i]->{'url'};?>"><?php echo $wptreehouse_profile->{'badges'}[$i]->{'name'};?></a>
            </li>
            <?php if(!empty($wptreehouse_profile->{'badges'}[$i]->{'courses'})): ?>
            <li class="wptreehouse-project-name">
                <a href="<?php echo $wptreehouse_profile->{'badges'}[$i]->{'courses'}[0]->{'url'};?>">
                    <?php echo $wptreehouse_profile->{'badges'}[$i]->{'courses'}[0]->{'title'};?>
                </a>
            </li>
        <?php endif; ?>
        </ul>
    </li>
    <?php endfor; ?>

</ul>