2013-06-26 18 views
0

將購物車與「添加到購物車」功能組合在一起。除了從購物車中移除物品之外,一切都可以使用。這裏是什麼,我有簡化的版本,使其更容易看到問題的所在:如何刪除一個PHP SESSION數組中的行?

$action = $_GET['action']; 
$id  = $_GET['id']; 
if ($action == "remove"){ 
    unset($_SESSION['cart'][$product_id][$id]); 
    //Also tried this without sucess: $_SESSION['cart'][$product_id]--; 
} 

foreach($_SESSION['cart'] as $product_id) { 
    echo 'Details'.$product_id['title'].' - '.$product_id['price']; 
    echo '<a href="Cart.php?action=remove&id='.$product_id['product_id'].'">Remove</a>'; 
} 
+0

得到你的答案! :)讓我知道它是否有效 – KyleK

回答

0

應該只能夠打電話:

unset($_SESSION['cart'][$id]); 
+0

我認爲,起初,但可能不會,因爲我們不知道他是否將$ id存儲爲數組鍵,否則他應該搜索並取消設置 – KyleK

0

這會爲ya工作.. 。

if ($action == "remove"){ 
    foreach($_SESSION['cart'] as $key => $value) { 
     if($value['product_id'] == $id) { 
     unset($_SESSION['cart'][$key]); 
     } 
    } 
} 

現在,這不會幫助,如果你要的量添加到每個產品的能力......那麼你就必須檢查是否存在數量與....相應增加....之類的東西那