Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Shopping Cart Update Problem

    Basically I have this shopping cart code. There are 3 cases, add, delete and update items in my shopping cart. My problem is with the update part. How can I make sure qty can never be 0 or -1? If it is 0 or less, it automatically sets to 1 again. $key is item_number while $value is quantity. Thanks

    Code:
    <?php
    session_start();
    // Process actions
    $cart = $_SESSION['cart'];
    $action = $_GET['action'];
    switch ($action) {
    	case 'add':
    		if ($cart) {
    			$cart .= ','.$_GET['item_number'];
    		} else {
    			$cart = $_GET['item_number'];
    		}
    		break;
    	case 'delete':
    		if ($cart) {
    			$items = explode(',',$cart);
    			$newcart = '';
    			foreach ($items as $item) {
    				if ($_GET['item_number'] != $item) {
    					if ($newcart != '') {
    						$newcart .= ','.$item;
    					} else {
    						$newcart = $item;
    					}
    				}
    			}
    			$cart = $newcart;
    		}
    		break;
    	case 'update':
    	if ($cart) {
    		$newcart = '';
    		foreach ($_POST as $key=>$value) {
    			if (stristr($key,'qty')) {
    				$item_number = str_replace('qty','',$key);
    				$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
    				$newcart = '';
    				foreach ($items as $item) {
    					if ($item_number != $item) {
    						if ($newcart != '') {
    							$newcart .= ','.$item;
    						} else {
    							$newcart = $item;
    						}
    					}
    				}
    				for ($i=1;$i<=$value;$i++) {
    					if ($newcart != '') {
    						$newcart .= ','.$item_number;
    					} else {
    						$newcart = $item_number;
    					}
    				}
    			}
    		}
    	}
    	$cart = $newcart;
    	break;
    }
    $_SESSION['cart'] = $cart;
    ?>
    Last edited by jhl84; 07-09-2007 at 12:34 PM. Reason: typing error

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    PHP Code:
    if ($value 1) {
        
    $value 0;


  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    PHP Code:
    $items explode(',',$cart); 
    This is a very bad way of storing items. It's not clean, it requires you to constantly 'pack' and 'unpack' the variable, which makes it a lot easier to lose information. Not to mention you're constantly writing extra code just to convert (harder to read and costs more to code.)

    I'd recommend storing them as such (in a most simplistic form):

    PHP Code:
    $cart = array(
             
    '100' => 5// 5 of product no. 100
             
    '102' => //  2 of product no. 102
            // etc.
            
    ); 
    Of course you wouldn't create the array like that, but what you end up with is how you would store it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •