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

Errors when following the code given (refactoring)

I received these errors when following randys code

Notice: Undefined index: sku in C:\xampp\htdocs\inc\products.php on line 8

Notice: Undefined index: img in C:\xampp\htdocs\inc\products.php on line 9

Notice: Undefined index: name in C:\xampp\htdocs\inc\products.php on line 9

I was following the refactoring tasks.

<?php

function get_list_view_html($product) {

    $output = "";

    $output = $output . "<li>";
    $output = $output . '<a href="' . BASE_URL . 'shirts/' . $product["sku"] . '/">';
    $output = $output . '<img src="' . BASE_URL . $product["img"] . '" alt="' . $product["name"] . '">';
    $output = $output . "<p>View Details</p>";
    $output = $output . "</a>";
    $output = $output . "</li>";

    return $output;
}

function get_products_recent() {
   $recent = array();
   $all = get_products_all();

    $total_products = count($all);
    $position = 0;


   foreach($all as $product){
    $position = $position + 1;
        if ($total_products - $position < 4) {
                $recent[] = $product;
        }
   } 
   return[$recent];
}

function get_products_all() {
    $products = array();
    $products[101] = array(
        "name" => "Logo Shirt, Red",
        "img" => "img/shirts/shirt-101.jpg",
        "price" => 18,
        "paypal" => "9P7DLECFD4LKE",
        "sizes" => array("Small","Medium","Large","X-Large")
    );
    $products[102] = array(
        "name" => "Mike the Frog Shirt, Black",
        "img" => "img/shirts/shirt-102.jpg",
        "price" => 20,
        "paypal" => "SXKPTHN2EES3J",
        "sizes" => array("Small","Medium","Large","X-Large")
    );
    $products[103] = array(
        "name" => "Mike the Frog Shirt, Blue",
        "img" => "img/shirts/shirt-103.jpg",     
        "price" => 20,
        "paypal" => "7T8LK5WXT5Q9J",
        "sizes" => array("Small","Medium","Large","X-Large")
    );
    $products[104] = array(
        "name" => "Logo Shirt, Green",
        "img" => "img/shirts/shirt-104.jpg",    
        "price" => 18,
        "paypal" => "YKVL5F87E8PCS",
        "sizes" => array("Small","Medium","Large","X-Large")
    );
    $products[105] = array(
        "name" => "Mike the Frog Shirt, Yellow",
        "img" => "img/shirts/shirt-105.jpg",    
        "price" => 25,
        "paypal" => "4CLP2SCVYM288",
        "sizes" => array("Small","Medium","Large","X-Large")
    );
    $products[106] = array(
        "name" => "Logo Shirt, Gray",
        "img" => "img/shirts/shirt-106.jpg",    
        "price" => 20,
        "paypal" => "TNAZ2RGYYJ396",
        "sizes" => array("Small","Medium","Large","X-Large")
    );
    $products[107] = array(
        "name" => "Logo Shirt, Teal",
        "img" => "img/shirts/shirt-107.jpg",    
        "price" => 20,
        "paypal" => "S5FMPJN6Y2C32",
        "sizes" => array("Small","Medium","Large","X-Large")
    );
    $products[108] = array(
        "name" => "Mike the Frog Shirt, Orange",
        "img" => "img/shirts/shirt-108.jpg",    
        "price" => 25,
        "paypal" => "JMFK7P7VEHS44",
        "sizes" => array("Large","X-Large")
    );

    foreach ($products as $product_id => $products) {
        $products[$product_id]["sku"] = $product_id;
    }

    return $products;
}
?>

1 Answer

Hi Mike Smith,

the error indicates that you try to access an undefined index and I think if you would replace sku, img and name with a product id you would receive no error and get back all the information for one product. You don't want that, but that will tell you that you are probably accessing the outer array which has no attributes named sku, img and name - but the inner array does.

Make sure that you really access the inner array. Check what argument you give to the function get_list_view_html().

I hope that helps a little bit. If you still need help feel free to ask again and it therefore would be good if you could also post the code where you call the get_list_view_html() function.

Hi Mario,

Id left a bit of code in the index.php page that should have been removed! Thank you for your help.

You are welcome Mike :)