Cookies and simple data manipulation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dink87522
Forum Newbie
Posts: 7
Joined: Mon Dec 28, 2009 10:19 pm

Cookies and simple data manipulation

Post by dink87522 »

This should be simple, but I can't get it to work now.

$score = $_COOKIE["score"];
if(isset($_COOKIE["hsPrev"])){
$hsPrevious = $_COOKIE["hsPrev"];
$hsPreviousSort = explode(",", $hsPrevious, 6);
}else{
$hsRecord = $score;
setcookie("hsPrev", $hsRecord);
}

the above doesn't work and the code I had which was mostly working is gone now.

$score will equal say 5.
This score is to be stored in the cookie hsPrev.

Say the next score = 2. Should be stored/appended to the end of the cookie also i.e. "5,2"

I only want their to be 5 scores in the cookie (i.e. say "4,2,12,3,9"), the 5 latest, so the older scores should be removed and this is predominantly where I am having trouble. Can someone help me with this?
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Cookies and simple data manipulation

Post by manohoo »

Is this what you were looking for?

Code: Select all

<?php
$a = array(1,2,3,4,5); //sample array
print_r($a); //Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
array_shift($a); //removes the first element... the oldest value in your case
$a[] = 6; // appends a new value to the array
print_r($a); //Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 ) 
?>
Post Reply