Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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];