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 trialAmber Hoddinott
6,494 PointsPLEASE PLEASE!! Can someone explain this to me... i can't move forward with my learning until i understand this??!!
//code 1
<?php
$random = array_rand($catalog,4);
foreach($random as $id){ echo get_item_html($id, $catalong[$id]); } ?>
//code 2
<?php
$categories = array_category($section);
foreach($categories as $id){ echo get_item_html($id, $catalong[$id]); } ?>
//THE FUNCTION CODE
function get_item_html($id, $item){
$output = "<li><a href='#'><img src=' . $item["img"] . "'alt = $item["title"] . "'/> <p>view details</p> . </a></li>"; return $output; }
MY UNDERSTANDING... That the $id value (has avalue of the keys numbers 102, 103, 202 etc) is created as the foreach loop run's through the arrays , which is then used in the function get_item_html code in the parentheses.
What i don't understand??.... is where does the function get_item_html need the $id as in the functions code itself doesn't use the variable $id anywhere and how does this function know the $item variables value. i.e $item["img"] and $iteml["title"] as i can only see at the moment that the function has been passed the variable $id with the keys values.
i hope so much that im making sense, I've been trying to understand this all day and would appreciate so so much someone being able to help me understand this!
3 Answers
Matthew Brock
16,791 Pointsyou are correct that the function get_item_html does not use the variable id. It may be left over from a previous use of the function or she may be getting ready to use it later on.
The array_rand returns an array of keys. (ie. the 101,102,103 of the catalogs). when you call the function
get_item_html($id, $catalog[$id]);
it sends a reference to the catalog item say catalog[101]. (depends on the random id given). this array is what is used inside the get_item_html()
so would be like $catalog[$id]["img"] but now you can reference it as item["img"] because item now equals $catalog["id"]
get_item_html($id, $catalog[$id]);
function get_item_html($id, $item) { // item = $catalog[$id]
// $item["img"] would be the same outside this function as $catalog[$id]["img"]
}
Hope this helps.
Amber Hoddinott
6,494 Pointsthank you Matthew but im still a little confused.
i still don't understand how $caterlog[$id] is equal to $item?
i understand that array_rand() returns a set of keys which a made into an array of four keys i.e 102,104,105,106 being the arrays values.
what do you mean by...
"when you call the function get_item_html() its send a reference to the catalog item say catalog[101]. (depending on the random id give)" ....what id? i thought the function array_rand() returned an array of keys 102,103,105,106 how are they $ids?
"this array is what is used inside the get_item_html()" which array the array returned from the function array_rand() , if that is the case how can it be used in the get_item_html() function when to me i thought i only had an array of the keys in the array, how does that tell the function what items to display. which come back to the fact i dont understand how $caterlog[$id] is equal to $item which is need in the get_item_html to know which img and title to display.
i have a good understand of the rest of this section of the course, im just bout to start the forms part, so hope you dont think i dont have a clue about php. i think im probly just over thinking this and maybe looking at it all completely wrong way. Matthew thank you so much for trying to help me ive read you answer prob 50 times and im still confused?? maybe you or someone can explain it to me in a different way?
so basically i just need someone to explain to me where the function...
function get_item_html($id, $item)
is getting its $item value from as its clear me nowhere is the variable $item is being passed into it... you saying that $caterlog[$id] (which im assume that thats the variable $id is equal to $item but how?
thank you again and sorry im asking again
Sean T. Unwin
28,690 PointsThe $id
in the get_item_html
function will be used 4 lessons from this titled, Displaying Item Details. Be sure to read the Teacher's Notes for this lesson.
In the file, catalog.php
, the $item
value is passed from within the foreach
loop which get_item_html
is called from. Remember that $catalog
is name of the Array in data.php
:
<?php
$categories = array_category($catalog, 4);
foreach ($categories as $id) {
echo get_item_html($id, $catalog[$id]
}
?>