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

General Discussion

Krystel Hagan
Krystel Hagan
4,731 Points

Listing Inventory Items Code Challenge pt 5

Q: Right now, the unordered list does not show the flavors from our array. In the next few tasks, we'll change that. This task has two steps. (1) Add the start of a foreach loop with an opening curly brace after the opening <ul> tag but before the first opening <li> tag. The foreach loop should load each element from the flavors array, one at a time, into another variable named flavor. (2) Add the closing curly brace for the foreach loop after the final closing </li> but before the closing </ul>.

A: ''' <?php

$flavors=array("Chocolate","Vanilla","Rocky Road");

?> <p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream.</p> <ul> <?php foreach ($flavors as $flavor) { <li><?php echo $flavors[0]; ?></li> <li><?php echo $flavors[1]; ?></li> <li><?php echo $flavors[2]; ?></li> } ?> </ul> '''

I am not sure what I am doing wrong?

5 Answers

Tong Vang
Tong Vang
9,926 Points
$flavors = array();

<?php foreach($flavors as $flavor) {
echo $flavor;
}
?>

It's not $flavors but $flavor. As it loops the $flavors array turns into a $flavor variable. You echo out the variable as it goes through the loop.

Krystel Hagan
Krystel Hagan
4,731 Points

''' <?php

$flavors=array("Chocolate","Vanilla", "Rocky Road");

?> <p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream.</p> <ul> <?php foreach($flavors as $flavor) { <li>echo $flavor[0];</li> <li>echo $flavor[1];</li> <li>echo $flavor[2];</li> } ?> </ul> '''

Sill isnt working :(

Tong Vang
Tong Vang
9,926 Points
<?php

$flavors = array("Chocolate", "Vanilla", "Rocky Road");

foreach($flavors as $flavor) {
    echo $flavor . " ";
}
?>

Just copy and paste. It works.