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

General Discussion

Challenge Problem

HI! I'm stuck at problem 2 of this challenge: http://teamtreehouse.com/library/enhancing-a-simple-php-application-2/adding-search-controller-view/accounting-for-empty-results

My code so far is this:

<?php

    $recommendations = array();

?>

<html>
<body>

    <h1>Flavor Recommendations</h1>

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

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

        <?php }

        ?>

    </ul>
<?php } ?>
</body>
</html>

and I just can't figure out what am I doing wrong. Wherever I put this else block I get the same answer that the array is empty... Is there anyone who can help me to get past this problem.

Thanks!

Andrej

6 Answers

Pol Martin
Pol Martin
8,200 Points

Hi Andrej,

you are following the right approach, but your else block is not in the right position.

Note that you want the 'ul' list and all its content to display only if the array is not empty, so your else block should be after closing the 'ul'.

Remember to put it inside the <?php ?> tags.

A couple of things:

  • Your else block isn't within <?php ?> tags, so won't do anything except print directly to the html.
  • At the moment, the else {} block is within the foreach, which won't work because else {} blocks should only follow the closing of an if {} block.

If you can resolve both of these, this snippet ought to work.

I tried this code right after the closing </ul> tag: <?php else { echo '<p>There are no flavor recommendations for you.</p>';} ?>

but still don't work.

Any ideas?

thanks

Hi Andrej,

I had similar problems getting this code to pass the challenge. I could get it working fine on my local server but not within the code challenge area. The way it got it to pass was by adding a value to the array; so $recommendations = array(); becomes something like $recommendations = array("one");

Hope this helps

Thanks for your help everyone, I passed this challenge...

Fabian Wilson
Fabian Wilson
5,076 Points

I found that this code works.

<?php
    $recommendations = array('Bannana');
?>


<html>
<body>
    <h1>Flavor Recommendations</h1>

    <ul>

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


`