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 Testing Model Code

failure @ 03:16 (Testing Model Code)

I don't understand, why my browser is displaying:

Notice: Undefined variable: products in C:\xampp\htdocs\shirts\shirts.php on line 56

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\shirts\shirts.php on line 56

after deleting the $products = get_products_all(); function the variable in the code becomes undefined and the arguments invalid. Beside this nothing has changed in my code, as I've been following along stroke for stroke! At 3:16 in the video, Randy goes to his browser to load the "new" page 1 of his "Shirts listing page" where he gets 8 shirts displayed.

I've been through these videos over and again, comparing code from various exercise files and I just cannot find WHAT I MISSED! I figure I could move forward (as the code seems to change even more in upcoming videos to end this course) but this won't "fix" my current problem.

I'm not sure what I'm asking from anyone, just if anyone has noticed this too? I can't even think what code from what file to post here for evaluation.

Is Randy out there somewheres??

Thank you all, anyway, either way.

For starters why don't you copy and paste your code from say, lines 50 to 60?

Also, if you look just below the text box area you will see something that says Markdown Cheetsheet. Also, there is a post here titled: Posting Code to the Forum by Dave McFarland. If you don't understand that maybe this will make sense: How to display code at Treehouse.

I'll try this, this is from my SHIRTS.PHP file, lines 51 - 60

        <div class="wrapper">

            <h1>Mike&rsquo;s Full Catalog Of Shirts</h1>

            <ul class="products">
                <?php foreach($products as $product) {
                        echo get_list_view_html($product);
                    }
                ?>
            </ul>

(cool, now i can post code...) however, I'm pretty sure that whatever my problem is there will be barely a clue from the code I posted. I'll post more, from another location, if anyone has an idea where to look.

like I said, before i deleted $products = get_products_all(); from line 42 in the same file, I got a view of all shirts in the $products (array); afterward, i got the Notice & Warning in my browser about line 56: <?php foreach($products as $product) {.

3 Answers

Hi Byron,

Don't hold me to this but I believe $products = get_products_all(); was supposed to be changed to $products = get_products_subset($start,$end);. When a variable is used like $products is being used in the foreach loop it has to have been defined beforehand. Let me know what happens.

Jeff

You're right. And I thought so too; I mean, I knew the $products variable needed defining, and before this lesson it had been in the get_products_all() function. But right then, when Randy was finished testing his pagination code, he deleted the new $products = get_products_subset($start, $end) function and went straight to refresh his browser -successfully showing the selection of 8 shirts he intended- which i thought was bizarre because the $products variable now needed a value! That's what was blowing my mind, I thought it HAD to be defined somewhere I wasn't seeing.

As I suspected, by the end of this course, with new "partial" include files, my code is somehow straightened out and is now showing the selected shirts in the browser as it should. But I don't feel super-satisfied in understanding this accomplishment - so I'll search for associations, study line by line.

Thank You Jeff.

Bill Roberts
Bill Roberts
8,337 Points

If you look at the very next video around 0:23 you can see that the

$products = get_products_subset($start, $end)

is still there. I think what happened was Randy deleted that line of code by accident, but didn't save the deletion before refreshing his browser, hence why it still worked in the browser. I had an issue with this too, but it worked just fine when I put that line of code back in.

Eric Welch
Eric Welch
20,754 Points

confirmed. Oversight when the video was edited I guess

Aaron Munoz
Aaron Munoz
11,177 Points

Give Thomas Powers a raise!