\1. 未登录且未注册 ,选购商品,然后退出
\2. 未登录但有账号,未登录下选购商品,退出
3.登录,选购商品退出,
4.登录,选购商品,退出,在其他客户端登录
第一个只能用session或者cookie 为了不给服务器增加负担,我用cookie。
第二个的话,如果用户没有登录,暂时用cookie存着,等他登录了,弄到数据库里,如果他好久不登录,那就直接忘了吧。
第三个的话,我直接往数据库塞
第四个,同第三个。
这里写存入数据库的
步骤:
获取用户的登录状态,当用户点击购买的时候,就自动创建购物车了
public function gouwuche()
{
$test = Controller('index/Index');
$strname = $test->asd_gets();
$this->assign('name', $strname);
$res = Db::table('gouwuche')->where('ggooduid', $strname)->select();
$this->assign('food', $res);
$num = count($res);
$sum = 0;
for ($i = 0; $i < $num; $i++) {
$sum = $sum + $res[$i]['addsum'];
}
$this->assign('sum', $sum);
dump($res);
return $this->fetch();
}
购物车的前端代码
<div id="head">
<h2>购物车</h2>
</div>
<div>
<h1>老板给您磕头了,咚咚咚咚咚咚噼里啪啦,请您马上下单,与您相中此商品的人还有50000000人。请尽快下单。
</h1>
</div>
<div id="car">
<table>
{volist name = "food" id = "vo"}
<tr>
<th>
商品名
</th>
<th>
购买个数
</th>
<th>
单个金额
</th>
<th>
总金额
</th>
<th>
操作1
</th>
<th>
操作2
</th>
<th>
操作3
</th>
</tr>
<tr>
<td>{$vo.ggoodsname}</td>
<td>{$vo.ggoodsnum}</td>
<td>{$vo.ggoodsprice}</td>
<td>{$vo.addsum}</td>
<td><button class="btnjia" type="button" id={$vo.id}>加一件</button></td>
<td><button class="btnjian" type="button" id={$vo.id}>减一件</button></td>
<td><button class="btndels" tyep="button" id={$vo.id}>删除</button></td>
<td></td>
</tr>
{/volist}
</table>
<hr>
<button tyep="button" id="btnadd">共计</button> <span id="gj">{$sum}元</span>
<button tyep="button" id="btndel">下单</button>
</div>
又到了坑爹的jQuery时间
$(document).ready(function() {
$('.btnjia').click(function() {
var ids = $(this).attr('id');
$.ajax({
type: "post",
url: "http://leisureshop.cn/index.php/index/goods/jia",
data: {
'ids': ids,
},
dataType: "json",
success: function(data) {
window.location.reload();
}
})
});
$('.btnjian').click(function() {
var ids = $(this).attr('id');
$.ajax({
type: "post",
url: "http://leisureshop.cn/index.php/index/goods/jian",
data: {
'ids': ids,
},
dataType: "json",
success: function(data) {
window.location.reload();
}
})
});
$('.btndels').click(function() {
var ids = $(this).attr('id');
$.ajax({
type: "post",
url: "http://leisureshop.cn/index.php/index/goods/shanchu",
data: {
'ids': ids,
},
dataType: "json",
success: function(data) {
window.location.reload();
}
})
});
});
嗯 就这样,实现了存到数据库端的购物车
相对应的按钮后端操作
public function jia()
{
if (Request::instance()->isPost()) {
$id = Request::instance()->post('ids');
$res = Db::table('gouwuche')->where('id', $id)->find();
$res['ggoodsnum'] = $res['ggoodsnum'] + 1;
$addsum = $res['ggoodsprice'] * $res['ggoodsnum'];
$q = Db::table('goodstable')->where('goodsname', $res['ggoodsname'])->find(); //仓库减一
Db::table('goodstable')->where('goodsname', $res['ggoodsname'])->update(['goodsnum' => $q['goodsnum'] - 1]);
Db::table('gouwuche')->where('ggoodsname', $res['ggoodsname'])->update(['ggoodsnum' => $res['ggoodsnum'], 'addsum' => $addsum]);
}
}
public function jian()
{
if (Request::instance()->isPost()) {
$id = Request::instance()->post('ids');
$res = Db::table('gouwuche')->where('id', $id)->find();
$res['ggoodsnum'] = $res['ggoodsnum'] - 1;
$addsum = $res['ggoodsprice'] * $res['ggoodsnum'];
$q = Db::table('goodstable')->where('goodsname', $res['ggoodsname'])->find(); //仓库加一
Db::table('goodstable')->where('goodsname', $res['ggoodsname'])->update(['goodsnum' => $q['goodsnum'] + 1]);
Db::table('gouwuche')->where('ggoodsname', $res['ggoodsname'])->update(['ggoodsnum' => $res['ggoodsnum'], 'addsum' => $addsum]);
}
}
public function shanchu()
{
if (Request::instance()->isPost()) {
$id = Request::instance()->post('ids');
$res = Db::table('gouwuche')->where('id', $id)->find();
//仓库加
$q = Db::table('goodstable')->where('goodsname', $res['ggoodsname'])->find();
Db::table('goodstable')->where('goodsname', $res['ggoodsname'])->update(['goodsnum' => $q['goodsnum'] + $res['ggoodsnum']]);
Db::table('gouwuche')->delete($id);
}
}
还有对选购界面,也可以点击多次实现多次购买,也就是坑爹的
$(document).ready(function() {
$(".btn").click(function() {
var y = $("#yname").html();
var x = $(this).attr('id');
$.ajax({
type: "post",
url: "http://leisureshop.cn/index.php/index/index/goumai",
data: {
'id': x,
'datas': y
},
dataType: "json",
success: function(data) {
window.location.reload();
}
})
});
});