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

Warren Johnson
Warren Johnson
6,870 Points

Code Challenge: Accounting for Empty Results (3 of 3): Feedback needed: Does this even look close??

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

My Code:

<?php

$recommendations = array();

This is where I instantiated the array (Did I even have to do this???!

$flavors = array();

?> This is where I attempted to add the value to the array <?php $flavors[0] = "Avocado Chocolate"; ?>

<html>

<body>

<h1>Flavor Recommendations</h1>

<?php

if (!empty($flavors)) {   

echo '<ul> <?php foreach($recommendations as $flavor) { ?> <li><?php echo $flavor; ?></li> <?php } ?> </ul>'; } else { echo '<p>There are no flavor recommendations for you.</p>'; } ?>

</body> </html>

I have tried every single placement of the code, and it still just DOES NOT WORK...

8 Answers

My code for step 3 won't work, I've even gone so far as to use the code Randy supplied above, and even that will not work. Please advise!

Peter Hatzer
Peter Hatzer
20,837 Points

hey, I was having trouble with this one also but I just figured it out your trying add the 'Avocado Chocolate' to the $recommendations = array() like so :

$recommendations = array('Avocado Chocolate');
Igor Prymak
Igor Prymak
13,590 Points

Wow! This one really saves the day!

put this after creating the array:

$recommendations[] = "Avocado Chocolate";

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I apologize for the confusion! I said the array was called "flavors" in the text, but it's really called "recommendations." I'll get that fixed. I can tell you definitely understand the concept. The conditional should look like this:

if (!empty($recommendations)) {

Hey Randy. On this last challenge I erased the whole ELSE part of the conditional just so it displayed the "Avocado Chocolate" without the message of "There are no flavor recommendations for you". It let me pass this way, but is this the correct way of doing it?

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Hmmm ... that doesn't sound right. Can you post your code here so I can have someone take a look?

Once you add an element to the array, then the array should no longer be empty. If the conditional is set up correctly, then the message shouldn't be displayed. It should look something like this:

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

Does that make sense?

@Randy I was using $flavors as well took me a while to figure out after I looked at this forum.

I refactored the code so it wouldn't have to use so many opening and closing php tags like this:

<ul>
    <?php 
        if (!empty($recommendations)) {
            foreach ($recommendations as $flavor) {
        echo '<li>' . $flavor . '</li>'
      } else {
        echo '<p>There are no flavors that match your search</p>'
                } 
    } ?>
</ul>

It's not working. Is that because the code itself doesn't work or because the challenge wants me to write it the other way?

No, because the test requests that the unordered list has not to be printed when the array is empty. The way you did always print <ul></ul>.

Cody Craig
Cody Craig
10,326 Points

Is there a way to refactor it the way Web Admin tried to do? I am curious in this method as the one in the exercise is confusing to me.

This code below has worked for me <?php $recommendations = array('Avocado Chocolate'); if (!empty($recommendations)) { ?> <ul> <?php foreach($recommendations as $flavor) { ?> <li><?php echo $flavor; ?></li> <?php } ?> </ul> <?php } else { ?> <p>There are no flavor recommendations for you.</p> <?php } ?>