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

SCU ACM
SCU ACM
32,131 Points

What is the point of passing $id around?

This is a question for the code so far in Alena Holligan's tutorial, "Build a Basic PHP Website". I'm on the video called "Displaying Categories."

So I'm following how we're listing $catalog on the various pages, and accessing the $catalog data with our functions get_item_html() and array_category(), but I see this variable $id being used as well, and I'm trying to follow it.

The only place I see $id actually being utilized is in array_category(), when, with each iteration in the foreach loop, $id is added to the $output[] array. It looks like $output[] thus becomes an array of numbers, like 102, 103, 104, etc.

From that point on, $id is returned in the $output[] array, and is then used in catalog.php in this foreach function... foreach($categories as $id) { echo get_item_html($id,$catalog[$id]); } ... so I see that $id is used to identify which index of $catalog to use. BUT, in that above foreach function, when $id is passed back into get_item_html(), it's never actually used.

So is there any point to the variable $id? Couldn't the $output[] array from array_category() just be filled with $item variables instead of $id variables, and it would then be an array of array items?

Maybe this question isn't too important, but isn't using $id just kind of cluttering the code a bit, or is it intended to make it easier to follow? I just keep losing track of the variables... :)

1 Answer

Hi SCU ACM,

At the time that you create the get_item_html function you don't use the $id variable but it will get used later.

The link has a # for a placeholder but later the $id will be needed to create the link to go to an individual item page.

The teacher's notes for this video should clear up why we need it: https://teamtreehouse.com/library/build-a-basic-php-website/listing-and-sorting-inventory-items/displaying-item-details