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 PointsI need some help understanding that last part fo this code please.
Ok so first i want to say im thinking that im over thinking this and im sorry if this end up being a silly question.
Firstly ill explain my understanding so far before i ask my questions just to be sure that im understanding this code correctly.
so the $random variable is holding a value of the new array which has been created by the array_rand() function. This function has the keys in this case 102,103,104 and 303 as it values.
when the foreach loop runs it gets in-turn as it runs through the array a variable name of $id. and this is where my understanding stops.
i guess i get that the foreach loop is echoing out the HTML in turn each time it hits the different values of the array, which with this makes the 4 random images ect appear in the browser, that i get. but how i dont get?
Dont understand it where the function_item_html() is getting this information from (which file?) i assume when it echos out that is making the function run? why do we need to pass in ($id,catalog $id) into the functions prentices.
how in having the values for example (102, catalog[102]) in the prentices making the function display the html.
i hope i have explained my understanding and my questions well enough. Thank you in advance to anyone who can help me understand this :)
2 Answers
Lyle Lewton
Courses Plus Student 10,585 PointsI would be willing to bet you are now well beyond having trouble with this code since this question is a year old, but I'm going to write out an explanation to help my understanding and to help anyone who stumbles upon this question.
$random is an array that will have 4 items, with the values set to 4 random keys from the catalog array. By removing "$item" from the foreach loop "$id" is now representing the value of the $random array instead of the key.
Then we pass the get_item_html function the values from $random (which is the 4 random keys from the catalog array) and the corresponding media item arrays from the catalog array. Then it just displays 4 random items in the same way it displayed all 12 before.
To explain this in more concrete terms, let's say that $random has 101 as it's first value and lets just analyze what happens with that first value. It would be in the array as 0 => 101 so I'm going to try to give a visual code representation of what's happening:
<?php
//$id would represent 101 since in the $random array it would be 0 => 101
foreach($random as $id){
echo get_item_html(101, $catalog[101]);
}
?>
These arguments get passed to the function from the functions.php file and the information for the arguments comes from the data.php file which contains the catalog array.
I hope that explanation helps someone and that I didn't just make it sound more confusing!
Amber Hoddinott
6,494 Points<?php
$random = array_rand($catalog,4);
foreach($random as $id){
echo get_item_html($id, $catalog[$id]);
}
?>
Jacob Mishkin
23,118 Pointsedited for formatting.
Jacob Mishkin
23,118 PointsJacob Mishkin
23,118 Pointscan you please post the code block you are having issues with.