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

Issue with codechallenge on search

What is wrong with this, can not pass last codechallenge

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

task is following The following code contains a foreach loop that displays a list of ice cream flavor recommendations. It works great when the $recommendations 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.

4 Answers

Hi,

Andrew is almost correct, he forgot a parenthesis after ($recommendations) in the if statement.

Jeff

<?php if(!empty($recommendations)) {?>
    <ul>
        <?php foreach($recommendations as $flavor) { ?>
            <li><?php echo $flavor; ?></li>
        <?php } ?>
    </ul>
  <?php }?>
Andrew Shook
Andrew Shook
31,709 Points

You're missing the closing "}" for your else statement. However you don't need the else statement so you should get rid of it. Other than that your code looks correct but I believe the code challenge is looking for the code in format like this:

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

Unfortunatelly, above answer did not pass also

guy this is frustrating