views:

24

answers:

1

I get an unneeded number combination.

($_COOKIE):

2, 3, 4, 5, 6, 7, 8, 901234567890123456789, 30

Should be ($_COOKIE):

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12... (till) 30

$_Get['id']="1"; (sorry, forgot to post it.)

Why that happens?

The code:

<?
ob_start(); 
$id=$_GET['id'];
if (!empty($id)){
    $id=str_replace('a9_','', $id);
    $value=$_COOKIE['NaudingasURL'];
    $exp = explode(", ", $value);
    if(in_array($id, $exp)){
        $value2=str_replace(', '.$id,"", ', '.$value);
        $value2=substr($value2, 2, strlen($value2));
        echo'r';
    }
    else{
        $value2=$value.', '.$id; echo'a';
    }
setcookie("NaudingasURL", $value2);
}
ob_end_flush();
?>

I'm calling it with Jquery ajax, but I don't thinks that's the problem.

+2  A: 

You are replacing every ",1" with and empty string. So 10 will be 0 and so on...

But i dont understand what exactly you want to achieve?

Ok, if it was what Max said, that you could do it like this:

$exp = explode(", ", $value);
if(in_array($id, $exp)){
    for ($i=0; $i<count($exp); $i++) {
      if ($exp[$i] == $id) {
         unset($exp[$i]);
      }
    }
    $value2 = implode(", ", $exp);
}
else{
    $value2 = implode(", ", $exp).', '.$id;
}
TheCandyMan666
As far as I understood, he gets the cookie containing the list of ID's, removes from there the current ID, and saves the cookie back. Probably to indicate that the given ID is no longer needed for some future operation.
Max
True, Max. So as i can see I replace every ", 1" and every ", 2" so my 10, 11, 12 and 21, 22, 23 turns to 012 and 123. Thanks for the answer! And what I want to achieve is http://www.naudingas.lt/?p=nustatymai
ne5tebiu
Ok, then i just added some code which should work.May can still be optimized ;)
TheCandyMan666
OK I'll test it
ne5tebiu
Yeah, for example you can do it like this (I was too late with my answer, as I'm not a PHP programmer :D ): http://pastebin.com/KtUfPfMq . First - you can use array_search. Second - you don't need to implode it back in 'else' part - you already have $value.
Max