[英]how to unset a specific key value from a session array in php
我有一個用於預訂產品的預訂車,在某個時候,用戶將能夠從其購物車中刪除特定的產品。
我需要從會話數組cos刪除產品的幫助,在嘗試刪除產品時,我不斷收到錯誤消息“致命錯誤:無法取消設置字符串偏移量”。
這是刪除的代碼
<?php
if(isset($_GET['inh_arr_key']) && isset($_GET['c_id'])) {
$inh_arr_key = $_GET['inh_arr_key'];
$del_c_id = $_GET['c_id'];
unset($_SESSION['inh_cart'][$inh_arr_key]);
$inh_cart = $_SESSION['inh_cart'];
// Parse the cart session variable
$inh_arr = explode(',',$inh_cart);
if (array_key_exists($inh_arr_key, $inh_arr)) {
echo '<p class="err_msg">Unable to delete selected course from your In-house course booking cart.</p>';
}
else{
$del_inh_query_course_info = @mysql_query("select * from inhouse_prod where id='".$del_c_id."'");
$del_inh_course_det = @mysql_fetch_assoc($del_inh_query_course_info);
$del_inh_course_title = $del_inh_course_det['title'];
echo '<p class="ok_msg"><strong>'.$ex_inh_course_title.'</strong> has been deleted from your In-house course booking cart.</p>';
}
}
?>
我已經獲取並附加了每個產品的數組鍵和值。 因此,這是由$ _GET檢索的,它們位於這些變量中
$inh_arr_key = $_GET['inh_arr_key'];
$del_c_id = $_GET['c_id'];
只是為了提供有關該問題的更多詳細信息,下面是將產品添加到購物車的代碼,它工作正常
<?php
$c_id = $_GET['c_id'];
session_name("inh_cart");
session_start();
$inh_cart = $_SESSION['inh_cart'];
if ($inh_cart) {
$get_inh_arr = explode(',',$inh_cart);
if(in_array($c_id, $get_inh_arr)){
$inh_cart = $inh_cart;
?>
<script language="javascript">
window.location = "user_allc_booking.php?ex_inh_cid=<?php echo $c_id; ?>";
</script>
<?php
}
else {
$inh_cart .= ','.$c_id;
}
} else {
$inh_cart = $c_id;
}
$_SESSION['inh_cart'] = $inh_cart;
?>
<script language="javascript">
window.location = "user_allc_booking.php";
</script>
<?php
$inh_query_course_info = @mysql_query("select * from inhouse_courses where id='".$c_id."'");
$inh_course_det = array();
$inh_course_det = @mysql_fetch_assoc($inh_query_course_info);
$inh_course_title = $inh_course_det['title'];
?>
例如,如果購物車中包含3個產品,那么我執行var_dump($ _ SESSION ['inh_cart']);
輸出將是:
string(8) "20,48,24"
確實需要刪除產品代碼有什么問題。 需要幫助。 謝謝!
由於某些原因, $_SESSION['inh_cart']
似乎是一個字符串,因此它試圖取消不允許在字符串的索引$inh_arr_key
上的字符。
看起來您將其作為逗號分隔的列表...因此,要進行取消設置,需要先將其展開,然后再調用取消設置。
但這是一種糟糕的方法。.在混合索引方面,您留出了很多錯誤的余地。 您應該將其設置為數組,然后讓php對其進行序列化/反序列化,這是正常會話行為的一部分。 另外,不要對密鑰使用通用的有序數字索引,而應使用每個產品所獨有的東西,例如SKU或數據庫記錄中的主鍵。
所以放在一起...
將商品添加到購物車:
$c_id = $_GET['c_id'];
session_name("inh_cart");
session_start();
if(!isset($_SESSION['inh_cart']) {
// if we dont have a cart - initialize it as an array
$_SESSION['inh_cart'] = array();
}
$inh_cart &= $_SESSION['inh_cart'];
if(in_array($c_id, $inh_cart)): ?>
<script language="javascript">
window.location = "user_allc_booking.php?ex_inh_cid=<?php echo $c_id; ?>";
</script>
<?php else:
// just append the item to the array
$inh_cart[] = .$c_id;
endif; ?>
<script language="javascript">
window.location = "user_allc_booking.php";
</script>
<?php
// not sure what youre trying to do here but ok...
$inh_query_course_info = @mysql_query("select * from inhouse_courses where id='".$c_id."'");
$inh_course_det = array();
$inh_course_det = @mysql_fetch_assoc($inh_query_course_info);
$inh_course_title = $inh_course_det['title'];
?>
然后從購物車中刪除:
<?php
// all processing at the top - easier to read -
// use the $error variable to tell what message to display
$error = false;
if(!isset($_GET['c_id'])) {
$error = true;
} else {
$del_c_id = $_GET['c_id'];
$del_key = array_search($del_c_id, $_SESSION['inh_cart']);
if($del_key) {
unset($_SESSION['inh_cart'][$delkey]);
// get the course info
$del_inh_query_course_info = @mysql_query("select * from inhouse_prod where id='".$del_c_id."'");
$del_inh_course_det = @mysql_fetch_assoc($del_inh_query_course_info);
$del_inh_course_title = $del_inh_course_det['title'];
} else {
$error = true;
}
}
?>
<?php if($error): ?>
<p class="err_msg">Unable to delete selected course from your In-house course booking cart.</p>
<?php else: ?>
<p class="ok_msg"><strong> <?php echo $ex_inh_course_title ?></strong> has been deleted from your In-house course booking cart.</p>
<?php endif; ?>
我的猜測是您的錯誤在這里:
unset($_SESSION['inh_cart'][$inh_arr_key]);
似乎您有一個用逗號分隔的值$_SESSION['inh_cart']
。 它是一個字符串,而不是數組。 因此,在字符串上使用[]
語法實際上是在做類似以下事情:
unset `$_SESSION['inh_cart']` at position starting at [some string value] ($inh_arr_key)
當然,該字符串不具有[某些字符串值]的偏移量,因為其偏移量嚴格是數字形式的。
您需要使用'$ _SESSION ['inh_cart']`作為數組。
另外,您可能不想通過$_GET
設置/取消購物車中的商品。 這是一個非常糟糕的主意,因為當有人使用前進/后退按鈕瀏覽您的網站時,他們(會在自己的腦海中)看到購物車發生了變化。
您可以嘗試確保密鑰存在。
if (array_key_exists($inh_arr_key, $_SESSION['inh_cart'])) {
unset($_SESSION['inh_cart'][$inh_arr_key]);
}
按照@prodigitalson確保$_SESSION['inh_cart']
是數組而不是字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.