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.
Johnatan GuzmanCourses Plus Student 2,360 Points
Can anyone explain how $product_id works?
I have been wondering from the beginning of it being introduced ($product_id) if it is a build-in php function (since $product_id is never explicitly defined in our project) and how it knows that it should refer exactly to $product and not for example $product.
Kevin Korte28,111 Points
I'm pretty sure I familiarized myself with this project to answer this.
$product_id is NOT a built in function or variable of PHP. It was defined in the
get_list_view_html function found in the products.php.
So with the
get_list_view_html function, we can pass it two values. Those two values passed will be stored inside the function as
How are those values set?
Well we know that in products.php, we set a products array and filled with array items. So if you look at shirts.php, we include the products page, and thus the products array at the very top. We know have that array information of all shirts, and we have access now to the
get_list_view_html function which also lives in the products.php file.
In shirts.php we run a foreach loop, that basically says take the entire $products array, and for each array in this array, set a key value pair where the key is the arrays index and name it $product_id, and the value is the array and name it $product.
Than inside of that for each loop, for each array item, we call the
get_list_view_html() function, and pass it this particular loops
At this point the function gets evaluated, where the
$product_id will be set in the
$output string, and the details of the array like the img url and the name are also available.
When the function is done, we echo it to the screen, and move on to the next array, rinse and repeat.
It know to be
$product and not
$product because we defined the array's indexs as 101, 102, 103 in the products.php file
You could change
$product_id to any valid PHP variable you want, you would just need to change that variable in the argument call in the
get_list_view_html function, inside the function where
$product_id gets evaluated, and in the foreach loop on shirts.php where we set
$products_id => $products and also just below where the function gets echoed with again the
$products_id variable as an argument.
That's a lot, but I hope that makes sense.
Leigh Maher21,830 Points
Great. Thanks for clearing that up, Kevin. It all makes sense to me now : )