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

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.