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

Accounting for Empty Results Challenge

Stuck here. Where is the $flavors array that they are talking about?

Let's make sure that foreach loop executes correctly if the $flavors array is NOT empty. Add one element to the $flavors array with a value of 'Avocado Chocolate'.

//My Attempt on this challenge

    $recommendations = array();

?><html> <body>

<h1>Flavor Recommendations</h1>

<?php

            if ($recommendations != "") {
                if (!empty($recommendations)) {
                    echo '<ul class="recommendations">';
                    foreach ($flavors as $flavor) {

                    }
                    echo '</ul>';
                } else {
                    echo '<p>There are no flavor recommendations for you.</p>';
                }
            }

        ?>

</body> </html>

3 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Two things:

(1) You don't need both of these checks:

if ($recommendations != "") {
    if (!empty($recommendations)) {

The second check alone should be sufficient:

    if (!empty($recommendations)) {

(2) There is supposed to be code inside the foreach loop that displays the flavor. Did you remove that?

foreach ($flavors as $flavor) {

}

Does that help?

Hi, I am also having trouble with this code challenge. It shows a white screen with Flavor Recommendations in the preview, and the message is Bummer! The unordered list tags appear even when the array is empty. Place those tags inside a conditional that uses the empty function. Here is my code:

<?php

    $recommendations = array();

?><html>
<body>

<h1>Flavor Recommendations</h1>

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

Can anyone help?

Hi Kari

You create two ul tags which is one too many Delete the echo '<ul>'; line above the if clause.

What stage is that challenge in?

stage 5

this should help....

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