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

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 :)