Problem with update function

Discussion in 'PHP' started by adamjblakey, Sep 1, 2009.

  1. #1
    Hi,

    I have a function which updates a cart but it is not working correctly. Basically if i have e.g.

    2 x Red Shoes
    3 x Blue Shorts

    And then i change the qty of the blue shorts to 4 and pass to the update function it just completely removes the first lot of items in the list e.g. the 2 x red shorts.

    The data is stored in a session like so:
    58:|Red Shoes|14.95,58:|Red Shoes|14.95,56:|Blue Shorts|8.95,56:Size|Blue Shorts|8.95,56:Size|Blue Shorts|8.95
    Code (markup):
    Here is the code for the function:
    
    case 'update':
    	if ($cart) {
    		$newcart = '';
    		foreach ($_POST as $key=>$value) {
    			if (stristr($key,'qty')) {
    				$id = str_replace('qty','',$key);
    				$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
    				$newcart = '';
    				foreach ($items as $item) {
    					if ($id != $item) {
    						if ($newcart != '') {
    							$item = '';
    							$newcart .= ','.$item;
    						} else {
    							$item = '';
    							$newcart = $item;
    						}
    					}
    				}
    				for ($i=1;$i<=$value;$i++) {
    					if ($newcart != '') {
    						$newcart .= ','.$id;
    					} else {
    						$newcart = $id;
    					}
    				}
    			}
    		}
    	}
    	$cart = preg_replace("/_/i", " ", preg_replace("/(\d+)_(\d+)/i", "$1.$2", $newcart));
    	
    	
    	break;
    }
    
    PHP:
    Thanks in advance,
    Adam
     
    adamjblakey, Sep 1, 2009 IP
  2. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #2
    It seems as though
    case 'update':
        if ($cart) { 
    PHP:
    this line :
            $newcart = ''; 
    PHP:
    is creating the problem as it empties the current contents of $newcart , red shoes and blue shorts you added earlier . try removing the 1st instance of
            $newcart = ''; 
    PHP:
    I hope it helps
     
    killerj, Sep 1, 2009 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    I thought this also and tried this but does not seem to work.
     
    adamjblakey, Sep 1, 2009 IP
  4. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #4
    hmm... is it? Is this post , the case you have posted ..is it within a function ? If so , have you tried declaring $cart as global again within the function ?
    
    function updateSumthing()
    {
    global $cart; 
    switch {
    //switch cases
    }
    } 
    PHP:
    By declaring $cart global within the function, all references to the variable will refer to the global version of $cart which may contain the previously stored items.
     
    killerj, Sep 1, 2009 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    It is not inside a function. This is the whole lot of code:

    
    switch ($action) {
    	
    	case 'add':
            $new = '';
    		$qty = $_POST['qty'];
            for ($i = 0; $i < $qty; $i++)
            {
    				
    				$new .= $_GET['id'];
    				
    				unset($_POST['qty']);
    				unset($_POST['button']);
    				
    				foreach ($_POST as $post => $val) {
                    	$new .= ':' . $val;
    				}
    				
    				$new .= ',';
    				
            }
            $new = substr($new, 0, -1);
            if ($cart)
            {
                    $cart .= ',' . $new;
            }
            else
            {
                    $cart = $new;
            }
            break;
    		
    	case 'delete':
    		if ($cart) {
    			$items = explode(',',$cart);
    			$newcart = '';
    			foreach ($items as $item) {
    				if ($_GET['id'] != $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')) {
    				$id = str_replace('qty','',$key);
    				$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
    				$newcart = '';
    				foreach ($items as $item) {
    					if ($id != $item) {
    						if ($newcart != '') {
    							$item = '';
    							$newcart .= ','.$item;
    						} else {
    							$item = '';
    							$newcart = $item;
    						}
    					}
    				}
    				for ($i=1;$i<=$value;$i++) {
    					if ($newcart != '') {
    						$newcart .= ','.$id;
    					} else {
    						$newcart = $id;
    					}
    				}
    			}
    		}
    	}
    	
    	$cart = $newcart;
    	$cart = preg_replace("/_/i", " ", preg_replace("/(\d+)_(\d+)/i", "$1.$2", $cart));
    	break;
    }
    $_SESSION['cart'] = $cart;
    
    PHP:
    I tried adding global $cart; before the switch and did not work :(
     
    adamjblakey, Sep 1, 2009 IP
  6. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #6
    I need to have a look at your entire file now. I have an hour to spare and will try and fix this up for you... Add me on MSN .
     
    killerj, Sep 1, 2009 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I have to say this is a very poor method of saving cart data. is there any reason you cant save it as a proper array?
     
    JAY6390, Sep 1, 2009 IP
  8. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #8
    I guess it works fine now right ? :)
     
    killerj, Sep 1, 2009 IP