2017-02-12 1 views
-1

まず、私は複数のスレッドと質問を見ました。私はまだそれに固執しています。そう私はまず PHPでのセッション配列の更新ページ上の投稿

をやっている私は にこのを通じて私のセッションの配列を埋めるよ、私は検索して、カートを表示しています

その後
 $_SESSION['food_id']=$get_id; 
     $_SESSION['food_name']=$get_name; 
     $_SESSION['food_price']=$get_price; 
     $_SESSION['food_image']=$get_image; 
     $_SESSION['food_quantity']=1; 
     $_SESSION['food_discount']=$get_discount; 

     $cart = array (
     'food_id' => $_SESSION['food_id'], 
     'food_name' => $_SESSION['food_name'], 
     'food_price'=> $_SESSION['food_price'], 
     'food_image'=> $_SESSION['food_image'], 
     'food_quantity' => $_SESSION['food_quantity'], 
     'food_discount' => $_SESSION['food_discount'] 
     ); 

     $_SESSION['cart'][] = $cart; 

ページ買い物かごに入れますcart.phpの配列 から

   $total=""; 
       $item_total=""; 
       $item_discount=""; 
       $item_total_quantity=""; 
       $order_total=""; 
       $total_discount=""; 
       if(isset($_SESSION['cart'])){ 
       foreach ($_SESSION['cart'] as $key => $item) { 

        //Get Cart Subtotal Before calculating discount 
         $total += $item['food_price']; 

         $item_total_quantity=$item['food_price']*$item['food_quantity']; 

         //Get Item discount 
         $item_discount = ($item['food_discount']/100)*$item_total_quantity; 
         //Get Item total 
        $item_total=$item_total_quantity-$item_discount; 
        //Get total quantity 
         $food_quantity=$item['food_total_quantity']; 

         $order_total += $item_total; 
         $total_discount += $item_discount; 
       ?> 
       <tr> 
        <td><a href="menu_single.php?q=<?php echo $item['food_id']; ?>"><img src="<?php echo 'img/menu/'.$item['food_image']; ?>" alt=""><?php echo $item['food_name']; echo $key; ?></a> </td> 
        <td><?php echo $item['food_price']; ?> $</td> 
        <td> 
        <span class="total"> <?php echo $item['food_quantity']; ?></span> 
         <!-- End input group minus & plus --></td> 
        <td><?php echo $item['food_discount']; ?> %</td> 
        <td><span class="total"> <?php echo $item_total; ?> $ </span> <a class="pull-right" href="#"><i class="fa fa-times"></i></a></td> 
       </tr> 
       <?php }} ?> 
数量項目が変更され、ボタンは私がその値に基づいて 更新量に必要なポスト 背中押したときに

は、今私はこだわっています。私が現在していることは、現在です。

