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 Enhancing a Simple PHP Application Paginating a List: Model and View Using a Conditional in a foreach Loop

Krystel Hagan
Krystel Hagan
4,731 Points

Paginating a List: Model & View

Q: Right now, this block of code displays all five ice cream flavors that this store carries. Modify it to display only the flavors that are in stock.

''' <?php

$flavors = array(); $flavors[] = array("name" => "Cookie Dough", "in_stock" => true); $flavors[] = array("name" => "Vanilla", "in_stock" => false); $flavors[] = array("name" => "Avocado Chocolate", "in_stock" => false); $flavors[] = array("name" => "Bacon Me Crazy", "in_stock" => true); $flavors[] = array("name" => "Strawberry", "in_stock" => false);

foreach($flavors as $flavor => $value) { if ($flavor=> in_stock = true){ echo $flavor["name"] . "\n"; } }

?> '''

I am not sure what I am missing...

7 Answers

Gareth Borcherds
Gareth Borcherds
9,372 Points

Here's the full code.

<pre><?php

$flavors = array();
$flavors[] = array("name" => "Cookie Dough",      "in_stock" => true);
$flavors[] = array("name" => "Vanilla",           "in_stock" => false);
$flavors[] = array("name" => "Avocado Chocolate", "in_stock" => false);
$flavors[] = array("name" => "Bacon Me Crazy",    "in_stock" => true);
$flavors[] = array("name" => "Strawberry",        "in_stock" => false);

foreach($flavors as $flavor) {

  if($flavor["in_stock"] == true) {
    echo $flavor["name"] . "\n";
  }

}

?></pre>
Gareth Borcherds
Gareth Borcherds
9,372 Points

$flavor["in_stock"] == true goes in the if statement.

Krystel Hagan
Krystel Hagan
4,731 Points

its still not working :(

Gareth Borcherds
Gareth Borcherds
9,372 Points

Sorry also change your foreach to foreach($flavors as $flavor)

Krystel Hagan
Krystel Hagan
4,731 Points

Thank you, the only difference was the pre tag and a few formatting changes but I know some of these challenges are picky. :)

Melissa Venter
Melissa Venter
6,197 Points

make sure to remove your vardump at the end