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

"Add one element to the $recommendations array with a value of 'Avocado Chocolate'." ?? How? (stage 5, challenge 3 of 3)

I have read all the other posts to the forum on this question, but none of them make sense to me! Could someone please give me a definitive answer as to what im doing wrong and what i must change.

Here is my code:

<?php

    $recommendations = array(1);


?>
<html>
<body>

    <h1>Flavor Recommendations</h1>

    <ul>
       <?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 } ?>
    </ul>

</body>
</html>
Hugo Paz
Hugo Paz
15,622 Points

I edited your post so its more readable. Robert Posted the correct answer.

2 Answers

Robert Walker
Robert Walker
16,514 Points

Hi Joshua,

What you need to do is actually pass the value of 'Avocado Chocolate' into the array like so:

$recommendations = array ('Avocado Chocolate');

Kevin Williams
PLUS
Kevin Williams
Courses Plus Student 19,952 Points

<?php

$recommendations = array();
$recommendations[] = 'Avocado Chocolate'; //this is a way to add one more element to the array

?>

<html> <body>

<h1>Flavor Recommendations</h1>

<?php

/* I found that adding additional closing and opening php tags around html markup a bit hard
   to read so, I refactored the code like this: (note comments are not needed) */

if(!empty($recommendations)) //checks if array is not empty
{
  echo '<ul>'; // start of unordered list

    foreach($recommendations as $flavors) // loops through array displays list of array items
    {
      echo '<li>' . $flavors . '</li>'; // outputs each array item in html list item tag within the unordered list
    }

   echo '</ul>'; //end of unordered list
}
else 
{
  echo '<p> There are no flavor recommendations for you.</p>'; 
}

?>

</body> </html>