foreach()语句中的PHP xpath错误

时间:2014-06-17 17:21:54

标签: php xml session xpath shopping-cart

我正在尝试仅使用PHP和XML构建基本购物车。我选择在这里使用simplexml和xpath。项目添加正常,但是当涉及到“getProduct($ id)”方法时,我将面临以下错误:

  

警告:为foreach()提供的参数无效   第203行/srv/disk6/1664324/www/mattc.biz.ht/cart.php

以下是我的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>

这是我的PHP / HTML。出于某种原因,我不允许添加PHP标签,所以我把它放在评论的位置。该页面将从URL访问,例如: http://www.mattc.biz.ht/cart.php?action=add&item=4(实际网站)

<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>

1 个答案:

答案 0 :(得分:1)

必须将数组作为参数传递,因为$prodz超出了范围(比较:PHP variable scope

function getProduct(array $products, $id)
{
    foreach ($products as $product) {
        if ($product['code'] == $id) {
            return $product;
        }
    }
}