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 Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Random Fun with Arrays

thimoschroeder
thimoschroeder
23,470 Points

Why is $id used in the echo statement twice?

Hey,

first off, here's the code:

<?php 
          $random = array_rand($catalog, 4);
          foreach ($random as $id) {
            echo get_item_html($id,$catalog[$id]);
          } 
          ?>    

As pointed out above, why do we use the $id twice inside the echo statement: get_item_html($id,$catalog[$id])?

I can't follow from this point further !

Any help is greatly appreciated !!

4 Answers

We use $id twice inside the echo because the get_item_html() function requires 2 parameters.

Ill try and work through an example so you can see why we wrote it like this:

Inside index.php the $random variable holds an array of 4 elements, these are random numbers that refer to the keys in our $catalog array. (array_rand only returns the key not the whole array itself - array_rand )

We can do var_dump($random) to see what that looks like:

array(4) { 
[0]=> int(204) 
[1]=> int(301) 
[2]=> int(302) 
[3]=> int(303) } 

We now have an array of 4 items:

$random = [204, 301, 301, 303]

Next we use a foreach loop to iterate over each item in this $random array and assign each item's value to $id. The first time around the loop $id = 204, second time around $id=301 etc.

We then call the get_item_html() function (from data.php) and pass in $id and $catalog[$id] as parameters. The first time around the loop the call to the get_item_html function will look like this:

get_item_html(204, $catalog[204]);

Using the parameters we just passed to get_item_html() we get the following:

    $output = "<li><a href='details.php?id="204"'><img src='"
        . $catalog[204]["img"] . "' alt='"
        . $catalog[204]["title"] . "' />" 
        . "<p>View Details</p>"
        . "</a></li>";

The get_item_html function returns the html that we can then echo out to the screen.

echo get_item_html($id, $catalog[$id]);

I used a bunch of var_dumps in the index.php file to help me understand what was happening at different stages of the code. Here's the code, just uncomment the var_dumps one at a time for easier reading.

<?php 

include("inc/data.php");
include("inc/functions.php");

$pageTitle = "Personal Media Library";
$section = null;

include('inc/header.php'); ?>
        <div class="section catalog random">

            <div class="wrapper">

                <h2>May we suggest something?</h2>

                    <ul class="items">
                        <?php 
                        $random = array_rand($catalog, 4);
            var_dump($random);
                        foreach($random as $id){

                            echo get_item_html($id, $catalog[$id]);
//              var_dump($id);
//              var_dump($catalog[$id]);
                        } 
//              var_dump( $catalog[101]["img"]);


                        ?>          
                    </ul>

            </div>

        </div>
<?php include('inc/footer.php'); ?>

Hope this helps, let me know if you have any questions :)

This is an awesome explanation! Thanks, James

Edward Lee
Edward Lee
1,805 Points

Wow, thank you so much. This explanation clarifies about 10 previous sections.

caroline
caroline
6,974 Points

Thank you James Anwyl for a great explanation. Using var_dump as a way to 'see inside' the code is a great tip. However, I'm still struggling to understand what the first argument ( $id ) is doing inside the function call get_item_html($id, $catalog[$id]). If I replace it with NULL, the function still works in the same way. Here's what I mean.

<?php 
include("inc/data.php");
include("inc/functions.php");

$pageTitle = "Personal Media Library";
$section = "null";

include("inc/header.php"); ?>
        <div class="section catalog random">

            <div class="wrapper">

                <h2>May we suggest something?</h2>

                    <ul class="items">

                        <?php 
                        $random = array_rand($catalog,4);
                        foreach ($random as $id) {
                            echo get_item_html(NULL, $catalog[$id]);
                        }
                        ?>                          
                    </ul>

            </div>

        </div>

<?php include("inc/footer.php"); ?> 

This code produces the same output of four random items on the page.

So my question is: why use $id as the argument and not NULL, or something completely different? Would love an explanation, as I just can't see the rationale for it.

Hi Caroline, You raise a good point. Please see this thread for an excellent explanation.

Hope this helps :)

thimoschroeder
thimoschroeder
23,470 Points

James Anwyl - awesome, many thanks for the thorough explanation - the var_dump method to see how it's working is pretty useful, I am going to adapt that !!!

James Anwyl This is the best answer i have found that make u love php and understand it. I love your detailed answer!

caroline
caroline
6,974 Points

Thanks James!