我得到了一個包含id數組的會話。這些id用於在報價頁面上顯示產品(使用($ sessionarray)中的id)。現在我添加了一個刪除圖標,並且我想在單擊該鏈接時刪除id。這可以做到不使用JavaScript?在href點擊會話中取消設置數組值
所有產品都從我的查詢加載像這樣:
if(count($_SESSION['product']) == 0){
echo 'No products added';
}else{
foreach($offertecr as $product){
if (strlen($product['introtext']) > 100){
$shortcat = substr($product['introtext'], 0, 100) . '...';
}else{
$shortcat = $product['introtext'];
}
$offerte_imgs = $product['image_intro']; // Get image parameters of the article
$offertepic = json_decode($offerte_imgs); // Split the parameters apart
if($offertepic->{'image_intro'} != ''){
$image = 'cms/'.$offertepic->{'image_intro'};
}else{
$image = 'http://www.website.nl/_extern/web/cms/images/Producten/Untitled-7.jpg';
}
if($product['id'] != ''){
$offerteoverzicht .= '
<div class="row productofferte">
<div class="col-md-6 offerteimg">
<img src="'.$image.'">
</div>
<div class="desc">
<p style="font-weight:bold;">'.$product['title'].' <a href="#"><i class="fa fa-times" aria-hidden="true"></i></a></p>
<p>'.$shortcat.'</p>
</div>
</div>';
}
}
}
當在說明中的鏈接按下我想從會話(這是一個數組)刪除ID。
代碼我使用(關於這一點我必須按兩次刪除按鈕它有效):
if(isset($_GET['id'])){
$productID = $_GET['id'];
$key=array_search($_GET['id'],$_SESSION['product']);
if($key!==false)
unset($_SESSION['product'][$key]);
$_SESSION["product"] = array_values($_SESSION["product"]);
}
燁但它會涉及到服務器和頁面重繪的一次往返 – RiggsFolly
@RiggsFolly你的意思是我必須重做它的代碼才能工作? – twan
看起來你有一個基本上涵蓋了我暗示的答案,看看是否適用於你 – RiggsFolly