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 trialJohnatan Guzman
Courses Plus Student 2,360 PointsExtra credit: how to do it?
I use this code in the shirt.php file
<?php
$id = $_GET["id"];
$product = get_product($id);
if(!isset($product)) {
header("location: " . BASE_URL . "shirts/");
exit();
}
and this in the products.php file
<?php
function get_product($shirt_id) {
if (isset($shirt_id)) {
$product_id = $shirt_id;
if(isset($products[$product_id])) {
return $product = $products[$product_id];
}
}
}
But it fails everytime, that is, it always goes back to shirts page, which means that the $product variable isnt set by calling my function in the shirt.php file. I come from javascript, and im quite confused to why my code isnt working.
1 Answer
Andrew Shook
31,709 PointsJohnatan Guzman, can you post all the code from the product.php file. I think the problem may be that your get_product function isn't able to access you products array.
Johnatan Guzman
Courses Plus Student 2,360 PointsOh silly me, I added the line $products = get_products_all() inside the get_product function; And now it works as intended! Thanks!
Andrew Shook
31,709 PointsNo problem, glad I could help.
Johnatan Guzman
Courses Plus Student 2,360 PointsI actually changed it now as a parameter
$products = get_products_all();
$id = $_GET["id"];
$product = get_product($id, $products);
that worked also
Aaron Munoz
11,177 PointsAaron Munoz
11,177 PointsI'm still trying to solve this with the best practices . It's still confusing to me how to separate controller from model. But if I'm looking at this correctly, the code at the top.
Should not be in the controller but rather in the model. Wouldn't you say?