2013-05-21 107 views
0

我一直在絞盡腦汁三天,仍然找不到解決我從會話數組中刪除項目的問題。刪除會話數組中的項目

我正在學習PHP以及如何構建購物車;我知道有框架,但我真的很想學習編程,解決問題並創建事物。

我的問題是這樣的:說我放在車三個項目...

  1. 餅乾
  2. 甜甜圈
  3. 蛋糕

當我在購物車中刪除項目,列表中的所有其他項目(在該項目下方)也會被刪除。 如果刪除DONUTS,紙杯蛋糕也被刪除。 如果我刪除Cookies,整個列表被刪除/清除

但是,如果我從底部開始(餅乾)上的名單,它工作正常;它只會刪除我點擊的那個。

這是我的代碼以及其他嘗試。還沒有造型,只是在邏輯上工作。

<?php 

session_start(); 

$prodid = $_POST['prodid']; 
$quantity = $_POST['quantity']; 
$empty= $_POST['empty']; 
$removed = $_POST['remove']; 

    if (isset($empty)) 
    { 
    unset($_SESSION['shop_cart']); 
    } 


if (!isset($_SESSION['shop_cart']))//Checking if Session is Set or empty 
    { 
     unset($_SESSION['shop_cart']); 
     echo "Cart is empty"; 
    } else 
    { 
     if(count($_SESSION['shop_cart'])==NULL || count($_SESSION['shop_cart'])==0) 
     { 
     echo "Cart is empty"; 
     } 
    } 



/////// THIS IS THE SECTION THAT I'VE BEEN FIDDLING WITH --Am I on the right track 
    if (isset($removed)) 
    { 
    foreach ($_SESSION['shop_cart'] as $cart_line_item => $item) 
     { 
     if($prodid==$item['prodid']) 
      { 
      unset($_SESSION['shop_cart'][$cart_line_item]); ///9:47pm May 20, 2013 
      break; 
      } 
     } 
    } 
/////////////////////////////////////////////////////////////////////////////////// 


//Check if item is already in cart 
foreach ($_SESSION['shop_cart'] as $cart_line_item => $item) 
    { 
    if ($item['prodid']==$prodid) 
     { 
     $quantity=0; 
    echo "In Cart Already <br/>"; 
     break;   
     } 
    } 

$cap=count($_SESSION['shop_cart']); 
$_SESSION['shop_cart'][$cap]['prodid']=$prodid; 
$_SESSION['shop_cart'][$cap]['quantity']=$quantity; 
$_SESSION['shop_cart'][$cap]['description']=$description; 

//Prevent Items with zero quantity 
foreach ($_SESSION['shop_cart'] as $cart_line_item => $item) 
    { 
    if ($item['quantity'] == 0 || $item['quantity'] == NULL) 
     { 
     unset($_SESSION['shop_cart'][$cart_line_item]); 
     break; 
     } 
    }  


$total=0; //For Price 

if (isset($_SESSION['shop_cart'])) 
    { 
    for($i=0;$i<count($_SESSION['shop_cart']);$i++) 
    { 
     $prodid=$_SESSION['shop_cart'][$i]['prodid']; 
     $quantity=$_SESSION['shop_cart'][$i]['quantity']; 
     $description=$_SESSION['shop_cart'][$i]['description']; 

     $query = "SELECT prodid, description, price FROM products WHERE prodid = $prodid"; 
     $result = mysqli_query($hook, $query); 
     $row = mysqli_fetch_assoc($result); 

     $prodid = $row['prodid']; 
     $price = $row['price']; 
     $description= $row['description']; 

     $subtotal = $price * $quantity; 
        $total += $subtotal; 

echo "$description($prodid)---Quantity: $quantity--- $$price";  

echo "<form action=\"$_SERVER[SCRIPT_NAME]\" method=\"post\" >"; 
echo "<INPUT TYPE=\"submit\" name=\"remove\" VALUE=\"Remove\">"; 
echo "<input type=\"hidden\" name=\"prodid\" value=$prodid />\n"; 
echo "<input type=\"hidden\" name=\"counter\" value=\"$i\"/>\n"; 
echo "</FORM>"; 

echo "--------------------------<br/><br/>"; 

    }                
    } 

echo "TOTAL $total <br/><br/>"; 

echo '<pre>'. var_dump($_SESSION['shop_cart']).'<pre/>'; 

echo "<br/>"; 

echo "<form action=\"$_SERVER[SCRIPT_NAME]\" method=\"post\" >"; 
echo "<INPUT TYPE=\"submit\" name=\"empty\" VALUE=\"Empty Cart \">"; 
echo "</FORM>"; 

?> 

這些版本代碼產率相同的結果的那個部分的

A.

$counter= $_POST['counter']; 
echo "COUNTER $counter"; 

    if (isset($removed)) 
    { 
    unset($_SESSION['shop_cart'][$counter]); 
    continue; 
    } 

B.

if (isset($removed)) 
for($i=0;$i<count($_SESSION['shop_cart']);$i++) 
    if($prodid==$_SESSION['shop_cart'][$i]['prodid']) 
     { 
     $_SESSION['shop_cart'][$i]=$quantity==0; 
      continue; 
     } 

