Can't Find The Problem

Discussion in 'PHP' started by adamjblakey, Aug 6, 2009.

  1. #1
    Hi,

    I have a case which is adding underlines in the place of spaces but i cannot see what the cause is?

    This is the case:

    
    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;
    	break;
    
    PHP:
    Can anyone see where the problem is?

    Cheers,
    Adam
     
    adamjblakey, Aug 6, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Underlines? Explain. You may want to do a view source in HTML mode as it may be some HTML that is not closed </u> (before the results of that PHP snippet)

    Otherwise, show us the input you have ($_POST, $cart), expected output, and what you now receive as output.
     
    premiumscripts, Aug 6, 2009 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Before the value is passed to the update it is like this:

    56:Size|Large (68cm x 43cm) - 11.95|11.95 
    Code (markup):
    And after it looks like this:

    56:Size|Large_(68cm_x_43cm)_-_11_95|11_95
    Code (markup):
     
    adamjblakey, Aug 6, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, you're not using any underscores in that piece of code. Are you sure that's where the error is generated?
     
    premiumscripts, Aug 6, 2009 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    It must be as that is the only thing that it is being passed to.
     
    adamjblakey, Aug 6, 2009 IP
  6. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #6
    How can it replace spaces with underscores if this code piece doesn't have anything like that? Same question as premiumscripts, but if it's the only thing that it is being passes to you shouldn't be getting '56:Size|Large_(68cm_x_43cm)_-_11_95|11_95'.
    Also I didn't get what the error is...?
     
    Sky AK47, Aug 6, 2009 IP
  7. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #7
    I know that is why i cannot see where the problem is... The error is that i don't want the _ in the string.
     
    adamjblakey, Aug 6, 2009 IP
  8. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well, it certainly isn't in that piece of code. Post the entire script. Also, what output are you expecting? The exact same as the input?
     
    premiumscripts, Aug 6, 2009 IP
  9. pubdomainshost.com

    pubdomainshost.com Peon

    Messages:
    1,277
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This is a PHP bug. What version are you running? change your request / post / get method and see if that makes any difference OR use string replace to get the values back.
     
    pubdomainshost.com, Aug 6, 2009 IP
    premiumscripts likes this.
  10. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #10
    ^Looks like a PHP bug then :(
    Just simply replace them then at the end of the code then:
    $cart = preg_replace("/_/i", " ", preg_replace("/(\d+)_(\d+)/i", "$1.$2", $newcart));
    Code (markup):
     
    Sky AK47, Aug 6, 2009 IP
  11. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Good catch pubdomainhost, very weird bug.
     
    premiumscripts, Aug 6, 2009 IP
  12. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #12
    Thank you very much, i was starting to think that this was going to be a lost cause.
     
    adamjblakey, Aug 7, 2009 IP