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 Build a Simple PHP Application Working With Functions Displaying Only Four Shirts

8 Shirts still displaying

Having some trouble with this video.. All is working OK up to this point but the modification to the foreach loop which is meant to only display 4 shirts is not working. Here is my code.. What am I missing?

                <?php 

                $total_products = count($products);
                $position = 0;
                $list_view_html = "";
                foreach($products as $product_id => $product) {
                        $position = $position + 1;
                        if ($total_products - $position < 4) {
                            $list_view_html = $list_view_html . get_list_view_html($product_id, $product);
                        }
                    }
                    echo $list_view_html;
                ?>

Thanks for any help!

1 Answer

Hi Nic,

That code doesn't seem to be the problem. I copied and pasted it into the Project Files I downloaded and everything worked. The only difference between your code and the downloaded code is on the last line of the foreach loop and that didn't make a difference. The code below is from Treehouse. Good luck.

Jeff

<?php 

    $total_products = count($products);
    $position = 0;
    $list_view_html = "";
    foreach($products as $product_id => $product) { 
        $position = $position + 1;
        if ($total_products - $position < 4) {
            $list_view_html = get_list_view_html($product_id,$product) . $list_view_html;
        }
    }
    echo $list_view_html;
?>