1

I am trying to build a basic shopping cart using only PHP and XML. I have chosen to use simplexml and xpath here. Items are added fine, but when it comes to the "getProduct($id)" method, which will return the product with a matching code, I am faced with the following error:

Warning: Invalid argument supplied for foreach() in /srv/disk6/1664324/www/mattc.biz.ht/cart.php on line 203

Here is a snippet from my XML (For the Structure)

<?xml version="1.0" encoding="utf-8"?>
<GreenTrade>
<brand id="Alara">
    <product code="1">
        <name>Fair Trade Muesli x 500g</name>
        <desc>Piced with cinnamon and honey</desc>
        <image>1.jpg</image>
    </product>
    <product code="2">
        <name>Luxury Gluten Free Muesli x 500g</name>
        <desc>Gluten Free - Wheat Free - High Proteins (Over 10 Percent) - Balanced Nutrition - Vegan </desc>
        <image>luxuryglutenfree.jpg</image>
    </product>
    <product code="3">
        <name>Of the Earth - Goji berries x 60g</name>
        <desc>Bursting with Beta-Carotene and all 8 Essential Amino Acids </desc>
        <image>gojiberries.jpg</image>
    </product>
</brand>

<brand id = "BigOz">
    <product code="5">
        <name>Corn Flakes x 350g</name>
        <desc> Gluten Free – Dairy free – Low in Sodium – No added artificial flavours, colours or preservatives – Naturally cholesterol and sodium free </desc>
        <image>5.jpg</image>
    </product>
</brand>

<brand id="Windmill">
    <product code="8">
        <name>Amisa Organic Corn and Rice Rigatoni</name>
        <desc>Organic, Gluten Free, Dairy Free, Wholegrain</desc>
        <image>corn_rice_rigatoni.jpg</image>
    </product>
</brand>    
</GreenTrade>

And here is my PHP / HTML. For some reason I was not allowed to add PHP tags so i put where they would be in comments. The page would be accessed from a URL Such as: http://www.mattc.biz.ht/cart.php?action=add&item=4 (Actual Site)

<body>

<div class="container">
<div class="header"><a href="index.php"><img src="Mattc.fw.png" alt="Insert Logo Here" name="Insert_logo" width="180" height="90" id="Insert_logo" style="background-color: #C6D580; display:block;" /></a> 
<!-- end .header --></div>
<div class="sidebar1">
<ul class="nav">
  <li><a href="index.php">Home</a></li>
  <li><a href="store.php">Store</a></li>
  <li><a href="cart.php">Cart</a></li>
  <li><a href="mailto:mat.cassar@gmail.com">Email</a></li>
</ul>
<p>Left Side Bar</p>
<!-- end .sidebar1 --></div>
<div class="content">
<h1>Your Shopping Cart</h1>


//OPEN PHP TAG

    $xml = simplexml_load_file('GreenTrade.xml');
    $brands = $xml->xpath('//brand');
 $prodz = $xml->xpath('//product'); 
 $product_id = $_GET [item];
 $action = $_GET [action];
//$contains = $_SESSION['my_cart'];
 $brandz = 3;
     //Adds 1 to QTY    
    if (!empty($_GET['item']) && $action == "add") {            
         $_SESSION['cart'][$product_id]++; 
    }


     //Delete 1 From QTY    
    if (!empty($_GET['item']) && $action == "del") {            
        $_SESSION['cart'][$product_id]--; 
        if($_SESSION['cart'][$product_id] <= 0){ 
            unset($_SESSION['cart'][$product_id]); 
        } 
    }

    if ($action == "empty") {
        unset($_SESSION['cart']); 
    }

    echo '</br><hr align="CENTER">';


     if($_SESSION['cart']){ 


    echo "<div align='center'><a href='cart.php?action=empty'>Empty Cart</a></div>";        
        foreach($_SESSION['cart'] as $prd => $quantity){        
            $product=getProduct($prd);  //<--- Call to getProduct       
            echo "Name: " . $product->name . '</br>';
            echo "Description: " . $product->desc . '</br>';
            echo '<img src=' . $product->image . ' >' . '</br>';
            echo "Ordered: " . $quantity . '</br>';
            echo '<a href="cart.php?action=add&item=' . $product['code'] . '">Add Another</a>' . '</br>';
            echo '<a href="cart.php?action=del&item=' . $product['code'] . '">Remove One</a>' . '</br>';
            echo '</br><hr align="CENTER">';
        }   

    } else {

        echo '<h2>'."Your Cart Is Empty".'</h2>';   
    }

//VVV Here Is the Troublesome Function VVV

function getProduct($id){                       
    foreach($prodz as $prod){
        if($prod['code'] == $id){           
            return $prod;
        }
    }   

}

// CLOSE PHP TAG


<!-- end .content --></div>
<div class="footer">
<p>MattC.biz.ht (C) Matthew Cassar 2014</p>
<p>A Chris Porter Web Project <!-- end .footer --></p>
</div>
<!-- end .container --></div>
</body>
Matthew Cassar
  • 223
  • 2
  • 5
  • 13
  • This warning is usually triggered when the iteratable. Probably $_SESSION['cart'] is not an array – Jose Areas Jun 17 '14 at 17:36
  • Jose it actually is, Here is the output when printing it with a cart filled with items using print_r($_SESSION['cart']); : Array ( [6] => 1 [3] => 1 [4] => 6 ) Meaning there is 1 of item 6 1 of item 3 6 of item 4 – Matthew Cassar Jun 17 '14 at 17:40
  • @MatthewCassar: Next to your obvious problem with the [PHP variable scope](http://www.php.net//manual/en/language.variables.scope.php) (see the edit of your answer as well), you might be actually interested in the duplicate I closed your question against as it shows how you can do this without re-inventing the wheel. – hakre Jun 19 '14 at 14:48

1 Answers1

1

The array must be passed as a paramter because $prodz is out of scope (compare: PHP variable scope.

function getProduct(array $products, $id)
{
    foreach ($products as $product) {
        if ($product['code'] == $id) {
            return $product;
        }
    }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Matthew Cassar
  • 223
  • 2
  • 5
  • 13