Forum Moderators: coopster

Message Too Old, No Replies

Need cookie help please.

         

Simone100

11:40 am on Jun 28, 2007 (gmt 0)

10+ Year Member



Hello, I have a text file named content.txt and it has lines divided by a ~
like this:

text line 1
~
text line 2 and so on.
~

The following code as it is right now is only rotating between number 1 and 2. So the out put of this script is 1 one the first browser visit, and 2 on the next brower visit and then is starts over.
I need something that will also rotate my text lines I just listed above. Can someone please help me encorporate that into the following script? Thank you very much. The basics are there I just don't know how to finish it.

<?php
$maximum = 2;
$a = join ('', file ('content.txt'));
$c = split("~",$a);
$b = (isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum)? intval($_COOKIE['e'])+1 : 1;
setcookie("e", $b, time()+60*60*24*30);
$include_file = "$b";
echo ($include_file);
?>

[edited by: Simone100 at 11:40 am (utc) on June 28, 2007]

dreamcatcher

8:42 am on Jun 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Simone100,

Do the vaues have to be in a text file? Have you thought about storing them in an array [uk.php.net] and then shuffling [uk3.php.net] the array? That would seem to be a better way of doing things. I`m a little unclear of what you mean when you say 'rotate my text lines'?

If you mean just order them randomnly, I think an array would be better.

dc

Simone100

4:39 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



So glad I got a reply, thank you so much. An array would be fine, I just don't know how to do it in php and also keep my cookie working.
The help would be greatly appreciated. Simone

dreamcatcher

8:42 pm on Jun 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Simone100,

Well, instead of the text file, add your data to an array like this:

$data = array(
'first block',
'second block',
'third block'
);

Then shuffle the array:

shuffle($data);

And just echo the first slot:

echo $data[0];

dc

Simone100

3:10 am on Jul 1, 2007 (gmt 0)

10+ Year Member



OK good your back, thanks, the cookie keeps it going from one to the next in order, just adding a one at each page load to go to the next.

So do I need to number each block in the array? If so, how?

Please let me know, thank you very much.

I don't believe I need a shuffle because it isn't a random selection unless for some reason you think it needs to stay.

Simone100

11:56 am on Jul 1, 2007 (gmt 0)

10+ Year Member



<?php
$maximum=2;
$this=array();
$this[1] = 'this is value of 1';
$this[2] = 'this is value of 2';
$a = (isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum)? intval($_COOKIE['e'])+1 : 1;
setcookie("e", $a, time()+60*60*24*30);
$include_file = "$this . $a";
echo ($include_file);
?>

This only returns Array . 1 and 2. Using the word "array" not whats in the array. I've read through the php manual, don't know how to number the array.

mattclayb

12:32 pm on Jul 1, 2007 (gmt 0)

10+ Year Member



Simone, are you trying to output the next line in txt file each time a user revisits the page. So that on visit one line one of the text file will show, on the second line two will show and so on?

Simone100

1:37 pm on Jul 1, 2007 (gmt 0)

10+ Year Member



I sure am and exactly what you said, I hope you can help.

All I need is it to say "this is something 1" on first page load.
"This is whatever 2" on the next page load and so on, whatever text I put in the array.

Its counting up to the maximum number like it is supposed to be and then starting over. But its not associating the cookie countdown to the array numbers or the text associated with them.

So then I though maybe I needed an associative array to associate the cookie numbers to the array and this didn't work either.

<?php
$maximum=3;
$a = (isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum)? intval($_COOKIE['e'])+1 : 1;
setcookie("e", $a, time()+60*60*24*30);
$this = array(array( 1 => 'test1'),
array( 2 => 'test2'),
array( 3 => 'test3'));
$include_file = "$a . $this";
echo($include_file);
?>

[edited by: Simone100 at 1:57 pm (utc) on July 1, 2007]

Simone100

3:58 pm on Jul 1, 2007 (gmt 0)

10+ Year Member



I think I got it, I'll let you know.

[edited by: Simone100 at 4:00 pm (utc) on July 1, 2007]

mattclayb

4:34 pm on Jul 1, 2007 (gmt 0)

10+ Year Member



you're set cookie method is okay, you just need to change the method you use to check the cookie and output the text.

Wht you want is to check the cookie and then use an if statement so -

if ($_COOKIE['yourvalue'] == "value 1"){
print "message 1";
}
if ($_COOKIE['yourvalue'] == "value 2"){
print "message 2";
}

and so on

Simone100

4:48 pm on Jul 1, 2007 (gmt 0)

10+ Year Member



Thanks Matt that helped really a lot and thanks to dream man as well.

The only problem left is another problem I've had all along with opera and php. I asked others here before and no one knew the answer.
Opera won't read the new text on next page load like IE and Mozilla do.

So I tried putting the code above the headers and just this in the body. <?php echo $text[$a];?>
That didn't work. So I tried each of these that opera said I needed to quit cacheing before or after the title of the page:
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" />
<meta http-equiv="Expires" CONTENT="Tue, 3 July 2007 14:25:27 GMT">

But none of that worked either. Anyone know how to get opera to reread php on a new page load when there is cookies? Thanks.

Simone100

5:31 pm on Jul 1, 2007 (gmt 0)

10+ Year Member



Or maybe I'm missing an important cookie value in my cookie? But I sure don't know what it is.

mattclayb

7:30 pm on Jul 2, 2007 (gmt 0)

10+ Year Member



it could be the path to the cookie, in your setcookie function try this -

setcookie("e", $a, time()+60*60*24*30, "/");