Hi,
I have been having this issue for a while and I know that it is located within the Foreach loop as it is crashing the compiler and nothing is generated after that code runs.
It is designed so that it will print table rows and fill them with data that has been returned from the MYSQL query but till not I have not been able to work out what index, etc is being used and I am unable to stop the crash I have run a var_dump($cart) and I see the code that is generated but it is not clear as to how I should be iterating through the code.
Any help would be terrific.
The complete code is:
[php]<?php
// Create connection
//Get Variables from Top Frame
$var = $_GET[‘var’];
$units = $_GET[‘units’];
//Create Cart Class
class Cart
{
//Initialise Cart
public function __construct ()
{
$this->cart = array();
$this->total_price = 0;
$existing_cart = $_SESSION['cart'];
echo "Step 1 Check for Existing Cart<br>";
if ($existing_cart)
{
echo "Step 1.5 There is an Existing Cart<br>";
foreach ($existing_cart as $item)
{
echo "Product ID from existing cart is:";
echo $item['0'];
echo "<br>";
$this->add($item['0'], $item['quantity']);
}
}
}
//Add An Object to the Cart
public function add($id, $quantity) {
session_start();
if (!$this->cart[$id])
{
$product = $this->fetchProduct($id);
echo "Step 3 Product is Returned<br>";
echo "The product ID is:";
echo $product[0];
if ($product)
{
echo "Step 4 Product Exists<br>";
if ($product['4'] < $quantity)
{
echo "Step 5 InStock<Quantity WONT WORK!<br>";
return false;
}
else
{
echo "Step 5 InStock>Quantity SHOULD WORK!<br>";
$units -= $quantity;
$product['3'] += $quantity;
$this->cart[$id] = $product;
$this->total_price += ($product['2'] * $quantity);
}
}
else
{
return false;
}
}
else
{
echo "Step 6 If Item is in Cart then Update Values";
$current_quantity = $this->cart[$id]['4'];
if ($quantity < $current_quantity)
{
echo "Step 7 Check if Quantity Required is less than current Quantity";
// update quantity
$units -= $quantity;
$this->cart[$id]['quantity'] += $quantity;
$this->total_price += ($product['2'] * $quantity);
}
else
{
return false;
}
}
echo "Step 8 Write Everything to Cookie called Cart<br>Cookie Contents are<br>";
// echo $_COOKIE["cart"];
//setcookie('cart', json_encode($this->cart), time() + 3600);
$_SESSION['cart'] = $this->cart;
return true;
}
public function getTotal()
{
echo "Step 9 Total all Values";
return $this->total_price;
}
public function clear()
{
$this->cart = array();
// setcookie("cart", "", time() - 3600);
session_destroy();
}
// Database helper function
public function fetchProduct($var)
{
echo “Step 2 Fetch Product
”;
$con=mysqli_connect(“rerun”,“potiro”,“password”,“poti”);
$result = mysqli_query($con,“SELECT * FROM products WHERE product_id=$var”);
return mysqli_fetch_row($result);
}
}
/* Begin Logic */
echo "
Product ID: | Product Name: | Unit Price: | Unit Quantity | In Stock | Quantity | Price |
---|---|---|---|---|---|---|
" .$item[‘0’] . " | " .$item[‘1’] . " | " .$item[‘2’] . " | " .$item[‘3’] . " | " .$item[‘4’] .” | " .$item[‘quantity’] .” | " . int($item[‘2’]) * int($item[‘quantity’]) . " |