First cookie field isn't working but else is

Discussion in 'PHP' started by Jen-, Jan 21, 2008.

  1. #1
    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
    $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:

     
    Jen-, Jan 21, 2008 IP
  2. webwurks

    webwurks Well-Known Member

    Messages:
    126
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    You are only using the set 1 when defining $get1 & $get2
    Try this:

    <?php
    $title1[1] = "title1";
    $title1[2] = "title2";
    $title2[1] = "title3";
    $title2[2] = "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[1] = "http://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Basilica_Cistern%2C_Constantinople.jpg/120px-Basilica_Cistern%2C_Constantinople.jpg";
    $img2[2] = "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));
    $get1 = $title2[$nbr1];
    $get2 = $img2[$nbr1];
    }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'>";
    ?>
     
    webwurks, Jan 21, 2008 IP
  3. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thats just the big piece I was missing and makes a lot of sense. Couldn't think of it. I sent you something. Thanks a whole lot webwurks!
     
    Jen-, Jan 22, 2008 IP
  4. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Webwurks are you still there? This is the last thing I need help with and I'm almost there except for the very last problem. Or anyone else can help too. There are two arrays. This looks for certain xhtml tags and if it find it, it echo's a line randomly. It also looks for web page files and if it find it echos it randomly as well. I need it to pick a random item from both preg_grep and preg_grep_invert at the same time. Instead of both individually. Can someone tell me how? Please let me know, thank you very much.

    <?php
    $one[1] = "<span style='color: rgb(70,0,0)'>test1</span>"; 
    $one[2] = "<img title='1' src='file1.jpg' border='0'>";
    $one[3] = "file1.txt"; 
    $two[4] = "<span style='color: rgb(158,0,0)'>test2</span>"; 
    $two[5] = "<img title='2' src='file2.jpg' border='0'>";
    $two[6] = "file2.txt";  
    $one1 = preg_grep('/(?:jpg)|(?:<span)/', $one);
    $one2 = preg_grep('/(?:jpg)|(?:<span)/', $one, PREG_GREP_INVERT); 
    $two1 = preg_grep('/(?:jpg)|(?:<span)/', $two);
    $two2 = preg_grep('/(?:jpg)|(?:<span)/', $two, PREG_GREP_INVERT); 
    if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){ 
      setcookie("array", "one", time()+60*60*24*180); 
    $get1 = $two1[array_rand($two1)];
    $get2 = $two2[array_rand($two2)];
    $get3 = file_get_contents($get2); 
    }else{ 
      setcookie("array", "two", time()+60*60*24*180); 
    $get1 = $one1[array_rand($one1)];
    $get2 = $one2[array_rand($one2)];
    $get3 = file_get_contents($get2); 
    }
    echo $get1; 
    echo $get3;
    ?>
    PHP:
     
    Jen-, Jan 22, 2008 IP