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

PHP

Amber Hoddinott
Amber Hoddinott
6,494 Points

PLEASE HELP!!

PLEASE PLEASE!! Can someone explain this to me... i can't move forward with my learning until i understand this??!!

//code 1

$random = array_rand($catalog,4);

foreach($random as $id){ echo get_item_html($id, $catalong[$id]); } ?>

//code 2

$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 = " . $item["img"] . "'alt = $item["title"] . "'/> view details . "; 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!

5 Answers

Matthew Brock
Matthew Brock
16,791 Points

Lets try another way. When you call

// invoking the method
 get_item_html($id, $catalog[$id]);

in the function variable1 is $id, and variable2 is $catalog[$id]. In the function implementation

// implementation
function  get_item_html($var1, $var2) {
   // $var1 = $id from when you invoked the method
   // $var2 = $catalog[$id] from when you invoked the method
}

Think of it as when you invoke the method you are giving it an exact value like "a long stemmed red rose without thorns", but when you are implementing the function you are just telling it that it is a "rose" because you do not know the specific details of what the rose is going to be until is invoked.

Matthew Brock
Matthew Brock
16,791 Points

// Just doing the same answer as before

you 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
Amber Hoddinott
6,494 Points

im trying so hard to understand how item = $catalog["$id"]; im sorry i know my understand is almost there, im waiting for a wow moment here lol im not quite there yet? thank so much by the way for helping me :D

Amber Hoddinott
Amber Hoddinott
6,494 Points

OK SO I THINK I GET IT!!! :D

The value of $id is the key numbers for example 102, which is passed into the $catalog[$id] ...$catalog[102] and sent to the function itself... causes the vaule of $item to be catalog[$id] still 102, which is then passed into the function code block into the $item["img"] and $item["title"] so it makes it $item["img"] ....... $102[this is the value of the 102 key number which will be the string of src of the picture]

is this right?..that hard to explain sorry!

Amber Hoddinott
Amber Hoddinott
6,494 Points

thank goodness now i feel silly why i didnt get it ha ha. Thanks so much for your help!!