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
Greg Galus
13,228 PointsRefactoring the codebase- Extra Credit Exercise
Is there a finished version of the extra credit exercise in the Refactoring the codebase badge? I started it (my code so far is below to be in the products.php file), but I'm having a lot of trouble grasping what is supposed to happen. Where are we getting the shirtID? Is that the $product_id that I have in there now? I'm just a bit lost on it. Has anyone else tried it?
function get_product($product_id){ if(isset($_GET["id"])){ $product_id = $_GET["id"]; if(isset($products[$product_id])) { $product = $products[$product_id]; } } return $product; }
1 Answer
mind
8,633 PointsI'm calling the function in shirt.php and passing it the shirt ID as an argument:
$product = get_product( $_GET['id'] );
I have the following code in products.php:
function get_product( $id ) {
$products = get_products_all();
if ( isset( $id ) ) {
$product_id = $id;
if ( isset( $products[ $product_id ] ) ) {
$product = $products[ $product_id ];
}
}
if ( ! isset( $product ) ) {
header( 'Location: ' . BASE_URL . 'shirts/' );
exit();
}
return $product;
}