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

PHP Enhancing a Simple PHP Application Adding Search: Controller & View Accounting for Empty Results

Let's make sure that foreach loop executes correctly if the $recommendations array is NOT empty. Add one element to the

im stuck where did i go wrong because the <ul> tag is not being identified

index.php
<?php

    $recommendations = array('Avocado Chocoolate');

?><html>
<body>

    <h1>Flavor Recommendations</h1>

 <ul>

 <<?php
     echo '<ul>';
     if (!empty($recommendations)) {
        echo '<ul>';
        foreach($recommendations as $flavor) { ?>
            <li><?php echo $flavor; ?></li>
        <?php }
        echo '</ul>';
        } ?>
</ul>
</body>
</html>

2 Answers

Hi,

So I noticed you had a couple of problems in your code, I wrote my own version below. But where you mainly went wrong is you didn't put a comma in-between your array values. Also not sure how it would of displayed but you had <ul> within <ul>

<?php

    $recommendations = array('Avocado', 'Chocolate');


?>
<html>
    <body>

        <h1>Flavor Recommendations</h1>

        <ul>
         <?php  
         if (!empty($recommendations)) {
            foreach($recommendations as $flavor) { ?>
                <li> 
                    <?php echo $flavor; ?>
                </li>
            <?php }} ?>  
        </ul>

    </body>
</html>
Andrew Breslin
Andrew Breslin
10,177 Points

This code challenge was using 'Avocado Chocolate' as a single flavor, so that wasn't the problem (though that sounds a bit gross to me and I would both assume AND hope that they were separate flavors, but they aren't). The problem with this challenge is that that what you need to do to pass the challenge is NOT what the challenge asks you to do. It explicitly asks you to add a single element (avocado chocolate) to the array $recommendations if the array is NOT empty. Naturally that would mean that you will end up with either an empty array (if you started with an empty array . . . you are explicitly told to add to it only if it is NOT empty) or with an array with at least two elements in it (the one or more that was there before, plus the 'Avocado chocolate' you just added.)

But that's not really what they want. If you try to follow the original instructions you are told that you need to end up with an array with exactly one element in it, with the string 'avocado chocolate).

You don't actually have to mess about with the conditional or anything to pass the challenge, as you might expect from the way the question is worded. all you need to do is add a line somewhere inside the php tags before the conditional that says: $recommendations[]='Avocado Chocolate';

Not what you are originally asked for, but that's what the challenge appears to want.

Andrew Breslin
Andrew Breslin
10,177 Points

I stand corrected. I read the challenge wrong. It doesn't explicitly say to add to the array if the array is empty. It says "Let's make sure that foreach loop executes correctly if the $recommendations array is NOT empty. Add one element to the $recommendations array with a value of 'Avocado Chocolate'."

I read this as saying add to the array if the array is not empty. So anyway, mea culpa and again, all you have to do is just add that one line adding "avocado chocolate" to the previously empty array.