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

Tong Vang
Tong Vang
9,926 Points

Challenge #3

Let's make sure that foreach loop executes correctly if the $recommendations array is NOT empty. Add one element to the $recommendations array with a value of 'Avocado Chocolate'.

I've used:

"<?php array_unshift($recommendations,"Avocado Chocolate"); ?>"

$recommendations = array() . "Avocado Chocolate";

It won't take them....

5 Answers

Stone Preston
Stone Preston
42,016 Points

your first one should have worked. just make sure you have it inside the first php block in the file and under the code that initialized the recommendations array

<?php

    $recommendations = array();
    array_unshift($recommendations,"Avocado Chocolate"); 
?>
Tong Vang
Tong Vang
9,926 Points

Did that before I started the discussion and no go...

Stone Preston
Stone Preston
42,016 Points

can you post all the code you have in the file. I just passed using that code above

Tong Vang
Tong Vang
9,926 Points
    $recommendations = array();
    array_unshift($recommendations,"Avocado Chocolate");
?>
<html>
<body>

    <h1>Flavor Recommendations</h1>
  <?php if(!empty($recommendations)) {?>
    <ul>
        <?php foreach($recommendations as $flavor) { ?>
            <li><?php echo $flavor; ?></li>
        <?php } ?>
    </ul>
  <?php exit;} ?>
  <p>There are no flavor recommendations for you.</p>
</body>
</html>```
Stone Preston
Stone Preston
42,016 Points

you did not do task 2 correctly and thats causing task 3 not to pass. You were supposed to use an else block and display that there are no recommendations paragraph within that else block. remove that exit statement and add an else block

<?php

    $recommendations = array();
    array_unshift($recommendations,"Avocado Chocolate");


?><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>
Tong Vang
Tong Vang
9,926 Points
    $recommendations = array();
        array_unshift($recommendations,"Avocado Chocolate");

?><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>```

Still said no.