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
Kari Jones
6,217 PointsUsing a WHERE Clause Help
I am watching "Using a WHERE Clause" in stage 3 of the Using PHP with mySQL project. Here is my get_products_single function:
<?php
function get_products_single($sku) {
require(ROOT_PATH . "inc/database.php");
try {
$results = $db->query("SELECT name, price, img, sku, paypal FROM products WHERE sku = 108");
} catch (Exception $e) {
echo "Data could not be retrieved from the database.";
exit;
}
$product = $results->fetch(PDO::FETCH_ASSOC);
return $product;
}
?>
And here is my Shirt Details code:
<?php
require_once("../inc/config.php");
require_once(ROOT_PATH . "inc/products.php");
// if an ID is specified in the query string, use it
if (isset($_GET["id"])) {
$product_id = $_GET["id"];
$product = get_product_single($product_id);
}
// a $product will only be set and not false if an ID is specified in the query
// string and it corresponds to a real product. If no product is
// set, then redirect to the shirts listing page; otherwise, continue
// on and display the Shirt Details page for that $product
if (empty($product)) {
header("Location: " . BASE_URL . "shirts/");
exit();
}
$section = "shirts";
$pageTitle = $product["name"];
include(ROOT_PATH . "inc/header.php"); ?>
<div class="section page">
<div class="wrapper">
<div class="breadcrumb"><a href="<?php echo BASE_URL; ?>shirts/">Shirts</a> > <?php echo $product["name"]; ?></div>
<div class="shirt-picture">
<span>
<img src="<?php echo BASE_URL . $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(ROOT_PATH . "inc/footer.php"); ?>
When I go to the Shirt details page, all I get is a blank page. Why is there a blank screen? I have checked and double checked my code over and over again, and I don't see anything wrong, but I click the shirt on the shirts page and I get a blank screen. HELP!!!
2 Answers
Kari Jones
6,217 PointsIt may also be helpful to have the full products.php code:
<?php
/*
* Returns the four most recent products, using the order of the elements in the array
* @return array a list of the last four products in the array;
the most recent product is the last one in the array
*/
function get_products_recent() {
$recent = array();
$all = get_products_all();
$total_products = count($all);
$position = 0;
foreach($all as $product) {
$position = $position + 1;
if ($total_products - $position < 4) {
$recent[] = $product;
}
}
return $recent;
}
/*
* Loops through all the products, looking for a search term in the product names
* @param string $s the search term
* @return array a list of the products that contain the search term in their name
*/
function get_products_search($s) {
$results = array();
$all = get_products_all();
foreach($all as $product) {
if (stripos($product["name"],$s) !== false) {
$results[] = $product;
}
}
return $results;
}
/*
* Counts the total number of products
* @return int the total number of products
*/
function get_products_count() {
return count(get_products_all());
}
/*
* Returns a specified subset of products, based on the values received,
* using the order of the elements in the array .
* @param int the position of the first product in the requested subset
* @param int the position of the last product in the requested subset
* @return array the list of products that correspond to the start and end positions
*/
function get_products_subset($positionStart, $positionEnd) {
$subset = array();
$all = get_products_all();
$position = 0;
foreach($all as $product) {
$position += 1;
if ($position >= $positionStart && $position <= $positionEnd) {
$subset[] = $product;
}
}
return $subset;
}
/*
* Returns the full list of products. This function contains the full list of products,
* and the other model functions first call this function.
* @return array the full list of products
*/
function get_products_all() {
$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")
);
$products[109] = array(
"name" => "Get Coding Shirt, Gray",
"img" => "img/shirts/shirt-109.jpg",
"price" => 20,
"paypal" => "B5DAJHWHDA4RC",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[110] = array(
"name" => "HTML5 Shirt, Orange",
"img" => "img/shirts/shirt-110.jpg",
"price" => 22,
"paypal" => "6T2LVA8EDZR8L",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[111] = array(
"name" => "CSS3 Shirt, Gray",
"img" => "img/shirts/shirt-111.jpg",
"price" => 22,
"paypal" => "MA2WQGE2KCWDS",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[112] = array(
"name" => "HTML5 Shirt, Blue",
"img" => "img/shirts/shirt-112.jpg",
"price" => 22,
"paypal" => "FWR955VF5PALA",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[113] = array(
"name" => "CSS3 Shirt, Black",
"img" => "img/shirts/shirt-113.jpg",
"price" => 22,
"paypal" => "4ELH2M2FW7272",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[114] = array(
"name" => "PHP Shirt, Yellow",
"img" => "img/shirts/shirt-114.jpg",
"price" => 24,
"paypal" => "AT3XQ3ZVP2DZG",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[115] = array(
"name" => "PHP Shirt, Purple",
"img" => "img/shirts/shirt-115.jpg",
"price" => 24,
"paypal" => "LYESEKV9JWE3A",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[116] = array(
"name" => "PHP Shirt, Green",
"img" => "img/shirts/shirt-116.jpg",
"price" => 24,
"paypal" => "KT7MRRJUXZR34",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[117] = array(
"name" => "Get Coding Shirt, Red",
"img" => "img/shirts/shirt-117.jpg",
"price" => 20,
"paypal" => "5UXJG8PXRXFKE",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[118] = array(
"name" => "Mike the Frog Shirt, Purple",
"img" => "img/shirts/shirt-118.jpg",
"price" => 25,
"paypal" => "KHP8PYPDZZFTA",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[119] = array(
"name" => "CSS3 Shirt, Purple",
"img" => "img/shirts/shirt-119.jpg",
"price" => 22,
"paypal" => "BFJRFE24L93NW",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[120] = array(
"name" => "HTML5 Shirt, Red",
"img" => "img/shirts/shirt-120.jpg",
"price" => 22,
"paypal" => "RUVJSBR9FXXWQ",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[121] = array(
"name" => "Get Coding Shirt, Blue",
"img" => "img/shirts/shirt-121.jpg",
"price" => 20,
"paypal" => "PGN6ULGFZTXL4",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[122] = array(
"name" => "PHP Shirt, Gray",
"img" => "img/shirts/shirt-122.jpg",
"price" => 24,
"paypal" => "PYR4QH97W2TSJ",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[123] = array(
"name" => "Mike the Frog Shirt, Green",
"img" => "img/shirts/shirt-123.jpg",
"price" => 25,
"paypal" => "STDAUJJTSPT54",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[124] = array(
"name" => "Logo Shirt, Yellow",
"img" => "img/shirts/shirt-124.jpg",
"price" => 20,
"paypal" => "2R2U74KWU5RXG",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[125] = array(
"name" => "CSS3 Shirt, Blue",
"img" => "img/shirts/shirt-125.jpg",
"price" => 22,
"paypal" => "GJG7F8EW3XFAS",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[126] = array(
"name" => "Doctype Shirt, Green",
"img" => "img/shirts/shirt-126.jpg",
"price" => 25,
"paypal" => "QW2LFRYGU7L4Q",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[127] = array(
"name" => "Logo Shirt, Purple",
"img" => "img/shirts/shirt-127.jpg",
"price" => 20,
"paypal" => "GFV6QVRMJU7F8",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[128] = array(
"name" => "Doctype Shirt, Purple",
"img" => "img/shirts/shirt-128.jpg",
"price" => 25,
"paypal" => "BARQMHMB565PN",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[129] = array(
"name" => "Get Coding Shirt, Green",
"img" => "img/shirts/shirt-129.jpg",
"price" => 20,
"paypal" => "DH9GXABU3P8GS",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[130] = array(
"name" => "HTML5 Shirt, Teal",
"img" => "img/shirts/shirt-130.jpg",
"price" => 22,
"paypal" => "4LZ3EUVCBENE4",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[131] = array(
"name" => "Logo Shirt, Orange",
"img" => "img/shirts/shirt-131.jpg",
"price" => 20,
"paypal" => "7BNDYJBKWD364",
"sizes" => array("Small","Medium","Large","X-Large")
);
$products[132] = array(
"name" => "Mike the Frog Shirt, Red",
"img" => "img/shirts/shirt-132.jpg",
"price" => 25,
"paypal" => "Y6EQRE445MYYW",
"sizes" => array("Small","Medium","Large","X-Large")
);
// when creating a new product, create it first in PayPal and
// then copy the product ID from PayPal to use here
// automated duplication to copy the product_id/sku from the array key
// and duplicate it into the product details inside the array
foreach ($products as $product_id => $product) {
$products[$product_id]["sku"] = $product_id;
}
return $products;
}
/*
* Returns an array of product information for the product that matches the sku;
* returns a boolean false if no product matches the sku
* @param int $sku the sku
* @return mixed array list of product information for the one matching product
* bool false if no product matches
*/
function get_products_single($sku) {
require(ROOT_PATH . "inc/database.php");
try {
$results = $db->query("SELECT name, price, img, sku, paypal FROM products WHERE sku = 108");
} catch (Exception $e) {
echo "Data could not be retrieved from the database.";
exit;
}
$product = $results->fetch(PDO::FETCH_ASSOC);
return $product;
}
?>
Mike Bronner
16,395 PointsLet's assume all your code is correct for a moment. Check your database to make sure you have an item with sku of 108 in it. Now, if that checks out, check each item with an sku of 108 and make sure it has a sizes field filled in.
My guess is that the field is called size and not sizes, so you may want to check your foreach loop if that is the case.
Kari Jones
6,217 PointsThis did not help, but thank you anyways.
Mike Bronner
16,395 PointsSorry about that. Can you provide a link to the challenge? I'll see if I can do it and check what's different.
Kari Jones
6,217 PointsYeah, sure, watch this for the Shirt Details code: http://teamtreehouse.com/library/using-php-with-mysql/filtering-input-for-queries/refactoring-shirt-details, and this for the get_products_single function: http://teamtreehouse.com/library/using-php-with-mysql/filtering-input-for-queries/using-a-where-clause.