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

Teslim Adeyemo
Teslim Adeyemo
21,275 Points

I have been stuck on this code challenge. What I am doing wrong please!

I have tried several ways of solving this challenge. But now I have to reach out to the community. I can't see what I'm doing wrong in the code I pasted can somebody help out? The question is: 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.'

index.php
<?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 } ?>
  <?php else {
   echo "<p>There are no flavor recommendations for you.</p>"; } 
 ?>
</body>
</html>
Teslim Adeyemo
Teslim Adeyemo
21,275 Points

Sorry, below is my code

<?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 } ?>
  <?php else 
   echo "<p>There are no flavor reccomendations for you.</p>";  } 
?>
</body>
</html>>
            ```
Paolo Scamardella
Paolo Scamardella
24,828 Points

What's your issue? As far I can see, you are not iteration over your array or not getting any results because your array is empty.

1 Answer

Sean T. Unwin
Sean T. Unwin
28,690 Points

So close. :)

The closing bracket is missing for the else statement.

I also noticed 'recommendations' is spelled incorrectly.

ProTip: When a challenge asks you to have a specific phrase in the answer, copy and paste it from the challenge request. :)

Teslim Adeyemo
Teslim Adeyemo
21,275 Points

Thanks so much. Though I added it, still can't solve the problem. Is there any other thing I should do? I actually, below is the code challenge I am trying to solve: 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.'

Sean T. Unwin
Sean T. Unwin
28,690 Points

I found another error - a typo. I have adjusted my original answer.

Teslim Adeyemo
Teslim Adeyemo
21,275 Points

Thanks man for the tips and for helping out. But am still stuck I did all you said yet I keep receiving the Bummer jest. I am really a Bummer, What a challenge!