create table if not exists shop_goods(
id int unsigned auto_increment key,
gname varchar(50) not null unique,
price decimal(10,2) not null default 0,
number int unsigned not null default 0,
pic varchar(50) NOT NULL DEFAULT 'jd.jpg'
);
//购物需要的数据篇
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
</style>
</head>
<body>
<?php
for ($i=1; $i < 50; $i++) {
# code...
$pic=mt_rand(1,4);
$sql="insert shop_goods(gname,price,pic) values('apple{$i}','488{$i}','{$pic}.jpg');";
echo $sql."<br />";
}
?>
</body>
</html>
//下面的页面都是重要页面
<?php
$link=mysqli_connect('localhost','root','','test44');
mysqli_set_charset($link,'utf8');
$sql="select id,gname,price,pic from shop_goods";
//然后是运行
$result=mysqli_query($link,$sql);
//存在并且有条数就执行下面的啊
if($result&&mysqli_num_rows($result)>0)
{//这里是显示$result里面的数据,并赋给$row并且赋值到一个新数组里面啊,因为不知道多少条数据所以为[]
while($row=mysqli_fetch_assoc($result))
{
$rows[]=$row;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{padding: 0px;margin: 0px;}
#container{width: 960px;border: 1px solid red;}
.pro{width: 200px;border: 1px solid red;float: left;
margin-left: 30px;margin-bottom: 10px;text-align: center;
}
.clearfix
{
overflow: hidden;zoom:1;clear: both;
}
</style>
</head>
<body>
<a href="gw3.php">查看购物车</a>
<div id="container" class='clearfix'>
<?php foreach($rows as $row):?>
<div class="pro">
<p><img src="./<?php
echo $row['pic'];
?>"></p>
<p>名称:<?php
echo $row['gname'];
?></p>
<p>价格:<?php
echo $row['price']
?>元</p>
<p><a href="gw2.php?act=addCart&id=<?php
echo $row['id'];
?>">加入购物车</a></p>
</div>
<?php
endforeach;
?>
</div>
</body>
</html>
<?php
error_reporting('E_ALL&~E_NOTICE');
header('content-type:text/html;charset=utf-8');
session_start();
$act=$_GET['act'];
//然后是获取id的值
$id=(int)$_GET['id'];
//然后是连接数据库
$link=mysqli_connect('localhost','root','','test44');
//然后是字符集
mysqli_set_charset($link,'utf8');
//然后是添加购物车操作
//当购物车有物品并且点击购物车的时候就执行
switch($act)
{
case 'addCart':
//添加购物车的id,看看是哪一条物品(编号)
$sql="select * from shop_goods where id='{$id}'";
//然后是运行把
$result=mysqli_query($link,$sql);//运行起来如果有数据并且数据是一条的话就执行下面的啊
//然后是存在也就是有东西并且是一条的话就
if($result&&mysqli_affected_rows($link)==1)//意思是找到了
{
//就取出来
$row=mysqli_fetch_assoc($result);
//然后是检测商品是否存在
if(!isset($_SESSION['cart']))
{
$_SESSION['cart']=array();//为空
}
//如果存在的话就+1
if(array_key_exists($row['id'],$_SESSION['cart']))//判断键名是否存在与数组中啊,第二个是哪一个数组中啊
{
$_SESSION['cart'][$row['id']]['count']++;
}else
{
$row['count']=1;//放count给数组中,并且为1
$_SESSION['cart'][$row['id']]=$row;
}
exit("<script>alert('加入购物车成功');
location.href='gw1.php';
</script>");
}
break;
case 'delGood':
unset($_SESSION['cart'][$id]);
exit("<script>alert('删除成功');
location.href='gw3.php';
</script>");
break;
case 'delCart':
if(isset($_COOKIE[session_name()]))
{
setcookie(session_name(),session_id(),time()-1);
}
$_SESSION=array();
session_destroy();
exit("<script>alert('清空购物车成功');location.href='gw1.php';</script>");
break;
}
?>
<?php
//购物车页面
error_reporting('E_ALL&~E_NOTICE');
header('content-type:text/html;charset-utf-8');
session_start();
//如果为空,证明没有物品在里面,,所以要去上一页买东西啦
if(empty($_SESSION['cart']))
{
exit("<script>alert('请先购物物品');
location.href='gw1.php';
</script>");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>购物车列表</h1>
<table>
<tr>
<td>商品编号</td>
<td>商品图片</td>
<td>商品名称</td>
<td>商品价格</td>
<td>商品数量</td>
<td>操作</td>
</tr>
<?php
error_reporting('E_ALL&~E_NOTICE');
header('content-type:text/html;charset-utf-8');
$sum=0;$i=1;
foreach($_SESSION['cart'] as $data): {
}//cart里面的东西,一个一个遍历给$data
?>
<tr><td><?php
header("Content-Type: text/html;charset=utf-8");
echo $data['id'];
?></td><td>
<img src="<?php
echo $data['pic'];
?>"width='30' height='30'>
</td>
<td><?php
header("Content-Type: text/html;charset=utf-8");
echo $data['gname'];
?></td>
<td><?php
header("Content-Type: text/html;charset=utf-8");
echo $data['price'];
?>元</td>
<td><?php
header("Content-Type: text/html;charset=utf-8");
echo $data['count'];
?></td>
<td>
<a href="gw2.php?act=delGood&id=<?php
echo $data['id'];
?>">删除商品</a>
</td>
</tr>
<?php
header("Content-Type: text/html;charset=utf-8");
$sum+=$data['price']*(int)$data['count'];endforeach;
?>
<tr colspan='6'>
<td>
合计<?php
header("Content-Type: text/html;charset=utf-8");
echo $sum;
?>元|<button>支付</button>|<a href="gw1.php">返回继续购物</a>
</td>
</tr>
<tr><td>
<a href="gw2.php?act=delCart&id=<?php
header("Content-Type: text/html;charset=utf-8");
echo $data['id'];
?>">删除所有商品</a>
</td></tr>
</table>
</body>
</html>