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

Code Challenge: Using a Conditional in a foreach Loop

I'm getting an error on the final code challenge of the Enhancing a Simple PHP Application Module: http://teamtreehouse.com/library/programming/enhancing-a-simple-php-application/paginating-a-list-model-and-view/using-a-conditional-in-a-foreach-loop

My code produces an error where it only shows 1 item in-stock and not all. Any suggestions?

<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) {
        $inStock = $flavors["in_stock" === true];
        if ($inStock !== false) {
    echo $flavor["name"] . "\n";
         }
    return $inStock;
}

?></pre>

5 Answers

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

Change the if conditional to:

    if ($flavor["in_stock"] == true ){

Note how you wrapped the [] and used flavors instead of flavor.

Same result. No output. When I made the $inStock variable it actually produced one of the in-stock flavors just not all of them.

Thanks for the help!

I think you may be over complicating it.

Within your foreach loop create a conditional that checks whether or not $flavor["in_stock"] is true and then echo the

$flavor["name"].

Mike, you make a very valid point where I may be overcomplicating it but when I simplify the code it doesn't show anything at all:

<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 ($flavors["in_stock"] == true) {
    echo $flavor["name"] . "\n";
}

?></pre>

I also tried:

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

and had the same result.

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

You tried the above and it didn't work?

EDIT: Meant to reply to your comment. My bad.

Eureka!

foreach($flavors as $flavor) {
        $inStock = $flavors["in_stock"] == true;
        if ($inStock !== false) {
    echo $flavor["name"] . "\n";
         }
    return $inStock;
}

THAT WORKS!