I am using the following code to generate a shoping cart. Right now I am trying to run the code using an array created on page rather than pulling data from database.
How should the code be formated to grab data from an array on the page instead?
Code: Select all
<?php
case 'addcart':
if (is_numeric($_GET['id'])) {
$itemid = $_GET['id'];
if (isset($_SESSION['cart'][$itemid])) {
$qty = $_SESSION['cart'][$itemid] + 1;
}else{
$qty = $_GET['qty'];
}
$_SESSION['cart'][$itemid] = $qty;
header("location: ../store.php");
}
break;
case 'cart';
if(isset($_POST['submit'])){
foreach($_REQUEST['qty'] as $key => $value){
if (($value == 0) && (is_numeric ($value))){
unset ($_SESSION['cart'][$key]);
}elseif ( is_numeric ($value) AND ($value > 0) ){
$_SESSION['cart'][$key] = $value;
}
}
}
$empty = TRUE;
if(isset($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
if(isset($value)) {
$empty = FALSE;
}
}
}
if(!$empty) {
$query = 'SELECT * FROM items WHERE itemid IN (';
foreach($_SESSION['cart'] as $key => $value) {
$query .= $key . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY itemid ASC';
$result = mysql_query($query);
// Start form to display content headings
$total = 0;
while ($row = mysql_fetch_array ($result)) {
foreach($row as $key=>$value){
$$key = ValidateInput($value);
}
$subtotal = $_SESSION['cart'][$itemid] * $price;
$total += $subtotal;
$qty = $_SESSION['cart'][$itemid];
$totalqty += $qty;
session_register('total');
$_SESSION['total'] = number_format($total, 2);
session_register('qty');
$_SESSION['qty'] = $qty;
session_register('totalqty');
$_SESSION['totalqty'] = $totalqty;
// loop through carts records and display
}
// End form to display
}else{
echo '<p /><center>Your cart is currently empty.</center>';
}
break;
?>