Object Iteration Issue!

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 "

";

$cart = new Cart();
if (isset($var))
{
$cart->add($var, $units);
}

//Code for adding rows from Cart Object here
$i = 0;
foreach ($cart->cart as $item)
{
if ($cart)
{
echo “
There is a cart contents =
“;
echo $cart[0];
var_dump($cart);
}
echo $i;
echo "









";
echo $i;
$i++;
}
//Display Final Row with Total Price of All Goods
   echo "<tr><td>Total</td><td></td><td></td><td></td><td></td><td></td><td>" .$cart->getTotal() ."</td></tr>";
   
   //Close Table
   echo "</table>";

    //Form to Submit & Form to Clear
    //  echo "<form method='post' action='email.php'>
//  <input type='hidden' name='var' value=$cart>
//  <input type='submit' value='Checkout'>
//  </form>";
echo "<form action=$cart->clear()>
<input type='submit' value='Clear'>
</form>";

?>
[/php]

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’]) . "

Hi,

I do not currently have access to a PHP executable, so the analysis will be based on structure only.

  • You attempt to access the $_SESSION superglobal in the constructor of your cart. While this is not wrong, what is definitely wrong is that your session_start call is in the add() function of the same class. If you expect sessions to not be started in your constructor but in add instead, consider removing the access to session variables in the constructor. Otherwise, start your session explicitely elsewhere (This is the preferred method).
  • Why are you using indices such as [‘1’]? If this is your index name, change that to something more memorable. If you are trying to access numeric keys, it’s [1]. There is a world of difference.
  • What do you mean by “crash the compiler”? What error are you getting?
Sponsor our Newsletter | Privacy Policy | Terms of Service