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 trialElena Paraschiv
9,938 PointsRandy 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.
shirt.php
<?php
include("inc/header.php");
include("inc/products.php");
if(isset($_GET["id"])) {
$product_id = $_GET ["id"];
if(isset($products[$product_id])) {
$product = $products[$product_id] ;
}
}
if(!isset($product)) {
header("Location: shirts.php");
exit();
}
$section = "shirts" ;
$pageTitle = $product["name"];
?>
<div class = "section page">
<div class = "wrapper">
<div class="breadcrumb"><a href= "shirts.php">Shirts</a> > <?php echo $product["name"]; ?></div>
<div class = "shirt-picture">
<span>
<img src="<?php echo $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">
</span>
</div>
<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"]; ?>">
<table>
<tr>
<th>
<input type="hidden" name="on0" value="Size">
<label for="os0">Size</label>
</th>
<td>
<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>
</select>
</td>
</tr>
</table>
<input type="submit" value="Add to Cart" name="submit">
</form>
<p class="note-designer">*All shirts are designed by Mike the Frog.</p>
</div>
<? php include('inc/footer.php'); ?>
products.php
<?php
$products = array();
$products[101]= array(
"name" => "Logo Shirt, Red",
"price" => 18,
"img" => "img/shirts/shirt-101.jpg",
"product_description" => "monkey",
"paypal"=>"MLR8KT66AUMDN",
"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",
"paypal"=>"Y73SQZ86THATG",
"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",
"paypal"=>"XP7DM2QMWT786",
"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")
);
?>
Elena Paraschiv
9,938 PointsThanks Shawn. Now it looks much better
2 Answers
thomascawthorn
22,986 PointsYou're looking specifically at this section of code:
<?php
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:
<?php
$products[$product_id];
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.
<?php
$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:
<?php
$products[$product_id]["name"];
This wouldn't be wrong, it's just more difficult to work with.
Hope this helps!
Ted Sumner
Courses Plus Student 17,967 PointsI 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.
Shawn Gregory
Courses Plus Student 40,672 PointsShawn Gregory
Courses Plus Student 40,672 PointsElena,
I modified your post to fix the markdown errors. It looks a lot better now!
Cheers!