https://blog.csdn.net/a345263950/article/details/81907275
学习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);