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 Simple PHP Application Integrating with PayPal Adding Available Sizes

Elena Paraschiv
Elena Paraschiv
9,938 Points

Randy says : "We copied the array of our shirt attribute from the 2nd dimensions of product array into $product

Can someone please explain this part with code.


if(isset($_GET["id"])) {
    $product_id = $_GET ["id"];

    if(isset($products[$product_id])) {
        $product = $products[$product_id] ;
    if(!isset($product)) {
        header("Location: shirts.php");
$section = "shirts" ;
$pageTitle = $product["name"];

<div class = "section page">
    <div class = "wrapper">

            <div class="breadcrumb"><a href= "shirts.php">Shirts</a> &gt; <?php echo $product["name"]; ?></div>
            <div class = "shirt-picture">
                <img src="<?php echo $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">

            <div class = "shirt-details ">

                    <h1> <span class="price"> $<?php echo $product["price"]; ?></span> <?php echo $product["name"]; ?></h1>

                    <form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
                        <input type="hidden" name="cmd" value="_s-xclick">
                        <input type="hidden" name="hosted_button_id" value="<?php echo $product["paypal"]; ?>">
                        <input type="hidden" name="item_name" value="<?php echo $product["name"]; ?>">

                                <input type="hidden" name="on0" value="Size">
                                <label for="os0">Size</label>
                                <select name="os0" id="os0">
                                    <option value="Small">Small </option>
                                    <option value="Medium">Medium </option>
                                    <option value="Large">Large </option>
                                    <option value="X-Large">X-Large</option>

                        <input type="submit" value="Add to Cart"  name="submit">


                    <p class="note-designer">*All shirts are designed by Mike the Frog.</p>


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


$products = array();
$products[101]= array(
    "name" => "Logo Shirt, Red",
    "price" => 18,
    "img"  => "img/shirts/shirt-101.jpg",
    "product_description" => "monkey",
    "sizes" => array("Small","Medium","Large")

$products[102] = array(
    "name" => "Mike the Frog Shirt, Black",
    "img" => "img/shirts/shirt-102.jpg",
    "price" => 20,
    "product_description" => "ponkey",
    "sizes" => array("Small","Large","X-Large")

$products[103] = array(
    "name" => "Mike the Frog Shirt, Blue",
    "img" => "img/shirts/shirt-103.jpg",    
    "price" => 20,
    "product_description" => "donkey",
    "sizes" => array("Small","Medium","Large","X-Large")
$products[104] = array(
    "name" => "Logo Shirt, Green",
    "img" => "img/shirts/shirt-104.jpg",    
    "price" => 18,
    "product_description" => "tonkey",
    "paypal"=>" YLFNNBZRTRY5Q",
    "sizes" => array("Small","Medium","Large","X-Large")
$products[105] = array(
    "name" => "Mike the Frog Shirt, Yellow",
    "img" => "img/shirts/shirt-105.jpg",    
    "price" => 25,
    "product_description" => "tronkey",
    "paypal"=>" D2JVQELK9XLD6",
    "sizes" => array("Small","Medium","Large","X-Large")
$products[106] = array(
    "name" => "Logo Shirt, Gray",
    "img" => "img/shirts/shirt-106.jpg",    
    "price" => 20,
    "product_description" => "punky",
    "paypal"=>" QNB2L8WJBL5RC",
    "sizes" => array("Small","Medium","Large","X-Large")
$products[107] = array(
    "name" => "Logo Shirt, Turquoise",
    "img" => "img/shirts/shirt-107.jpg",    
    "price" => 20,
    "product_description" => "zonkey",
    "paypal"=>" Z7NW2KEL84V6C",
    "sizes" => array("Small","Medium","Large","X-Large")
$products[108] = array(
    "name" => "Logo Shirt, Orange",
    "img" => "img/shirts/shirt-108.jpg",    
    "price" => 25,
    "product_description" => "lonky",
    "paypal"=>"KVVEV499GG7P8 ",
    "sizes" => array("Small","Medium","Large","X-Large")



I modified your post to fix the markdown errors. It looks a lot better now!


Elena Paraschiv
Elena Paraschiv
9,938 Points

Thanks Shawn. Now it looks much better

2 Answers

You're looking specifically at this section of code:


if(isset($products[$product_id])) {
    $product = $products[$product_id];

Inside $products you've got loads of shirts - and their associated attributes. Each shirt is keyed by 'product_id' and you can access a specific shirt by doing:



On this page you want to use one product in particular. It would be kinda annoying (and probably increased processor work) if have to access your one project by passing in the product ID to the products array every time.

To make the code more readable and manageable, you've 'copied' the array at $products[$product_id] into a dedicated product variable.


$product = $products[$product_id];

For example, if you didn't transfer your product to it's own variable, you would have to constantly be doing things like:



This wouldn't be wrong, it's just more difficult to work with.

Hope this helps!

I am not sure what your question is, but the products.php is just a hard coded array of the shirts in the store. In a later course you will actually move that to a database, which is a standard way of handling data like this.