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
Marcos Melone
5,220 PointsQuestion about a code challenge when arrays with PHP
The code block below displays a list of flavors available at an ice cream store. In this code challenge, we'll modify this block to use an array so that it’s easier to add a new flavor. Change the line that creates the $flavor1 variable so that it instead
I don´t understand: my code:
<?php
$flavors = array("chocolate","vanilla");
?>
<p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo "2"; ?> flavors of ice cream.</p>
<ul>
<li><?php echo $flavors[1]; ?></li>
<li><?php echo $flavors[2]; ?></li>
</ul>
18 Answers
Endrit Avdullahi
3,878 Pointslisting-inventory-items
Challenge task 6 of 7
Inside the foreach loop, we should only display one <li> tag for each flavor. The one list item should display the value for the array element that the foreach loop is considering. Remove one of the <li> elements, and change the value that gets displayed in the other to display the flavor.
<?php
$flavors = array("Chocolate","Vanilla","Cookiie Dough");
?>
<p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo "3"; ?> flavors of ice cream.</p>
<ul>
<?php foreach ($flavors as $flavor) { ?>
<li><?php echo $flavors[1]; ?></li>
<li><?php echo $flavors[2]; ?></li>
<li><?php echo $flavors[3]; ?></li>
<? } ?>
</ul>
It says: Bummer! One of the flavors from the array is not being displayed on the page.
Whats the problem ?
Thanks!
Marcos Melone
5,220 Pointsthanks man!
Marcos Melone
5,220 PointsNext, add a third element to the flavors array. Specify your favorite ice cream flavor for this third element (something other than Chocolate or Vanilla). that's the challenge...
Can you tell me what is wrong here please?
<?php
$flavors[1] = "Chocolate";
$flavors[2] = "Vanilla";
$flavors[3] = "Cookie Dough";
?>
<p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo "2"; ?> flavors of ice cream.</p>
<ul>
<li><?php echo $flavors[1]; ?></li>
<li><?php echo $flavors[2]; ?></li>
<li><?php echo $flavors[3] . " my favorite"; ?></li>
</ul>
Marcos Melone
5,220 Pointssorry about that I'm new here ... lol bad can you help me in this new issue?
Marcos Melone
5,220 PointsI'm from Brazil and my English can sometimes fall short ok?
Kevin Korte
28,149 PointsI think you're just getting a head of the curve here. Instead of adding array elements using an index, try just doing this:
$flavors = array("Chocolate", "Vanilla", "Cookie Dough");
Kevin Korte
28,149 Pointsdouble post again, sorry
Marcos Melone
5,220 PointsThen Kevin, I'm not knowing exactly how to declare the new value as my favorite ... sorry I'm struggling bad ...
Kevin Korte
28,149 PointsNo worries. I think the console is looking for your favorite, the third value to be declared inline with the other flavors.
Randy Hoyt
Treehouse Guest TeacherAt the end of Step 3, your code should look like this:
<?php
$flavors = array("Chocolate","Vanilla", "Strawberry");
?>
<p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo "2"; ?> flavors of ice cream.</p>
<ul>
<li><?php echo $flavor1; ?></li>
<li><?php echo $flavor2; ?></li>
</ul>
Marcos Melone
5,220 PointsI did this exact way it worked ...
Randy Hoyt
Treehouse Guest TeacherCool! Then you can move on to Step 4 next, right?
Marcos Melone
5,220 PointsThank's solved..
Endrit Avdullahi
3,878 PointsMarcos, yes you solved it but I have a problem with that task :) Instead of making a new post is just post a reply here
durul
62,690 Points<?php $flavors = array("Chocolate","Vanilla","Cookiie Dough"); ?> <p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo "3"; ?> flavors of ice cream.</p> <ul> <?php foreach($flavors as $flavor) { ?> <li><?php echo $flavors[1]; ?></li> <li><?php echo $flavors[2]; ?></li> <li><?php echo $flavors[3]; ?></li>
<?php }
?> </ul>
It says: Bummer! One of the flavors from the array is not being displayed on the page.
Whats the problem ?
Thanks!
Kevin Korte
28,149 PointsTry capitalizing Chocolate and Vanilla.
James Barnett
39,199 PointsMarcos Melone - Impressive you created a title that was more than 255 characters, the absolute max for title field.
Remember: The best titles are short yet descriptive.
Here's are some more tips on asking questions on this forum
Kevin Korte
28,149 PointsYou're welcome