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

Frank Hoke
Frank Hoke
8,651 Points

My code works ...but the challenge says "bummer", says I don't have Avocado Chocolate in my $recommendations array. Idea

<?php

$recommendations = array("Avocado Chocolate");

//echo '<h1>'.$recommendations[0].'</h1>';

?><html> <body> <?php if (!empty($recommendations)) { echo '<ul class="$recommendations">'; foreach ($recommendations as $flavor) { echo $flavor."<br>"; } echo '</ul>'; } else { echo '<p>There are no flavor recommendations for you.</p>'; } ?>

</body> </html>

index.php
<?php

    $recommendations = array('Avocado Chocolate');

    //echo '<h1>'.$recommendations[0].'</h1>';


?><html>
<body>
        <?php
                    if (!empty($recommendations)) {
                        echo '<ul class="$recommendations">';
                        foreach ($recommendations as $flavor) {
                            echo $flavor;
                        }
                        echo '</ul>';
                    } else {
                        echo '<p>There are no flavor recommendations for you.</p>';
                    }
        ?>

</body>
</html>
Frank Hoke
Frank Hoke
8,651 Points

Thank you Jeff! That makes sense.

1 Answer

Jeff Lemay
Jeff Lemay
14,268 Points

It seems you have removed the < li > tags from inside the foreach loop, this may be causing the challenge to fail. Also, I'm not sure which codeblock you are actually using, but you've altered some of the other code (like commenting out the < h1 >, and putting it in a php echo instead of just in html).

The following code worked to pass the challenge. I added comments to help show you what was updated during the challenge:

<?php
    // added Avocado Chocolate to recommendations array to make sure our if/else statement works
    $recommendations = array('Avocado Chocolate');

?><html>
<body>

    <h1>Flavor Recommendations</h1>
    <!-- added if statement to check if the array is empty, if it's not we'll execute the foreach loop -->
    <?php if(!empty($recommendations)) { ?>
    <ul>
      <?php
        foreach($recommendations as $flavor) { ?>
            <li><?php echo $flavor; ?></li>
      <?php } ?>
    </ul>
  <!-- closed if statement and initiated else portion where we print out a message -->
  <?php } else { ?>
   <p>There are no flavor recommendations for you.</p>
  <?php } ?>
</body>
</html>

FYI: I prefer to come in and out of php instead of using echo statements, I find it cleaner and easier to manage.