• 购物车信息存cookie


    //以商品为单位分别存入到各个不同的cookie中,避免因为cookie值过大,导致数据存储失败
    $cart_info_one[brand_name] = $parent['brand_name'];
    setcookie("cart_info_".$maxindex,serialize($cart_info_one),time()+315360000,'/');
    
    //获取cookie中购物车中信息
    $cart_info_arr[] = unserialize(stripslashes($_COOKIE["$cookie_name"]));//cookie 购物车信息
    
    //主要函数封装方法
    /*
     * 用户登录后将本地购物车cookie和数据库信息进行同步
     * */
    function syn_cart_cookie(){
        if(empty($_SESSION['user_id'])){
            return;
        }
        $sql = "SELECT goods_id FROM ".$GLOBALS['ecs']->table('cart')." WHERE user_id=".$_SESSION['user_id'];
        $cart_goods_id = $GLOBALS['db']->getAll($sql);
        foreach ($cart_goods_id as $cart_goods_id_list){
            $db_cart_goods_id[] = $cart_goods_id_list['goods_id'];
        }
       
        $cart_info_arr = cookie_cart_info();//cookie 购物车信息
        if(!is_array($cart_info_arr)){
            return ;
        }
       
        foreach ($cart_info_arr as $key=>$cart_info_list){
            $cart_info = array();
            if(!in_array($cart_info_list['goods_id'], $db_cart_goods_id)){
                unset($cart_info_list['rec_id']);
                $cart_info_list['user_id'] = $_SESSION['user_id'];
                $cart_info_list['session_id'] = $_SESSION['session_id'];
                $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('cart'), $cart_info_list, 'INSERT');
            }
        }
        clean_cookie_cart();//清除购物车cookie信息
    }
    
    /* 获取cookie中购物车的名称 */
    function cookie_cart_index(){
        $cookie_keys = array_keys($_COOKIE);
        foreach ($cookie_keys as $cookie_keys_list){
            if(strpos($cookie_keys_list,'cart_info_') !== false && is_numeric(ltrim($cookie_keys_list,'cart_info_'))){
                $index[] = ltrim($cookie_keys_list,'cart_info_');
            }
        }
        if(empty($index)){
            return false;
        }
        return $index;
    }
    
    /* 获取cookie中购物车最大的索引值 */
    function cookie_cart_maxIndex(){
        $index = cookie_cart_index();
        if($index === false){
            return -1;
        }
        rsort($index);
        return $index[0];
    }
    
    /* 将cookie中的购物车信息合拼为一个二维数组 */
    function cookie_cart_info(){
        $index = cookie_cart_index();
        if($index === false){
            return;
        }
        $cart_info_arr = array();
        foreach ($index as $index_info){
            $cookie_name = 'cart_info_'.$index_info;
            $cart_info_arr[] = unserialize(stripslashes($_COOKIE["$cookie_name"]));//cookie 购物车信息
        }
        return $cart_info_arr;
    }
    
    /* 清除cookie中购物车信息 */
    function clean_cookie_cart(){
        $index = cookie_cart_index();
        foreach ($index as $cart_info_dex){
            setcookie('cart_info_'.$cart_info_dex,'',time()-100,'/');
        }
        return true;
    }
  • 相关阅读:
    【LOJ】#6432. 「PKUSC2018」真实排名
    【Codechef】BB-Billboards
    【BZOJ】4361: isn
    【BZOJ】4380: [POI2015]Myjnie
    【BZOJ】4292: [PA2015]Równanie
    【LOJ】#121. 「离线可过」动态图连通性
    【BZOJ】4025: 二分图
    【LOJ】#2230. 「BJOI2014」大融合
    【SPOJ】QTREE6-Query on a tree VI
    小白初理解树状数组
  • 原文地址:https://www.cnblogs.com/wanghaokun/p/6104309.html
Copyright © 2020-2023  润新知