2016-08-18 5 views
1

ID 배열이있는 세션이 있습니다. 이 id는 ($ sessionarray의 id를 사용하여) 견적 페이지에 제품을 표시하는 데 사용됩니다. 이제 제거 아이콘을 추가하고 해당 링크를 클릭하면 ID를 제거하고 싶습니다. 이 자바 스크립트를 사용하지 않고 할 수 있습니까?href 클릭시 세션의 배열 값 설정 해제.

모든 제품과 같이 내 쿼리에서로드 :

설명에 링크가 나는 ID가 (배열입니다) 세션에서 제거하고 싶은 누르면
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>';  
     } 
    } 
} 

.

코드 나는 (있는 나는 그것이 효과가 두 번 제거 버튼을 눌러야합니다) 사용 :

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