Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

accounting for empty results 2 of 3 challenge task

If the array is NOT empty, the foreach loop will execute. But if it IS empty, we should display a message. Add an else block to the conditional that displays the following message in an HTML paragraph tag: 'There are no flavor recommendations for you.'

I knew there is barely to pass it.

<?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>
/* I added a paragraph */ 
   <p> <?php else { echo 'There are no flavor recommendations for you.'}; ?> </p>
<?php } ?>
</body>
</html>

the error said BUMMER! try again any idea??

7 Answers

Hugo Paz
Hugo Paz
15,621 Points
<?php if(!empty($recommendations)) { ?>
    <ul>
        <?php foreach($recommendations as $flavor) { ?>
            <li><?php echo $flavor; ?></li>
        <?php } ?>
    </ul>
<?php } ?> // You forgot to close the If
/* I added a paragraph */ 
   <?php else { ?>
       <p> <?php echo 'There are no flavor recommendations for you.'; ?> </p>
  <?php } ?>

David solution is correct, he just did not notice that the IF was not closed. I added it to the code.

David Omar
David Omar
5,676 Points

Oh yea good catch, think it would be better to put the closing brace on the same line as the else to have a little less code.

David Omar
David Omar
5,676 Points

edit: included the closing brace from the if statement pointed out by Hugo Paz

I think its because you are putting the whole else statement between <p> tags when you need to put them around what your echoing out

<?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>
/* I added a paragraph */ 
   <?php } else { ?>
       <p> <?php echo 'There are no flavor recommendations for you.'; ?> </p>
  <?php } ?>
</body>
</html>

gotcha it

You need to remove the curly brace that is right before the semi-colon.

you code is wont let us to pass it... I believe you forget add the close curly bracket after the else, so I put my code here

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

and it still said BUMMER! try again...

I have no idea whats wrong with it.

A couple comments to the original. Switch the semi-colon ; and closing curly brace } right after the end quote ' Also put the else block after the if block.

For code clarity, I also included the paragraph tags in the echoed html.

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

Sorry if my code shows up strangely.

Steven Walters
Steven Walters
10,573 Points

Why not stay in php?

<?php
if (!empty($recommendations)) {
    echo '<ul >';
    foreach ($recommendations as $flavor) {
        echo '<li>' . $flavor . '</li>';
    }
    echo '</ul>';
} else {
    echo '<p>There are no flavor recommendations for you</p>';
}
?>