C.

if (isset($removed)) 
for($i=0;$i<count($_SESSION['shop_cart']);$i++) 
    if($prodid==$_SESSION['shop_cart'][$i]['prodid']) 
     { 
     unset($_SESSION['shop_cart'][$i]['prodid']); 
    //OR 
     //unset($_SESSION['shop_cart'][$i]); 
     continue; 
     } 

這並不是在所有

if (isset($removed)) 
{ 
foreach ($_SESSION['shop_cart'] as $cart_line_item => $item) 
     { 

     if($prodid==$item['prodid']) 
      { 
      unset($_SESSION['shop_cart']['prodid']); 
      unset($_SESSION['shop_cart']['quantity']); 
      unset($_SESSION['shop_cart']['description']);   
      continue; 
      }  
} 
} 

對於@Soyale工作:

這是你的意思?:

if (isset($removed)) 
{ 
foreach ($_SESSION['shop_cart'] as $cart_line_item => $item) 
    { 
    if($prodid==$item['prodid']) 
     { 
     unset($_SESSION['shop_cart'][$cart_line_item]); 
     continue; 
     } 
    } 
} 

相同的結果。

+0

的http://stackoverflow.com/questions/2231332/remove-a-variable-from-a-php-session-array –

+1

可能重複我見過該帖子並非重複;在某些方面類似但不相同。我也試過這個帖子的答案,他們不工作;有些甚至與我的結果相同。 – Patty

回答

0

你的問題就在這裏:

for($i=0;$i<count($_SESSION['shop_cart']);$i++) 
    { 
     $prodid=$_SESSION['shop_cart'][$i]['prodid']; 
     $quantity=$_SESSION['shop_cart'][$i]['quantity']; 
     $description=$_SESSION['shop_cart'][$i]['description']; 

     $query = "SELECT prodid, description, price FROM products WHERE prodid = $prodid"; 
     $result = mysqli_query($hook, $query); 
     $row = mysqli_fetch_assoc($result); 

     $prodid = $row['prodid']; 
     $price = $row['price']; 
     $description= $row['description']; 

     $subtotal = $price * $quantity; 
        $total += $subtotal; 

echo "$description($prodid)---Quantity: $quantity--- $$price";  

echo "<form action=\"$_SERVER[SCRIPT_NAME]\" method=\"post\" >"; 
echo "<INPUT TYPE=\"submit\" name=\"remove\" VALUE=\"Remove\">"; 
echo "<input type=\"hidden\" name=\"prodid\" value=$prodid />\n"; 
echo "<input type=\"hidden\" name=\"counter\" value=\"$i\"/>\n"; 
echo "</FORM>"; 

echo "--------------------------<br/><br/>"; 

    }                
    } 

您shuld使用的foreach而不是爲。爲什麼? 因爲使用未設置時鍵被刪除。 基本陣列即

array (size=2) 
    0 => 
    array (size=3) 
     'prodid' => string '1' (length=1) 
     'quantity' => string '2' (length=1) 
     'description' => null 
    1 => 
    array (size=3) 
     'prodid' => string '2' (length=1) 
     'quantity' => string '2' (length=1) 
     'description' => null 
    2 => 
    array (size=3) 
     'prodid' => string '3' (length=1) 
     'quantity' => string '2' (length=1) 
     'description' => null 

刪除產品與prodid=2後,將是這樣的:

array (size=2) 
    0 => 
    array (size=3) 
     'prodid' => string '1' (length=1) 
     'quantity' => string '2' (length=1) 
     'description' => null 
    2 => 
    array (size=3) 
     'prodid' => string '3' (length=1) 
     'quantity' => string '2' (length=1) 
     'description' => null 

決賽桌的長度爲2。當你使用這樣的for($i=0;$i<count($_SESSION['shop_cart']);$i++)然後第二個產品將更新顯示(因爲鍵)。甚至更多沒有索引1的行,所以empy行將被放置在那裏。

看這行:

$total=0; //For Price 

if (isset($_SESSION['shop_cart'])) 
    { 
    for($i=0;$i<count($_SESSION['shop_cart']);$i++) //-> this is a bad idea please change this to foreach 
    { 
+0

感謝您的回覆。我認爲沒有人會有想法。我沒有在我的示例代碼中加入foreach,並得到了相同的結果。 這是你的意思?: 如果(isset($刪除)){ 的foreach ($ _SESSION [ 'shop_cart']爲$ cart_line_item => $項目) { 如果($ PRODID == $項目['prodid']) { unset($ _ SESSION ['shop_cart'] [$ cart_line_item]); 繼續; } } } – Patty

+0

我編輯了我原來的帖子,以顯示我嘗試過的foreach。 – Patty

+0

正如我指導,如果我有購物車中的三個項目,我點擊刪除第二個項目,這就是我已經在var dump:array(1){[0] => array(3){[「prodid 「] => string(2)」13「[」quantity「] => string(1)」1「[」description「] => NULL}} – Patty