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 trialStu Cowley
26,287 PointsExtending "Shirts4Mike" Shirts Array
Hey guys,
I've got a bit of a doozy for you guys today...
So I want to know how I can extend on from the Shirts array on the Shirts4Mike project to include other items, such as "Pants", "Sweat Shirts" and "Shoes". I like the way the Randy Hoyt coded it up.
Here is what is contained within the shirts array:
products.php
<?php
function get_list_view_html($product_id, $product) {
$output = "";
$output = $output . "<li>";
$output = $output . '<a href="shirt.php?id=' . $product_id . '">';
$output = $output . '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
$output = $output . "<p>View Details</p>";
$output = $output . "</a>";
$output = $output . "</li>";
return $output;
}
$products = array();
$products[101] = array(
"name" => "Logo Shirt, Red",
"img" => "img/shirts/shirt-101.jpg",
"price" => 18,
"paypal" => "9P7DLECFD4LKE",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[102] = array(
"name" => "Mike the Frog Shirt, Black",
"img" => "img/shirts/shirt-102.jpg",
"price" => 20,
"paypal" => "SXKPTHN2EES3J",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[103] = array(
"name" => "Mike the Frog Shirt, Blue",
"img" => "img/shirts/shirt-103.jpg",
"price" => 20,
"paypal" => "7T8LK5WXT5Q9J",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[104] = array(
"name" => "Logo Shirt, Green",
"img" => "img/shirts/shirt-104.jpg",
"price" => 18,
"paypal" => "YKVL5F87E8PCS",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[105] = array(
"name" => "Mike the Frog Shirt, Yellow",
"img" => "img/shirts/shirt-105.jpg",
"price" => 25,
"paypal" => "4CLP2SCVYM288",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[106] = array(
"name" => "Logo Shirt, Gray",
"img" => "img/shirts/shirt-106.jpg",
"price" => 20,
"paypal" => "TNAZ2RGYYJ396",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[107] = array(
"name" => "Logo Shirt, Teal",
"img" => "img/shirts/shirt-107.jpg",
"price" => 20,
"paypal" => "S5FMPJN6Y2C32",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[108] = array(
"name" => "Mike the Frog Shirt, Orange",
"img" => "img/shirts/shirt-108.jpg",
"price" => 25,
"paypal" => "JMFK7P7VEHS44",
"sizes" => array("Large","X-Large")
);
?>
shirts.php
<?php include("inc/products.php"); ?><?php
$pageTitle = "Mike's Full Catalog of Shirts";
$section = "shirts";
include('inc/header.php'); ?>
<div class="section shirts page">
<div class="wrapper">
<h1>Mike’s Full Catalog of Shirts</h1>
<ul class="products">
<?php foreach($products as $product_id => $product) {
echo get_list_view_html($product_id,$product);
}
?>
</ul>
</div>
</div>
<?php include('inc/footer.php') ?>
shirt.php
<?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"];
include("inc/header.php"); ?>
<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">
<?php foreach($product["sizes"] as $size) { ?>
<option value="<?php echo $size; ?>"><?php echo $size; ?> </option>
<?php } ?>
</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>
</div>
</div>
<?php include("inc/footer.php"); ?>
Would I create a new file for say pant.php & pants.php? Do I add the pant info within the products.php file? I've been scratching my head for awhile now and I thought I'd just post up this question. I might tag Hampton Paulk on this one cause his way of teaching PHP is perfect to understand!
Thanks and I look forward to seeing what people can come up with,
Cheers!
Stu : )
1 Answer
Daniel Santos
34,969 PointsHi Stu, I will suggest you to keep the array of products in products.php and add more products to it. Do not create a new file called, for example, pants.php. My reasons are the following: 1) Your array its called products, it make sense, it could be anything!. 2) At the end of the day you will want this in a database, which if you haven't learned, you will soon in Treehouse.
One of way that you can solve this is by, instead of call shirts.php or shirt.php, I would say item.php and items.php. So now you will have two files that handle any product, this is just organization thing. And then add more products to your array of products in products.php. Let me know anything, If I didn't answer your question let me know.
Stu Cowley
26,287 PointsStu Cowley
26,287 PointsHey Daniel,
Thanks for that,
I was thinking that I would need to create a database for this, it seems logical as I have a lot of things in my array. I'm focusing my study more on the PHP side of things.
Thanks for the tip!
Stu : D