Skip to content

Instantly share code, notes, and snippets.

Created March 23, 2013 02:51
Show Gist options
  • Save anonymous/5226210 to your computer and use it in GitHub Desktop.
Save anonymous/5226210 to your computer and use it in GitHub Desktop.
<?php
require_once 'lib.php';
redirect_if_offline();
/******************************************************
Write code to grab item ID and quantity GET parameters
Validate that item and quantity are valid integers
*****************************************************/
$quantity = $_GET['quantity'];
$itemID = $_GET['item'];
$quantCheck = 0;
$itemCheck = 0;
session_start();
$_SESSION['cart'] = array();
if (is_numeric($quantity))
{
echo "IN!";
$quant = intval($quantity);
if ($quant > 0)
{
$quantCheck = 1;
}
echo $quantCheck;
}
if (is_numeric($itemID))
{
$itemID = intval($itemID);
if ($itemID > -1 && $itemID < 10)
{
$itemCheck = 1;
}
echo $itemCheck;
}
if ($quantCheck == 1 && $itemCheck == 1)
{
echo "IN!";
$_SESSION['cart'][$itemID][0] = $itemID;
$_SESSION['cart'][$itemID][1] = $quant;
// echo $_SESSION['cart'][$itemID][0];
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="wrapper">
<?php require_once 'left.php'; ?>
<div id="main">
<h2>Your Shopping Cart</h2>
<table>
<tr><th>Item</th><th>Qty</th><th>Price</th></tr>
<?php
/******************************************************
Display a table of items, quantities, and subtotal for
each item. Output the total cost below.
*****************************************************/
$price = 0;
foreach ($_SESSION['cart'] as $itemID => $qty) {
echo $itemID;
?>
<tr> <td><?php echo $items[$itemID][0] ?> </td><td> <?php
echo $qty[1]?></td><td> <?php
echo $items[$itemID][1]*$qty[1] ?> </td></tr> <?php
$price = $price + $items[$itemID][1]*$qty[1];
}
?>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>Total</td><td>&nbsp;</td><td>$<?php echo $price ?></td></tr>
</table>
</div>
</div>
</body>
</html>
<?php
require_once 'lib.php';
redirect_if_offline();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="wrapper">
<?php require_once 'left.php'; ?>
<div id="main">
<h2>PHP Store Demo</h2>
<br />
<?php
/******************************************************
- Validate the item ID:
- If there is no item ID specified, display a welcome message.
- If an item ID is not valid, display an error message.
- If it is a valid item ID, display the item name and price,
and provide a form to add items to the cart.
- hint: your form should use GET, and there needs to be
a hidden form element with the item ID. Example:
<input type='hidden' name='item' value='<?php echo $itemID; ?>' />
*****************************************************/
if (isset($_GET['item']))
{
$item = $_GET['item'];
if (is_numeric($item) )
{
$itemID = intval($item);
if ($itemID > 0 || $itemID == 0 && $itemID < 10)
{
if ($items[$item][0] != NULL)
{
echo $items[$item][0];
echo "<br/> Price: ". $items[$item][1] . "<br/>" ;
?>
<form action ="cart.php" method="get">
Quantity: <input type="text" name="quantity" id="Quantity">
Add to Cart: <input type = "Submit" value="Submit" id="Add to Cart">
<input type='hidden' name='item' value='<?php echo $itemID; ?>' />
<?php
}
else
{
echo "INVALID ITEM ID!";
}
}
else if ($itemID)
{
echo "INVALID ITEM ID!";
}
}
else
{
echo "INVALID ITEM ID!";
}
}
else
{
echo " Weclome to the store! Thank you for shopping with us! \n\n";
}
?>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment