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

Johnatan Guzman
PRO
Johnatan Guzman
Pro 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/");
        exit();
    }

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?

btw:

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