PHP+JQuery实现购物车


\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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="gj">{$sum}元</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <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();
                    }
                })
            });
        });

点击并拖拽以移动


文章作者: blue—sky
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 blue—sky !
 上一篇
北京市调频呼号 北京市调频呼号
收音机按地市频率表收集内容来自收音机爱好者吧 吧友提供本网站只负责收录,方便查询,不存在任何商业行为,您也可以前往收音机爱好者吧查询.本网页按地市是指,在某地能收到的频率。取数量最多的。北京市 北京市 documen
下一篇 
TP5.1实现简易留言板之二 TP5.1实现简易留言板之二
比较尴尬。。。反正是自己的学习记录。。。无所谓哪里了。 怎么获取留言内容以及精确评论呢?我是用{volist}{/volist}标签做了一个循环然后 <button class="good" id="{$vo.id}" type="b
2020-05-22
  目录