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

geoffrey
geoffrey
28,736 Points

Can't pass this exercise

I'm struggling to pass that simple exercise, I all the time have the answer that the array is empty but the message isn't displayed.

I've tried to format my code two ways and It's still not working. What am I missing ???

Here are the codes I've tried.

<?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>";
    }
?>

4 Answers

Ivan Uvarov
Ivan Uvarov
22,403 Points

This code works:

<?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 } else { 
      echo "<p>".'There are no flavor recommendations for you.'."</p>";
    }
        ?>

</body>
</html>

I just put together these 2 lines:

<?php } ?>
<?php else {

I did:

<?php } else {
Nicholas Olsen
seal-mask
.a{fill-rule:evenodd;}techdegree
Nicholas Olsen
Front End Web Development Techdegree Student 19,342 Points

Your first $recommandations is not spelt the same as your last $recommendations.

<?php

    if(!empty($recommandations)){  // $recommendations IS NOT SPELLED CORRECTLY
        echo "<ul>";
        foreach($recommendations as $flavor) { // THIS ONE IS SPELLED CORRECTLY
            echo "<li>".$flavor."</li>";
         } 
        echo "</ul>";
    }
     else
    { 
     echo "<p>"."There are no flavor recommendations for you"."</p>";
    }
?>
geoffrey
geoffrey
28,736 Points

Well spotted, but in fact, I had typed it correctly inside the exercise, I've just checked again. In case I had missed something else, I've also copied and pasted what you did, and It's still not working. Would you mind to try this on your side, to see if you can pass it ? (the first step is validated, that's at the second one that I get stuck).

Thank you for your help.

EDIT here is the other way I formatted my code, but doesn't work neither.

<?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>";
    }
?>
geoffrey
geoffrey
28,736 Points

Thank you ! Normally what I did should work as well, If I'm not wrong.. at least, I've passed :)