11Tami Posted January 16, 2008 Share Posted January 16, 2008 Hello, this pulls an image with a title on every next page load. Problem is when it pulls image one I need it to also pull "title 1" and when it pulls image 3 I need it to pull "title 3" right now its not doing that. Right now its sticking whatever title it wants in with the images. How can I get to match the arrays while its also pulling something randomly? Please let me know, thank you very much. <?php $title1[1] = "title1"; $title1[2] = "title2"; $title2[3] = "title3"; $title2[3] = "title3"; $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); $get2 = $title2[array_rand($title2)]; $get3 = $img2[array_rand($image2)]; }else{ setcookie("array", "two", time()+60*60*24*180); $get2 = $title1[array_rand($title1)]; $get3 = $img1[array_rand($image1)]; } echo "<img title='$get2' src='$get3' border='0'>"; ?> Link to comment https://forums.phpfreaks.com/topic/86363-matching-arrays/ Share on other sites More sharing options...
Ken2k7 Posted January 16, 2008 Share Posted January 16, 2008 The ordering of your array doesn't make sense here. $title2[3] = "title3"; $title2[3] = "title3"; Those 2 lines are the same thing. And what happened to $title2[1] and $title2[2]? Edit: Same here: $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"; Link to comment https://forums.phpfreaks.com/topic/86363-matching-arrays/#findComment-441292 Share on other sites More sharing options...
Daukan Posted January 16, 2008 Share Posted January 16, 2008 Change this <?php if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){ setcookie("array", "one", time()+60*60*24*180); $get2 = $title2[array_rand($title2)]; $get3 = $img2[array_rand($image2)]; }else{ setcookie("array", "two", time()+60*60*24*180); $get2 = $title1[array_rand($title1)]; $get3 = $img1[array_rand($image1)]; } ?> To this <?php if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){ setcookie("array", "one", time()+60*60*24*180); $image = rand(1,count($image2) ); $get2 = $title2[$image]; $get3 = $img2[$image]; }else{ setcookie("array", "two", time()+60*60*24*180); $image = rand(1,count($image2) ) $get2 = $title2[$image]; $get3 = $img2[$image]; } ?> Link to comment https://forums.phpfreaks.com/topic/86363-matching-arrays/#findComment-441296 Share on other sites More sharing options...
toplay Posted January 16, 2008 Share Posted January 16, 2008 Just generate a random number instead of using array_rand() or assign the return of array_rand() so you know what it is and can use it on the title. Example - do this if array starts with zero: $nbr = mt_rand(0, count($title1) - 1); or this if starts with 1: $nbr = mt_rand(1, count($title1)); or this: $nbr = array_rand($title1); Then you can use it in this way: $get2 = $title1[$nbr]; $get3 = $img1[$nbr]; I assume there's always going to be the same number of titles and images. Link to comment https://forums.phpfreaks.com/topic/86363-matching-arrays/#findComment-441298 Share on other sites More sharing options...
11Tami Posted January 17, 2008 Author Share Posted January 17, 2008 Sorry for the numbers. Ok thanks a lot, its almost there now. Its showing whats in array title1 and img1 and matching them now but its not showing the rest. I have this. <? 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'>"; ?> Link to comment https://forums.phpfreaks.com/topic/86363-matching-arrays/#findComment-441490 Share on other sites More sharing options...
11Tami Posted January 17, 2008 Author Share Posted January 17, 2008 Sorry about that everything froze, I couldn't finish my post. I have what I just listed above and I can't get it to show whats in $title2 and $img2 arrays. Everything I try doesn't work. Please let me know how to fix this. Thank you very much. Link to comment https://forums.phpfreaks.com/topic/86363-matching-arrays/#findComment-441496 Share on other sites More sharing options...
11Tami Posted January 17, 2008 Author Share Posted January 17, 2008 For instance I tried adding this at the bottom as well like those right above it. Doesn't work. $get1 = $title2[$nbr2]; $get2 = $img2[$nbr2]; Link to comment https://forums.phpfreaks.com/topic/86363-matching-arrays/#findComment-441503 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.