php添加多个购物车方法

时间:2012-02-25 13:28:40

标签: php

我目前能够将一个数量的产品添加到一个或在我的购物车中减少一个但是我正在尝试添加一种方法,我可以提交我输入到文本字段中的数量。

要增加1,这是代码的主要部分。它可以在索引页面找到。

        echo '<a href="cart.php?increase1='.$uniquenum.'"> Plus 1</a>';

这是购物车页面上的代码,用于实现单一操作和操作:

       if (isset($_GET['increase1'])) {
         $result = 'cart_' . $_GET['increase1'];
         $_SESSION[$result] = isset($_SESSION[$result]) ? $_SESSION[$result] + 1 : 0;
       }

如何使用表单和发布或获取请求从索引页面到购物车页面获取$ uniquenum变量。如果我能做到这一点,我可以更新数量。非常感谢提前。

4 个答案:

答案 0 :(得分:0)

// next line breaks for formatting only, take them out
echo '<form action="cart.php" method="get">
    <input type="hidden" name="increasewhat" value="'.$uniquenum.'">
    <label for="increaseby">Increase by</label>
    <input type="text" name="increaseby" value="1">
    <input type="submit" name="increaseyes" value="Yes!">
    </form>';

//real linebreaks from here, parameters assumed to be validated
$result = 'cart_' . $_GET['increasewhat'];
$_SESSION[$result] = (isset($_SESSION[$result]) ? $_SESSION[$result]:0)+$_GET['increaseby'];

答案 1 :(得分:0)

我建议你将属于购物车逻辑的所有代码放入一个自己的类中。然后,您可以在代码中将其用作外观:

// initialize session for Cart
$_SESSION['cart'] || $_SESSION['cart'] = new Cart();

// fetch cart from session
$cart = $_SESSION['cart'];

然后通过购物车提供方法,以简化的形式提供您正在寻找的功能:

$cart->increadeByOne($item);

或者通过完全访问更具差异化级别的封装:

$cart->getItem($item)->getQuantity()->increaseBy($number);

最后一个例子可能看起来很臃肿,但通常最好有一个基础Cart类,这样你就可以更好地处理和测试你的操作。

然后,您可以将其与您的GET请求绑定:

if (isset($_GET['increase1']))
{
    $cart->getItem($_GET['increase1'])->getQuantity->increaseByOne();
}

一些粗略的购物车和数量布局:

Class CartItemQuantity
{
     private $item;
     private $quantity = 0;

     public function __construct(CartItem $item)
     {
         $this->item = $item;
     }

     public function increaseByOne()
     {
         $this->increaseBy(1);
     }
     public function decreaseByOne()
     {
         $this->quantity = max(0, $this->quantity - 1);
     }
     public function increaseBy($number)
     {
         $this->quantity = max(0, $this->quantity + $number);
     }
     public function getQuantity()
     {
         return $this->quantity;
     }
     public function setQuantity($quantity)
     {
         if (is_string($quantity) && ctype_digit($quantity))
         {
             $quantity = (int) $quantity;
         }

         if (!is_int($quantity))
         {
             throw new InvalidArgumentException('Not a valid quantity (%s).', $quantity);
         }

         $this->quantity = max(0, $quantity);
     }
}

Class CartItem
{
     private $quantity;

     ...

     public function __construct()
     {
         $this->quantity = new CartItemQuantity($this);
         ...
     }

     public function getQuantity()
     {
         return $this->quantity;
     }
}

Class CartItems
{
     ...

     /**
      * @return CartItem
      */
     public function getItem($item)
     {
         ...

         return $item;
     }
}

Class Cart
{
     /**
      * @var CartItems
      */
     private $items;
     ...
     public function increaseByOne($item)
     {
         $this->items->getItem($item)->getQuantity()->increaseByOne();
     }

     /**
      * @return CartItem
      */
     public function getItem($item)
     {
         return $this->items->getItem($item);
     }
}

答案 2 :(得分:-1)

<form name="add_to_cart" method="get" action="cart.php">
   <label for="uniquenum">Quantity: </label>
   <input type="text" name="uniquenum" />
   <input type="submit" value="Add to cart" />
</form>

在服务器端:

if (isset($_GET['uniquenum'])) {
  $uniquenum = $_GET['uniquenum'];
  /* Important! Check if it is an integer */
  if(is_int($uniquenum)) {
     $_SESSION[$result] = isset($_SESSION[$result]) ? $_SESSION[$result] + $uniquenum : 0;
   } else {
      echo "Invalid input!";
   }
}

答案 3 :(得分:-1)

<form action='cart.php' method='GET'>
Input quantity:<br/>
<input type='text' name='increase1'><br/>
<input type='submit' value='Increase'/>
</form>
相关问题