2012-02-25 86 views
-1

我目前能够添加一个产品的数量或在我的购物车中减少一个但我试图添加一个方法,我可以提交一个数量,我输入到一个文本域。php多个购物车方法

要增加一个,这是代码的主要部分。它位于索引页面。

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

这是购物车页面上的代码来获取uniquenum和行动:

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

我怎么会从索引页到购物车页面使用表格和后拿到$ uniquenum变量或获取请求。如果我能做到这一点,我可以更新数量。提前谢谢了。

+0

,而不是让你应该使用POST方法。 否则先搜索爬虫找到你的店铺将是第一个购买你所有的股票。 – 2012-02-25 13:48:44

回答

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); 
    } 
} 
-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!"; 
    } 
} 
+0

小心解释downvote? – check123 2012-02-25 13:44:46

-1
<form action='cart.php' method='GET'> 
Input quantity:<br/> 
<input type='text' name='increase1'><br/> 
<input type='submit' value='Increase'/> 
</form> 
+0

如何评论downvote? – heximal 2012-02-25 15:44:57

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']; 
+0

嗯,当我尝试我得到一个SQL错误 – user1060187 2012-02-25 14:20:10

+0

回应你的SQL语句并发布它 – 2012-02-25 14:23:52

+0

我宁可不要在这里发布太多的代码。你有MSN吗?或者我可以给你发电子邮件? – user1060187 2012-02-25 14:30:52