在href单击时取消会话中的数组值

时间:2016-08-18 12:49:40

标签: php arrays session

我有一个包含id的数组的会话。这些ID用于在引用页面上显示产品(使用id in($ sessionarray))。现在我添加了一个删除图标,我想在单击该链接时删除ID。这可以不使用javascript完成吗?

所有产品都是从我的查询中加载的,如下所示:

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"]);
}

1 个答案:

答案 0 :(得分: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>';