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

Michael Dowell
Michael Dowell
2,390 Points

Shirts sizes won't pop up. Can't find typo.

Here's the code for shirt.php:

<?php include "inc/products.php" ?>
<?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> &gt; <?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-sclick">
                            <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"); ?>

and here is products.php:

<?php

$products = array();
$products[101] = array(
    "name" => "Logo Shirt, Red",
    "img" => "img/shirts/shirt-101.jpg",
    "price" => 18,
        "paypal" => "BK6EVECGZ78XG",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[102] = array(
    "name" => "Mike the Frog Shirt, Browny Black",
    "img" => "img/shirts/shirt-102.jpg",
    "price" => 20,
    "paypal" => "BK6EVECGZ78XG",
    "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" => "BK6EVECGZ78XG",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[104] = array(
    "name" => "Logo Shirt, Green",
    "img" => "img/shirts/shirt-104.jpg",    
    "price" => 18,
    "paypal" => "BK6EVECGZ78XG",
    "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" => "BK6EVECGZ78XG",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[106] = array(
    "name" => "Logo Shirt, Gray",
    "img" => "img/shirts/shirt-106.jpg",    
    "price" => 20,
    "paypal" => "BK6EVECGZ78XG",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[107] = array(
    "name" => "Logo Shirt, Turquoise",
    "img" => "img/shirts/shirt-107.jpg",
    "price" => 20,
    "paypal" => "BK6EVECGZ78XG",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[108] = array(
    "name" => "Logo Shirt, Orange",
    "img" => "img/shirts/shirt-108.jpg",    
    "price" => 25,
    "paypal" => "BK6EVECGZ78XG",
    "sizes" => array("Large","X-Large")
);
?>

4 Answers

Michael Dowell
Michael Dowell
2,390 Points

Got it! Here was the typo: <?php foreach($product["$sizes"] as $size) { ?>

Instead, it should be: <?php foreach($product["sizes"] as $size) { ?>

Notice that the value inside the brackets $product["$sizes"] had an extra $...After I removed it, it now works. Here is the correct syntax: $product["sizes"]

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Michael,

I think your typo is in your HTML. You are using double quotes inside of double quotes which is messing it up.

This is the code snippet in question:

<span>
    <img src="<?php echo $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">
</span>

Just replace the doubles around img and name with singles and see if that helps.

<span>
    <img src="<?php echo $product['img']; ?>" alt="<?php echo $product['name']; ?>">
</span>

:)

Michael Dowell
Michael Dowell
2,390 Points

Hmm, ok. Thanks! So I implemented the changes you mentioned, but unfortunately it didn't change anything that I could see. Actually I think the problem is in this section of code:

<select name="os0" id="os0">
    <?php foreach($product["$sizes"] as $size) { ?>
    <option value="<?php echo $size; ?>"><?php echo $size; ?></option>
    <?php } ?>
</select>
Michael Dowell
Michael Dowell
2,390 Points

Thanks Jason! :) Much appreciated