0

以下に書いたコードに問題があります。基本的に、カートに x 個のアイテムがある場合、"You have x item(s) in the cart" というテキストがエコーされます。ただし、アイテムがない場合は、「カートにアイテムがありません」と表示されるはずですが、何も表示されません。私は何を間違っていますか?

<?php 
    $array = unserialize($_SESSION['__vm']['vmcart']); 
    foreach($array->products as $product){
        $amount = $product->amount;
        if ($amount != 0){ echo "You have $amount item(s) in the cart."; } 
        else { echo "You don't have any items in the cart."; } 
    }
?>
4

2 に答える 2

0

コードが for each ループに入らないためです。

<?php 
    $array = unserialize($_SESSION['__vm']['vmcart']); 
    if (count($array->products) > 0) {
      foreach($array->products as $product){
          $amount = $product->amount;
          echo "You have $amount item(s) in the cart."; 
          /* Do other thinks here. */
      }
    } else { 
     echo "You don't have any items in the cart."; 
    } 
?>

なぜループを使用したいのかわかりません。

于 2013-04-12T22:17:03.237 に答える
0

foreach を削除して、これを使用します

if($size=sizeof($array->products))
echo "You have ".$size." item(s) in the cart.";
else
echo "You don't have any items in the cart.";
于 2013-04-12T22:18:04.867 に答える