php基于thinkphp的购物车--能够更改商品数量

action部分:

 public function buygoods()
    {
     
     $goods = new GoodsModel();
     
   
     $selection["goods_id"] = $_REQUEST["goods_id"];
     
     $goods1 = $goods->where($selection)->find();
     $goods1["num"] = 1;
     //print_r($goods1);
             
     if($_SESSION["goods"]=="")
     {
      $_SESSION["goods"] = array();
      array_push($_SESSION["goods"], $goods1);
                            
     }
     else
     {
      
      array_push($_SESSION["goods"], $goods1);
      
     }
    
    
      // print_r($_SESSION["goods"]);
     back();
   
   
    }
    

    function cart()

   {
          
     if($_SESSION["goods"]==""){
   
     alert("购物车为空,请添加花卉后查看!");
     back();
      
     }else{
     $sum = 0;
     for($i=0;$i<count($_SESSION["goods"]);$i++ )
     {
      $k = $i+1;  //更改购物车商品数量
      $j = "goods_num".$k;
      if ($_REQUEST["$j"]==""){}else{
       $_SESSION["goods"]["$i"]["num"] = $_REQUEST["$j"];}
      $sum += $_SESSION["goods"][$i][goods_price]*$_SESSION["goods"][$i][num];
      $_SESSION["goods"][$i]["i"] = $i;
                  
     }          
     
     $this->assign("price",$sum);
     $this->assign("num",count($_SESSION["goods"]));
     $this->assign("goodsCar",$_SESSION["goods"]);
     $this->assign("id",$_SESSION["goods"][$i]["i"]);
     //print_r($_SESSION["goods"]);
     //alert($_SESSION["goods"]["0"]["num"]);
          
     $this->display();}      
          
    }
                    
 public function moveout()
    {   //print_r($_REQUEST["type"]);
     if($_REQUEST['type'] =="all"){
      
      $_SESSION["goods"] = "";
      jump($this->config["url"]."index.php/Index/index");
      
     }else{
      
      $moveout = $_REQUEST["id"];
      array_splice($_SESSION["goods"],$moveout-1,1);     
      jump($this->config["url"] . "index.php/Index/cart");}    
    }
  
 
 
html部分:

cart.htm--不完整只有核心代码

<table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
        <thead>
            <tr>
                <th width="420"><h3>商品名</h3></th>
                <th width="190"><h3>属性</h3></th>
                <th width="80"><h3>价格</h3></th>
                <th width="60"><h3>件数</h3></th>
                <th width="80"><h3>小计</h3></th>
                <th width="100"><h3>操作</h3></th>
            </tr>
        </thead>
        <form action="{$config.url}index.php/Index/cart" name="form" id="form">
        <tbody>
     <volist name="goodsCar" id="goods">
            <tr>
                <td><span class="img_1 img1"> <b>{$goods.goods_name}<span class="img_1_h"><img src="{$config.url}{$goods.goods_litpic}" /></span></b> </span> </td>
                <td>{$goods.goods_color}</td>
                <td>{$goods.goods_price}</td>
                <td align="center"><input type="text" name="goods_num{$i}" id="textfield49" value="{$goods.num}"/></td>
                <td>¥17,800</td>
                <td><a href="{$config.url}index.php/Index/moveout?id={$i}">删除</a>&nbsp;<a href="#">移到收藏</a></td>
            </tr>
         
          </volist>
        </tbody>
      
    </table>
  <div class="text">
    金额小计: {$price}元     <a href="{$config.url}index.php/Index/moveout?type=all" style="margin-left:290px">清空</a> <a href="javascript:form.submit();">更新</a>  <a href="{$config.url}index.php/Index/index" style="margin-left:400px">继续</a>
  <a href="{$config.url}index.php/Index/order">结算</a>
  </div>
 
  </form>

 

更改商品数量是通过提交表单给本页面实现的。每个商品的goods_num{$i} 都直接保存在$_SESSION["goods"]["$i"]["num"]中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值