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

Question 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>

http://teamtreehouse.com/library/programming/build-a-simple-php-application/listing-inventory-items/more-excitement-with-arrays

18 Answers

listing-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!

thanks man!

Next, 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>

sorry about that I'm new here ... lol bad can you help me in this new issue?

I'm from Brazil and my English can sometimes fall short ok?

I 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");

double post again, sorry

Then Kevin, I'm not knowing exactly how to declare the new value as my favorite ... sorry I'm struggling bad ...

No worries. I think the console is looking for your favorite, the third value to be declared inline with the other flavors.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

At 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>

I did this exact way it worked ...

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Cool! Then you can move on to Step 4 next, right?

Thank's solved..

Marcos, yes you solved it but I have a problem with that task :) Instead of making a new post is just post a reply here

<?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!

Try capitalizing Chocolate and Vanilla.

Marcos 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

You're welcome