Forum Moderators: coopster

Message Too Old, No Replies

First cookie field isn't working but its else is

         

Simone100

7:22 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Hello, I need to list 4 arrays in the following script so it doesn't choose the same random item twice in a row which works fine. Except the way the following is listed, it only picks a random item from arrays $title1 and $img1 on every next page load. So its showing the else in the second cookie field but not the first cookie field. How do I get it to show items from $title2 and $img2 as well? Please let me know, the help is really appreciated, thank you very much.

[PHP]<?php
$title1[1] = "title1";
$title1[2] = "title2";
$title2[3] = "title3";
$title2[4] = "title4";
$img1[1] = "http://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/BahnhofBludenz.JPG/120px-BahnhofBludenz.JPG";
$img1[2] = "http://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Korean_stew-Budae_jjigae-01.jpg/120px-Korean_stew-Budae_jjigae-01.jpg";
$img2[3] = "http://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Basilica_Cistern%2C_Constantinople.jpg/120px-Basilica_Cistern%2C_Constantinople.jpg";
$img2[4] = "http://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/A330-200F.jpg/120px-A330-200F.jpg";
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){
setcookie("array", "one", time()+60*60*24*180);
$nbr1 = mt_rand(1, count($title2));
$nbr2 = mt_rand(1, count($img2));
}else{
setcookie("array", "two", time()+60*60*24*180);
$nbr1 = mt_rand(1, count($title1));
$nbr2 = mt_rand(3, count($img1));
}
$get1 = $title1[$nbr1];
$get2 = $img1[$nbr1];
echo "<img title='$get1' src='$get2' border='0'>";
?>
[/PHP]

Habtom

6:41 am on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. setcookie("array", "one", time()+60*60*24*180);

Even if that is not the case here, I don't encourage using reserved
words like 'array' to be cookie name.

2. Just before the following line, print_r("mycookie"); and see if that is holding a real value.

Simone100

7:53 am on Jan 23, 2008 (gmt 0)

10+ Year Member



Thanks that is working fine. I'll use another name for it in the future though. Anyone know how to help me with my problem? Please let me know, thank you very much.

jatar_k

2:35 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it never seems to think your cookie is the proper value

change your cookie name and see if that works

using reserved words results in all kinds of weird errors

cameraman

5:31 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you checked the cookie to see if it's got the value you expect?

These lines:
$get1 = $title1[$nbr1];
$get2 = $img1[$nbr1];

are always using $title1 and $img1 regardless of which branch of the if/else has executed. You should probably assign them in the if and else sections instead of below, so you know which arrays to use.

Also I think this line has problems:
$nbr2 = mt_rand(3, count($img1));

I think the 3 needs to be a 1.