2016-08-18 80 views
1

我得到了一個包含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"]); 
} 
+0

燁但它會涉及到服務器和頁面重繪的一次往返 – RiggsFolly

+0

@RiggsFolly你的意思是我必須重做它的代碼才能工作? – twan

+0

看起來你有一個基本上涵蓋了我暗示的答案,看看是否適用於你 – RiggsFolly

回答

0

下面的代碼註釋,並且會給你(種)如何你的想法可以去這樣做用PHP ...

<?php 

    // IF YOU WISH TO USE PHP TO DELETE YOUR PRODUCT FROM THE SESSION, 
    // YOU COULD CHANGE THE VALUE OF THE href ATTRIBUTE OF YOUR LINK 
    // TO CONTAIN THE PRODUCT ID WHICH WOULD BE USED TO DELETE THAT SPECIFIC PRODUCT. 
    // IN THIS CASE, CLICKING ON THE DELETE BUTTON WOULD REDIRECT BACK TO THIS PAGE 
    // BUT WITH SOME QUERY PARAMETERS ATTACHED TO THE URL 

    // SO; FIRST, AT THE TOP OF THE FILE WE CHECK IF THE QUERY PARAM IS THERE: 
    if(isset($_GET['id'])){ 
     // THEN WE KNOW THE ID OF THE PRODUCT WE WANT TO DELETE 
     $productID = $_GET['id']; 

     // NOW YOU CAN VERY EASILY DELETE THIS ID FROM THE SESSION LIKE SO: 
     // IF WE HAVE THE $productID IN OUR SESSION: $_SESSION['product'] 
     // WE CAN SIMPLY UNSET IT.... 
     if($index = array_search($productID, $_SESSION['product'])) { 
      $_SESSION['product'][$index] = null; 
      unset($_SESSION['product'][$index]); 
     } 
    } 

    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'] != ''){ 
       $deleteLink  = "./?id=" . $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="' . $deleteLink . '"> 
          <i class="fa fa-times" aria-hidden="true"></i> 
         </a> 
        </p> 
        <p>'.$shortcat.'</p> 
       </div> 
      </div>'; 
      } 
     } 
    } 

注意

 // THIS IS THE PART THAT MAKES THIS WORK 
     // NOTICE THE $deleteLink WITHIN THE href ATTRIBUTE OF THE ANCHOR TAG.. 
     $deleteLink  = "./?id=" . $product['id']; 

     '<p style="font-weight:bold;">'.$product['title'].' 
      <a href="' . $deleteLink . '"> 
       <i class="fa fa-times" aria-hidden="true"></i> 
      </a> 
     </p>'; 
+0

謝謝,但我不想從我的數據庫中刪除產品,但從我的會話數組中取消設置。 – twan

+0

@twan同樣的原則也適用於這兩種情況......您所要做的只是取消if條款中的id ...代碼已更新爲此效果。 – Poiz

+0

謝謝我得到它的工作,唯一的問題,我不得不按兩次鏈接的產品從會議中刪除。任何想法爲什麼?我添加了原始答案中使用的代碼。 – twan