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.

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.