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 trialGeorge Akinian
17,615 Pointspaypal button does not display sizes after I add sizes to the products.php array and create foreach loop inside form.
The drop down menu for sizes remains blank for some reason. Here's my code. Any pointers are greatly appreciated.
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"] ?>"; >
<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["size"] 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 approved by George Akinian. </p>
</div>
</div>
</div>
<?php include("inc/footer.php");
products.php
<?php
$products = array();
$products[101] = array(
"name" => "Motley Crew",
"img" => "img/shirts/shirt-101.jpg",
"price" => 18,
"paypal" => "LREGR8T9KJVBL",
"sizes" => array("Small", "Medium", "Large","X-Large")
);
$products[102] = array(
"name" => "Logo Shirt,Black",
"img" => "img/shirts/shirt-102.jpg",
"price" => 20,
"paypal"=> "4QJP28T7TVFM2",
"sizes" => array("Small", "Medium", "Large","X-Large")
);
$products[103] = array(
"name" => "Logo Shirt,Blue",
"img" => "img/shirts/shirt-103.jpg",
"price" => 20,
"paypal" => "NJLAHH9WNRU2W",
"sizes" => array("Small", "Medium", "Large","X-Large")
);
$products[104] = array(
"name" => "Logo Shirt,Green",
"img" => "img/shirts/shirt-104.jpg",
"price" => 18,
"paypal" => "Y7VGRU9NMS2CS",
"sizes" => array("Small", "Medium", "Large","X-Large")
);
$products[105] = array(
"name" => "Logo Shirt,Yellow",
"img" => "img/shirts/shirt-105.jpg",
"price" => 25,
"paypal" => "WX7BTHCKXXNQU",
"sizes" => array("Small", "Medium", "Large","X-Large")
);
$products[106] = array(
"name" => "Logo Shirt,Gray",
"img" => "img/shirts/shirt-106.jpg",
"price" => 20,
"paypal" => "QQ6SY3MZ2RR7Q",
"sizes" => array("Small", "Medium", "Large","X-Large")
);
$products[107] = array(
"name" => "Logo Shirt,Turquoise",
"img" => "img/shirts/shirt-107.jpg",
"price" => 20,
"paypal" => "J4UGJEFJRAQMU",
"sizes" => array("Small", "Medium", "Large","X-Large")
);
$products[108] = array(
"name" => "Logo Shirt,Orange",
"img" => "img/shirts/shirt-108.jpg",
"price" => 25,
"paypal" => "EYNAEAUEYTSH2",
"sizes" => array("Large","X-Large")
);
?>
1 Answer
George Akinian
17,615 PointsSolved. Found a typo: <?php foreach($product["sizes"] as $size) { ?>
"size" is suppose to be "sizes".
tks