PHP使用cookie实现简单的购物车功能

学习php过程中尝试用cookie做一下购物车,参考和学习网上的做法,主要用cookie记录一个二维数组

----------------------------------------------------

array(

'商品id1'=>array('名字','库存等'),

'商品id2'=>array('名字','库存等')

);

商品页面

 

function.php 函数页面

<?php
// 加入购物车 
function addCart($gid, $gname)
{
    // 第一次点击创建一个数组
    if (empty(unserialize($_COOKIE['cart']))) {
        $arr = array($gid => array($gname, 1));
        setcookie('cart', serialize($arr));
    } else {
        $arr = unserialize($_COOKIE['cart']);

        // 如果没有这个商品,添加一个商品数组
        if ($arr[$gid][0] != $gname) {
            $arr[$gid] = array($gname, 1);
            setcookie('cart', serialize($arr));
        } else { //如果商品存在数量加1
            $arr[$gid][1]++;
            setcookie('cart', serialize($arr));
        }
    }
}
// 删除一个商品
// $del是get请求的删除id $del = $_GET['del'];
function delCart($shop_car, $del)
{
    unset($shop_car[$del]);
    setcookie('cart', serialize($shop_car));
    echo '<script>window.location.href = "showcar.php"</script>';
}

//清空购物车全部
function delAll()
{
    if (isset($_GET['clean'])) {
        if ($_GET['clean'] == 'all') {
            setcookie('cart', '', time() - 3600);
            echo '<script>alert("清空成功");window.location.href="showcar.php";</script>';
        }
    }
}


// 计算总价格
function totalPrice($shop_car)
{
    require_once 'mysqli.php';
    $price = 0;
    // $kk是随便定义的一个数组名字
    foreach ($shop_car as $kk) {
        $sql = 'select price from one where gname="' . $kk[0] . '"';
        $select = $db->query($sql);
        $result = $select->fetch_object();
        $price += $kk[1] * $result->price;
    }
    return $price;
}
```

shop.php 商品页面
```
<?php
include 'mysqli.php';
$select = $db->query('select * from one');
$result = $select->fetch_assoc();

while ($result) {
    echo '<div style="float:left; width:30%;margin-left:1.5%;text-align:center">
            <img src="' . $result['url'] . '" style="width:200px;height:200px"/>';

    echo '<p>商品id: ' . $result['id'] . ' 名称: ' . $result['gname'] . ' 价格: $' . $result['price'] . '</p>';

    echo '<a href="addcar.php?gid=' . $result['id'] . '">加入购物车</a></div>';
    $result = $select->fetch_assoc();
}

echo '<div style="margin:0 auto; width:150px; font-size:22px">
        <a target="_blank" href="showcar.php">查看购物车</a>
    </div>';

shop.php 商品页面

<?php
include 'mysqli.php';
$select = $db->query('select * from one');
$result = $select->fetch_assoc();

while ($result) {
    echo '<div style="float:left; width:30%;margin-left:1.5%;text-align:center">
            <img src="' . $result['url'] . '" style="width:200px;height:200px"/>';

    echo '<p>商品id: ' . $result['id'] . ' 名称: ' . $result['gname'] . ' 价格: $' . $result['price'] . '</p>';

    echo '<a href="addcar.php?gid=' . $result['id'] . '">加入购物车</a></div>';
    $result = $select->fetch_assoc();
}

echo '<div style="margin:0 auto; width:150px; font-size:22px">
        <a target="_blank" href="showcar.php">查看购物车</a>
    </div>';

addcar.php 加入购物车

<?php
ini_set('error_reporting', 'E_ALL & ~E_NOTICE');
require_once 'mysqli.php';
require_once 'function.php';
$gid = $_GET['gid'];

$sql = 'select gname from one where id=' . $gid;
$select = $db->query($sql);

// 查询错误返回
if (!$select) {
    header('location:shop.php');
    exit;
}

$result = $select->fetch_assoc();
$gname = $result['gname'];

// 查询为空返回,避免空商品加到购物车
if (empty($gname)) {
    header('location:shop.php');
    exit;
}

addCart($gid, $gname);

//setcookie('cart','',time()-3600);
header('location:shop.php');

showcar.php 购物车展示页面

<?php
require_once 'function.php';

ini_set('error_reporting', 'E_ALL & ~E_NOTICE');
$shop_car = unserialize($_COOKIE['cart']);
echo '<h1>购物车</h1>';

if (empty($shop_car)) {
    echo '购物车为空';
    echo '<br /><a href="shop.php">返回首页购物</a>';
    exit;
}

// 展示
$key = array_keys($shop_car);
for ($i = 0; $i < count($shop_car); $i++) {
    echo '商品:' . $shop_car[$key[$i]][0] . ' 数量:' . $shop_car[$key[$i]][1] . '<br />';
    echo '<a href="showcar.php?del=' . $key[$i] . '">删除商品</a><br />';
}

// 总价格
echo '<br />总价格:$' . totalPrice($shop_car);
if (isset($_GET['del'])) {
    $del = $_GET['del'];
    if ($_GET['del'] == $del) {
        delCart($shop_car, $del);
    }
}

// 清空按钮
echo '<br /><a href="showcar.php?clean=all">清空购物车</a><br />';
if (isset($_GET['clean'])) {
    if ($_GET['clean'] == 'all') {
        delAll();
    }
}
```

conn.php
```
<?php
define('host','localhost');
define('user','root');
define('pwd','');
define('dbname','goods');

conn.php

<?php
define('host','localhost');
define('user','root');
define('pwd','');
define('dbname','goods');

mysqli.php

<?php
include 'conn.php';
$db = new mysqli(host,user,pwd,dbname);

初学php...如果有错误请大神指出......

  • 7
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值