购物车以php原生cookie实现
程序员文章站
2022-03-20 10:36:14
index.php //入口文件 conn.inc.php //数据库连接常量设置 mysqli.php //数据库连接 goods.php //商品展示 goodsshow.php //商品详情页展示 shoppingcart.php //购物车功能 以上摘自php中文网 ......
index.php //入口文件
<?php
/**
* @name index.php
* @decs
* @author 老猫 <18368091722@163.com>
* updated on: 2019/5/30 9:49
*/
include "goods.php";
conn.inc.php //数据库连接常量设置
<?php
/**
* @name conn.inc.php
* @decs 数据库常量配置
* @author 老猫 <18368091722@163.com>
* updated on: 2019/5/30 9:43
*/
define("host",'13.232.50.253');
define("user",'liuyu');
define("pwd",'94492474');
define("dbname",'onecms');
mysqli.php //数据库连接
<?php
/**
* @name mysqli.php
* @decs 数据库连接
* @author 老猫 <18368091722@163.com>
* updated on: 2019/5/30 9:44
*/
include 'conn.inc.php';
$mysqli=new mysqli(host,user,pwd,dbname);
if($mysqli->connect_errno){
die('数据库连接出现错误,请检查配置'.$mysqli->connect_error);
}
goods.php //商品展示
<?php
/**
* @name goods.php
* @decs
* @author 老猫 <18368091722@163.com>
* updated on: 2019/5/30 9:46
*/
header("content-type:text/html;charset=utf-8");
include "mysqli.php";
?>
<div style='width:800px;float:none'>
<h1>商品列表</h1>
<!-- 显示当前系统时间-->
<h3><p id="demo"></p>
<script>
var myvar=setinterval(function(){mytimer()},1000);
function mytimer() {
var d = new date();
document.getelementbyid("demo").innerhtml = d.tolocaletimestring();
}
</script>
</h3>
<form method="get" action="">
<table style="100%" border="1">
<tr>
<td>
<select name="gid">
<option value="0">请选择商品</option>
<?php
/**
* @name show
* @decs 无限级分类
* 关联model:
* @param $fid
* @param $i
* @author 老猫 <18368091722@163.com>
* updated on: 2019/5/30 10:12
*/
function show($fid,$i)
{
global $mysqli;//全球变量关键字,将前面的变量作用域使其在函数内生效
$sql = "select *from goodstype where fid=$fid";
$result = $mysqli->query($sql);
$str=" ";
$i++;
for($n=1;$n<$i;$n++) {
$str .= "---";
}
$id=isset($_get["gid"])?$id=$_get["gid"]:"";
?>
<?php
while ($row = $result->fetch_assoc()) {
?>
<option <?php if($id==$row['id']){echo "selected";}?> id="<?php echo $str.$row["classname"] ?>" value="<?php echo $row["id"] ?>">
<?php echo $str.$row["classname"] ?>
</option>
<?php
show($fid=$row["id"],$i);
?>
<?php
}
}
show(0,0);
?>
<input id="select" type="submit" value="查询"></select></td></tr>
</table>
</form>
</div>
<div style="float: none;width: 600px">
<?php
$id=isset($_get["gid"])?$id=$_get["gid"]:"";
if(!empty($id)){
$sql="select *from goods where goodstypefid=$id or goodstypefstr like '%$id%' and checkinfo=1 and delstate=0";
}else{
$sql="select *from goods";
}
$result=$mysqli->query($sql);
?>
<table border="1" cellpadding="3" cellspacing="0" width="60%">
<tr bgcolor="skyblue">
<?php
while($row=$result->fetch_assoc()){
?>
<td >
<image width="200px" height="200px" src="<?php echo $row["picurl"]?>"></image>
<a title="查看商品详细信息" href="goodsshow.php?id=<?php echo $row["id"]?>"><?php echo $row["title"]?></a>
</td>
<?php
}
?>
</tr>
</table>
</div>
goodsshow.php //商品详情页展示
<?php
/**
* @name goodsshow.php
* @decs
* @author 老猫 <18368091722@163.com>
* updated on: 2019/5/30 10:15
*/
header("content-type:text/html;charset=utf-8");
include "mysqli.php";
?>
<?php
$id=isset($_get["id"])?$_get["id"]:1;
$sql="update goods set hits=hits+1 where id=".$id;
$mysqli->query($sql);
$sql="select *from goods where id=".$id;
$result=$mysqli->query($sql);
while($row=$result->fetch_assoc()){
?>
<div>
<span><?php echo $row["title"] ?></span>
<hr>
<image src="<?php echo $row["picurl"] ?>" width="200"></image>
<br>
数量:-<input type="text" id="buynum" value="1">+
价格:
<del>市场价:<?php echo $row["marketprice"] ?></del>
出售价:<i style="color: red"><?php echo $row["salesprice"] ?></i>
<input type="hidden" id="id" value="<?php echo $row["id"] ?>">
<hr>
内容:<?php echo $row["content"] ?><br>
<a href="javascript:;" onclick="buynow()">立刻购买</a> <a href="javascript:;" onclick="addshoppingcart()">加入购物车</a>
<?php
}
?>
</div>
<script src="jquery.min.js"></script>
<script>
//立即购买
function buynow(){
//先添加到购物车再进行跳转到购买页面
addshoppingcart("buy");
}
//添加到购物车
function addshoppingcart(a){
$.ajax({
url:"shoppingcart.php?a=addshoppingcart",
type:"post",
data:{'buynum':$("#buynum").val(),'id':$("#id").val()},
datatype:"html",
success:function (data) {
if(a=="buy"){
location.href="shoppingcart.php?a=buynow";
}else{
if(data){
alert("添加购物车成功!");
}
}
}
})
}
</script>
shoppingcart.php //购物车功能
<?php
/**
* @name shoppingcart.php
* @decs
* @author 老猫 <18368091722@163.com>
* updated on: 2019/5/30 10:20
*/
header("content-type:text/html;charset=utf-8");
include "mysqli.php";
$a=isset($_get["a"])?$_get["a"]:"";
//添加购物车
if($a=="addshoppingcart"){
$buynum=$_post["buynum"];
$id=$_post["id"];
// echo "<script>alert($buynum+$id)</script>";
if(!empty($_cookie["shoppingcart"]))
$shoppingcart=unserialize($_cookie["shoppingcart"]);
else
$shoppingcart=array();
if(isset($id) && isset($buynum)){
$id=intval($id);
$buynum=intval($buynum);
$shoppingcart[]=array($id,$buynum);
}
setcookie('shoppingcart',serialize($shoppingcart));//商品属性进行序列化保存到cookie中
return true;
}elseif($a=="buynow") {
//下面写购物车页面
if (!empty($_cookie["shoppingcart"])) {
?>
<table width="36%" border="1" cellspacing="0" cellpadding="0">
<tr bgcolor="#87ceeb">
<td width="20%">商品id</td>
<td width="35%" height="30">商品名称</td>
<td width="25%">购买数量</td>
<td width="15%">价格</td>
<td width="5%">操作</td>
</tr>
<tr>
<td height="10" colspan="4"></td>
</tr>
<?php
$totalprice = "";
$shoppingcart = unserialize($_cookie["shoppingcart"]);
foreach ($shoppingcart as $key => $value) {
$keys = array($key);
?>
<tr>
<td><?php echo $value[0] ?></td>
<td height="30">
<?php
$sql = "select *from goods where id=" . intval($value[0]);
$result = $mysqli->query($sql);
$row = $result->fetch_assoc();
$totalprice += $row["salesprice"] * $value[1];
echo '<a href="goodsshow.php?cid=' . $row['goodstypeid'] . '&id=' . $row['id'] . '" class="title" target="_blank">' . $row['title'] . '</a>';
?>
</td>
<td><?php echo $value[1] ?></td>
<td><?php echo $row["salesprice"] * $value[1] ?></td>
<td> <a href="shoppingcart.php?a=delone&key=<?php echo $key ?>" onclick="">取消</a></td>
</tr>
<?php
}
?>
</table>
<hr>
<span style="float: left;width: 250px;height: 150px">
总价格:<?php echo $totalprice ?><a href="">下一步</a> <a href="shoppingcart.php?a=empty">清空购物车</a>
</span>
<?php
}
}elseif($a=="delone"){
$key=$_get["key"];
$shoppingcart=unserialize($_cookie["shoppingcart"]);
unset($shoppingcart[$key]);
if(empty($_cookie)){
setcookie($shoppingcart,"",time()-3600);
}elseif(empty($shoppingcart)){
setcookie("shoppingcart",serialize($shoppingcart));
echo "<div class='shoppingcartempty'>您的购物车目前没有商品!3秒后跳回首页......</div>";
header("refresh:3;url=goods.php");
}else{
setcookie("shoppingcart",serialize($shoppingcart));
header("location:shoppingcart.php?a=buynow");
}
exit();
}//清空购物车
elseif($a=="empty"){
//清除整个cookie保存的商品信息
unset($_cookie["shoppingcart"]);
setcookie("shoppingcart","",time()-3600);
echo "<div class='shoppingcartempty'>您的购物车目前没有商品!3秒后跳回首页......</div>";
header("refresh:3;url=goods.php");
}
以上摘自php中文网
上一篇: 从B2B到+区块链 中农网的产业共建之路
下一篇: 兄弟你啥意思