2016-12-14 2 views
0

今日、私はユーザーのウェブサイトスクリプトのショッピングカートを作成しています。カートにアイテムを追加すると、cartitemsという変数$_SESSIONに格納されます。アイテムのitemidとカートに追加しようとしているアイテムの数量を含む$_SESSION['cartitems']の配列内に配列を格納しています。単にアイテムを追加するだけで、以下のリストにあるコードを使ってうまくいきますが、単純に新しい配列をSESSIONに追加するのではなく、同じアイテムを追加しようとすると、配列のアイテムの価値を高める必要があります。相続人例:配列に項目を追加しますが、配列内に存在する場合はその値を大きくしてください。

-> User 1 visits the website. 
    - Add 5 melons to cart. 
    - Add 3 lemons to cart. 
    - Add 2 more melons to cart. 

私の配列のようなものの印刷になります。それらを追加する目的ではなく、次のようなものになるだろうが...

array(
     array{0 => 1, 1 => 5}, 
     array{0 => 2, 1 => 3}, 
     array{0 => 1, 1 => 2} 
) 

を:

array(
     array{0 => 1, 1 => 7}, 
     array{0 => 2, 1 => 3} 
) 

だから1のitemidの値は7に増加するでしょう。私はまた、余分な2を追加する前に、それが何であるかを知る必要があります。誰かがメロンを追加する方法を見つけることを望んでいないだろうし、ストックフィールドに残っている今私たちだろう!

私はすでに在庫量を渡しています。天気とともに無制限の株式サポートを提供しています。つまり、商品の制限を購入しているので、アイテムを追加する際に必要なすべての情報があります。すでに数を増やすためにすでにそこにある場合は、配列を変更する方法が必要です。ここで私はアイテムを追加するために使用するコードは次のとおりです。

if(isset($_POST['quantity'])) { 
    // Cast quantity to an int for protection 
    $quantity = (int) $_POST['quantity']; 
    if(!empty($quantity) && $quantity > 0) { 
     $errors = 0; 
     // It doesn't support unlimited stock so we check stock level 
     if($unlimstock == "0") { 
      if($quantity > $stock) { 
       $quantity = $stock; 
      } 
      if($buylimit > 0) { 
       if($quantity > $buylimit) { 
        $errors = "1"; 
       } 
      } 
     } 
     if($errors == 0) { 
      $_SESSION['cartitems'][] = array($itemid, $quantity); 
      header("Location: cart.php"); 
      die(); 
     } 
    } 
} 

それは私はすでに午前のように私はそれを追加することができない場合は、値を増加させ、それならばであるならば、それは配列のだかどうかを確認するための最良の方法は何ですかそれはどれくらいの価値があるのか​​分かります。みんなありがとう!あなたの$_SESSION['cartitems']のようにデータを保存する必要があるコードを簡素化するために

答えて

1

$_SESSION['cartitems'] = [ 
    'product_id1' => 'quantity1', 
    'product_id2' => 'quantity2', 
]; 

は次に数量を更新することです:

if (isset($_SESSION['cartitems'][$product_id])) { 
    $_SESSION['cartitems'][$product_id] += $quantity; 
} else { 
    $_SESSION['cartitems'][$product_id] = $quantity; 
} 

$_SESSION['cartitems']構造を変更することができない場合は、あなたがそれを反復処理する必要があります。

$found = false; 
foreach ($_SESSION['cartitems'] as $key => $item) { 
    // I suppose that 0-indexed element stores id 
    if ($item[0] == $product_id) { 
     // I suppose that 1-indexed element stores quantity 
     $_SESSION['cartitems'][$key][1] += $quantity; 
     $found = true; 

     // break as certain element found 
     break; 
    } 
} 
if (!$found) { 
    $_SESSION['cartitems'][] = array($product_id, $quantity); 
} 
+0

非常に良い提案!私はこれを試してみる、ありがとう。 – Kaboom

+0

問題が発生した場合は、現在の '$ _SESSION'のコードを追加しました。だからどちらが簡単かを比較することができます) –

+0

2番目のオプションは、現在の構造体で実装するのが簡単で、セッションのすべての項目を必ずしも知っているわけではありませんので、最初のオプション2番目のオプションはうまく動作します。在庫制限を超えた場合、今すぐ私の小切手を追加する必要があります。 – Kaboom

0

Heres私は最終的な事実chec @U_mulderのおかげです。

// Set that we dont't see it by default 
$found = false; 
foreach($_SESSION['cartitems'] as $key => $item) { 
    if($item[0] == $itemid) { 
     // If it has unlimited stock, who cares, otherwise fact check it 
     if($unlimstock == "1") { 
      $_SESSION['cartitems'][$key][1] += $quantity; 
      $found = true; 
      break; 
     } else { 
      // If it's less than or equal to stock, we can try and add it 
      if(($_SESSION['cartitems'][$key][1] + $quantity) <= $stock) { 
       // If it has a buy limit, we set max to buy limit and check it 
       if($buylimit > 0) { 
        if(($_SESSION['cartitems'][$key][1] + $quantity) <= $buylimit) { 
         $_SESSION['cartitems'][$key][1] += $quantity; 
        } 
       } else { 
        $_SESSION['cartitems'][$key][1] += $quantity; 
       } 
      } 
      // Woot, we found it, so we can update it 
      $found = true; 
      break; 
     } 
    } 
} 
// If it wasn't found, we can add it as a new item. This has been fact checked already 
if(!$found) { 
    $_SESSION['cartitems'][] = array($itemid, $quantity); 
} 
関連する問題