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>