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

Brenda Krafft
Brenda Krafft
18,036 Points

Notice: Undefined variable: product in C:\xampp\htdocs\shirts4mike\shirt.php on line 23

I'm working on "Building a Simple Website in PHP" tutorial and am at the stage where I am displaying all the t-shirts in the products array on the SHIRTS.PHP (plural) page.

When I click on a shirt, I'm supposed to go to the SHIRT.PHP (singular) page and display the shirt details, but I keep getting the above error.

I include the $products array by including the following, where the array is set up: <?php include('inc/products.php'); ?>

<?php
if (isset($_GET["id"])) {
    $product_id = $_GET["id"];
    echo $product_id;                  // Product ID shows up
    if (isset($products[$product_id])) {
        echo 'Products array is set';
        $product = $products[$product_id]; 
    }
    else {echo "\nArray not set";   var_dump($products);} 
     // The else executes, but the $products array dumps out just fine
} ?>

Line 23, which triggers the error is: $pageTitle = $product["name"];

After the first "if", I can echo out the $product_id, no problem.

When the second "if" fails, but the "else" executes, I can dump out the $products array just fine and see all the ids and names and so on.

So why isn't $products[$product_id] set?

Brenda Krafft
Brenda Krafft
18,036 Points

ARGH! Sorry, that code snippet is a mess and doesn't show how I'm including the products array.

Have to go pick up the kids.

Will make more readable later today.

... OK, came back and hopefully this makes more sense.