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!

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

Development Tools Using PHP with MySQL Querying the Database with PHP Avoiding Duplication

Johnatan Guzman
Johnatan Guzman
Courses Plus Student 2,360 Points

It doesnt show single shirts page anymore

I have this code in shirt.php

    $id = $_GET["id"];
    $product = get_product($id, $products);

    if(!isset($product)) {
        header("location: " . BASE_URL . "shirts/");

and this code in products.php

function get_product($shirt_id, $products){
    if (isset($shirt_id)) {
        $product_id = $shirt_id;
        if(isset($products[$product_id])) {
            return $product = $products[$product_id];

It worked before, but after including the database it always returns to the main shirts pages when clicking on a single shirt.

1 Answer

try some basic debugging & var_dump info to confirm

  1. check id exists
  2. check products exist
  3. go through get_products and see what happens here?


""" function get_product($shirt_id, $products){ if (isset($shirt_id)) { $product_id = $shirt_id; if(isset($products[$product_id])) { return $product = $products[$product_id]; } } } """

You don't need: $product_id = $shirt_id; just use shirt_id in the next statement

and instead of: return $product = $products[$product_id];

just do

return $products[$product_id];