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

Warren Johnson
Warren Johnson
6,870 Points

Code Challenge: Accounting for Empty Results (1 0f 3) - Can't "SEE" what I'm missing here...

The following code contains a foreach loop that displays a list of ice cream flavor recommendations. It works great when the $flavors array has at least one element, but we end up with some extra HTML when we don't have any recommendations. Write a conditional that makes sure the unordered list tags and the foreach loop get executed only if the array has at least one element.

my Code:

<html> <body>

<h1>Flavor Recommendations</h1>

<?php $recommendations = array();

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

</body> </html>

3 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Sorry for the confusion. The text should say "It works great when the $recommendations array has at least one element ... ." I must have changed the name of the array. :-( The conditional you want is this:

if (!empty($recomendations)) {

Does that help?

Randy did you know if you put a "1" in the array () it passes you for this test? Does that make any sense?

<?php

$recommendations = array(1);

?><html> <body>

<h1>Flavor Recommendations</h1>

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

</body> </html>

Ok, too many hours struggling with this. I understand the 2nd and 3rd part of this challenge, but I can't get it right on the 1st part. I understand the:

if(!empty($recommendations)) {

but I can't get past that. I tried:

$recommendations = array(); if(!empty($recommendations)) { echo '<ul>'; foreach($recommendations as $flavor) { echo '<li>'.$flavor.'</li>'; } echo '</ul>'; }

Beyond frustrated. Thank you in advance.

the <ul> and <li> didn't show up between the "

uhh, the unordered list and list items won't show up in code.

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

It looks like you have made a lot of changes to the code. Here's what it should look like at the end of step 1:

<?php

    $recommendations = array();

?><html>
<body>

    <h1>Flavor Recommendations</h1>

    <?php if (!empty($recommendations)) { ?>

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

    <?php } ?>

</body>
</html>

Haha, of course it was that simple. Thank you for the quick response Randy!

Warren Johnson
Warren Johnson
6,870 Points

Hey guys, I hate to tell you this, but...Jamil, YOUR POSTS ARE BEING SENT TO ME!!! - Warren Johnson.. AND... RANDY ,...YOUR RESPONSES TO JAMIL ARE FILTERING TO ME AS WELL!???

That's just how the forum works Warren. You will see all replies.

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Warren Johnson, Once you post a comment, you are automatically subscribed to the whole thread. If you want, you can come back to the thread and click Unsubscribe at the top to stop receiving notifications.

Hope that helps!