php+cookie簡單實(shí)現(xiàn)購物車功能

部分代碼未完成 只是提供一個(gè)思路

<?php

header("Content-Type: text/html; charset=UTF-8");

class Cart extends MY_Controller {

function __construct() {
    parent::__construct();
    $this->load->model('mobile/goods_class_model', 'class');
    $this->load->model('mobile/goods_model', 'goods');
    $this->load->model('mobile/cart_model', 'cart');
}

/*
 * 首頁
 *
 */

function index() {
    $biao = 0;
    $uid = !empty($_SESSION['uid']) ? $_SESSION['uid'] : 1;
    if ($uid > 0) {
        $cartgoods = $this->cart->getcart("user_id=" . $uid);
        $sku_ids = array_column($cartgoods, 'goods_id');
        $where = "g.id in (" . implode(",", $sku_ids) . ")";
        $goods = $this->goods->getgoodslist($where);
        foreach ($goods as $k => $v) {
            foreach ($cartgoods as $key => $val) {
                if ($v['id'] == $val['goods_id']) {
                    $goods[$k]['num'] = $val['goods_number'];
                }
            }
        }
    } else {
        $date = json_decode($_COOKIE['cart'], TRUE);
        $sku_ids = array_keys($date);
        $where = "g.id in (" . implode(",", $sku_ids) . ")";
        $goods = $this->goods->getgoodslist($where);
        foreach ($goods as $k => $v) {
            foreach ($date as $key => $val) {
                if ($v['id'] == $key) {
                    $num = explode(",", $val);
                    $goods[$k]['num'] = $num['1'];
                }
            }
        }
    }
    $date['goods'] = $goods;
    $this->mobile_show_view('mobile/cart/index', $date);
}

/*
 * 加入購物車
 * $id  商品id
 * $pp  是否一步購物 1.加入購物車  2.立即購買
 * $num 商品數(shù)量    目前暫設(shè)定為1
 * $uid 用戶id
 */

function addcart() {
    $id = $_REQUEST['id'];
    $pp = $_REQUEST['pp'];
    $num = 1;
    $uid = !empty($_SESSION['uid']) ? $_SESSION['uid'] : 1;
    //如果$pp==1  立即購買則執(zhí)行
    if ($pp == 1) {
        
    } else {
        //如果$pp==2  加入購物車則執(zhí)行
        //如果用戶登錄則寫入到數(shù)據(jù)庫內(nèi) 否則寫入到cookie
        if ($uid > 0) {
            $date['user_id'] = $uid;
            $date['session_id'] = $_COOKIE['PHPSESSID'];
            $date['goods_id'] = $id;
            $date['goods_number'] = $num;
            $date['goods_type'] = $num;
            $date['rec_type'] = $num;
            $biao = $this->cart->getcartgoods("goods_id=" . $id . " and user_id=" . $uid);
            if (empty($biao)) {
                $add = $this->cart->addcart($date);
            } else {
                $date['goods_number'] = $biao['goods_number'] + 1;
                $add = $this->cart->updatecart($date, $biao['id']);
            }
        } else {
            //賦值數(shù)組
            $date = json_decode($_COOKIE['cart'], TRUE);
            $sku_ids = array_keys($date);
            if (count($date) > 0 && in_array($id, $sku_ids)) {
                $aa = explode(',', $date[$id]);
                $aa['1'] += 1;
                $date[$id] = implode(',', $aa);
                setcookie('cart', json_encode($date), time() + 3600 * 24, '/');
                echo "1";
            } else {
                $date[$id] = $id . ',' . $num;
                //設(shè)置24小時(shí)后過期
                setcookie('cart_nums', count($date), time() + 3600 * 24, '/');
                setcookie('cart', json_encode($date), time() + 3600 * 24, '/');
                echo "1";
            }
        }
    }
}

/*
 * 刪除購物車商品
 * $id  購物車id
 *
 */

function delcart() {
    $id = $_GET['id'];
    $uid = !empty($_SESSION['uid']) ? $_SESSION['uid'] : 1;
    if ($uid > 0) {
        $this->cart->delcart($id, $uid);
    } else {
        $date = json_decode($_COOKIE['cart'], TRUE);
        unset($date[$id]);
        setcookie('cart_nums', count($date), time() + 3600 * 24, '/');
        setcookie('cart', json_encode($date), time() + 3600 * 24, '/');
        echo "1";
    }
}

/*
 * 購物車商品數(shù)量加減
 * $id  購物車id
 *
 */

function cartnum() {
    $id = $_GET['id'];
    $uid = !empty($_SESSION['uid']) ? $_SESSION['uid'] : 1;
    if ($uid > 0) {
        $this->cart->delcart($id, $uid);
    } else {
        $date = json_decode($_COOKIE['cart'], TRUE);
        unset($date[$id]);
        setcookie('cart_nums', count($date), time() + 3600 * 24, '/');
        setcookie('cart', json_encode($date), time() + 3600 * 24, '/');
        echo "1";
    }
}

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,111評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,408評(píng)論 4 61
  • 我們的辦公室空間狹小,卻被塞納了10個(gè)人。5男5女,需要側(cè)身才能順利出入。一天,領(lǐng)導(dǎo)偶爾進(jìn)行科室視察時(shí),發(fā)現(xiàn)了我們...
    緣在今生_58af閱讀 417評(píng)論 0 1
  • 我的生命富足而美好!感恩今天清晨我又如期醒來!感恩父母賜予我生命,讓我體驗(yàn)感受人生!感恩老公在人生路上相依相伴,不...
    淘淘的簡書閱讀 140評(píng)論 0 0
  • 買家秀 長方形我畫成了方形 冒出來泡有點(diǎn)硬 涂色時(shí)貪快,把鉛筆給抹胡了 現(xiàn)在的節(jié)奏是,低溫暴雨回南天。 意思是你得...
    左手邊的檸檬糖閱讀 371評(píng)論 0 1

友情鏈接更多精彩內(nèi)容