if(isset($_POST['apply']{ 
     foreach ($_SESSION['cart'] as $key => $item) { 
      //Updating Quantity 
       $item['food_quantity']=$_POST['quantity']; 
} 

しかし、私はそれは常に私は今、壁に頭を打つことを約てる1に設定されている以前の量を表示する私の望ましい結果を得ていませんよ。どんな提案も役に立ちます。

編集#1 Rendy Eko Prastiyoの答えを読んだ後。今、私はセッション配列を更新することができるよそれだけで私のコードここ

     if(isset($_POST['apply'])){ 
         //$cart[] = $_SESSION['cart']; 
        foreach ($_SESSION['cart'] as $key => $item) { 
        echo $_POST['key']; 
        $_SESSION['cart'][$_POST['key']]['food_quantity'] = $_POST['quantity']; 
        //Get Cart Subtotal Before calculating discount 
         $total += $item['food_price']; 

         $item_total_quantity=$item['food_price']*$item['food_quantity']; 

         //Get Item discount 
         $item_discount = ($item['food_discount']/100)*$item_total_quantity; 
         //Get Item total 
        $item_total=$item_total_quantity-$item_discount; 
        //Get total quantity 
         $food_quantity=$item['food_total_quantity']; 

         $order_total += $item_total; 
         $total_discount += $item_discount; 

         echo "<pre>"; 
         print_r($_SESSION['cart']); 
         echo "</pre>"; 
        } 
        } 

がしますprint_r結果は

Array 
(
    [0] => Array 
     (
      [food_id] => 5 
      [food_name] => New Item 
      [food_price] => 14 
      [food_image] => dishes2.jpg 
      [food_quantity] => 9 
      [food_discount] => 10 
      [food_total_quantity] => 21 
     ) 

    [1] => Array 
     (
      [food_id] => 5 
      [food_name] => New Item 
      [food_price] => 14 
      [food_image] => dishes2.jpg 
      [food_quantity] => 1 
      [food_discount] => 10 
      [food_total_quantity] => 21 
     ) 

    [2] => Array 
     (
      [food_id] => 5 
      [food_name] => New Item 
      [food_price] => 14 
      [food_image] => dishes2.jpg 
      [food_quantity] => 1 
      [food_discount] => 10 
      [food_total_quantity] => 21 
     ) 

) 

Array 
(
    [0] => Array 
     (
      [food_id] => 5 
      [food_name] => New Item 
      [food_price] => 14 
      [food_image] => dishes2.jpg 
      [food_quantity] => 9 
      [food_discount] => 10 
      [food_total_quantity] => 21 
     ) 

    [1] => Array 
     (
      [food_id] => 5 
      [food_name] => New Item 
      [food_price] => 14 
      [food_image] => dishes2.jpg 
      [food_quantity] => 1 
      [food_discount] => 10 
      [food_total_quantity] => 21 
     ) 

    [2] => Array 
     (
      [food_id] => 5 
      [food_name] => New Item 
      [food_price] => 14 
      [food_image] => dishes2.jpg 
      [food_quantity] => 1 
      [food_discount] => 10 
      [food_total_quantity] => 21 
     ) 

)  

Array 
    (
     [0] => Array 
      (
       [food_id] => 5 
       [food_name] => New Item 
       [food_price] => 14 
       [food_image] => dishes2.jpg 
       [food_quantity] => 9 
       [food_discount] => 10 
       [food_total_quantity] => 21 
      ) 

     [1] => Array 
      (
       [food_id] => 5 
       [food_name] => New Item 
       [food_price] => 14 
       [food_image] => dishes2.jpg 
       [food_quantity] => 1 
       [food_discount] => 10 
       [food_total_quantity] => 21 
      ) 

     [2] => Array 
      (
       [food_id] => 5 
       [food_name] => New Item 
       [food_price] => 14 
       [food_image] => dishes2.jpg 
       [food_quantity] => 1 
       [food_discount] => 10 
       [food_total_quantity] => 21 
      ) 

    ) 
+0

* "任意の提案は参考になる" *を$_SESSION['cart'][$key]['food_quantity']直接は設定し

+0

私は 'session_start()'をどこにも見ないので、すべてのスクリプトに1つありますか? – RiggsFolly

+0

@RiggsFolly 100回中99回、彼らはそれを開始しました。驚いた?編集:私はありません。 –

答えて

1

配列は、参照により渡されていないです、ここで1つのアイテムを更新しますforeach ($_SESSION['cart'] as $key => $item)したがって、$item['food_quantity'] = $_POST['quantity']をループ内に設定すると、$_SESSION['cart'][$key]['food_quantity']は変更されません。 2つの解決策があります。$_SESSION['cart'][$key]['food_quantity']を直接設定するか、$item$_SESSION['cart']に渡してください。

if (isset($_POST['apply'] { 
    foreach ($_SESSION['cart'] as $key => $item) { 
     // Updating quantity. 
     $_SESSION['cart'][$key]['food_quantity'] = $_POST['quantity']; 
    } 
} 

参照することにより$_SESSION['cart']$itemを渡します: - エラー報告、のvar_dump、エコー、htmlソース、

if (isset($_POST['apply'] { 
    foreach ($_SESSION['cart'] as $key => &$item) { 
     // Updating quantity. 
     $item['food_quantity'] = $_POST['quantity']; 
    } 
} 
+0

参照渡しを使用する場合、 'foreach($ _SESSION ['cart'] as $ key => $ item)'と 'foreach($ _SESSION ['cart'] as $ key =>& '$ item'の前に' $ item'の前に '$'を追加すると '$ item'が' $ _SESSION ['cart'] [$ key] 'の参照によって渡されます)。 –

+0

はい私はこれを試して、それは動作します。しかし、私はページをリフレッシュするときに動作します。どのアイデアか、私はカート全体のコードを投稿しますか? –

+0

'とはどういう意味ですか、私はページをリフレッシュすると動くのですか? –

関連する問題