11Tami Posted September 17, 2007 Share Posted September 17, 2007 Hello, how would I get this to choose a line of text randomly instead of in order so it doesn't pick the same one twice in a row? Please let me know, thanks very much. <?php $maximum=3; $a = (isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum)? intval($_COOKIE['e'])+1 : 1; setcookie("e", $a, time()+60*60*24*180); $text[1] = "test1"; $text[2] = "test2"; $text[3] = "test3"; php echo "$text[$a]"; ?> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 17, 2007 Share Posted September 17, 2007 <?php echo rand($text[$a]); ?> Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 17, 2007 Author Share Posted September 17, 2007 Thank you, it doesn't like those there, think its looking for numbers. Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 17, 2007 Author Share Posted September 17, 2007 Maybe there's a way to get it to output a number from the array first like 1, 2, or 3 and then put that into the random function? Anyone know how to get it to output an array number? Or maybe there is an easier way. I was missing the whole code, here's all of it. <?php $maximum=3; $a = (isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum)? intval($_COOKIE['e'])+1 : 1; setcookie("e", $a, time()+60*60*24*180); $text = Array(); $text[1] = "test1"; $text[2] = "test2"; $text[3] = "test3"; php echo "$text[$a]"; ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted September 17, 2007 Share Posted September 17, 2007 Well you could generate a random number between 0 and the number of keys -1. Example: $array = array('Corbin', 'Is', 'Awesome'); $random = $array[rand(0, count($array)) - 1]; //the count isn't necesary if you know the number of keys.... Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 17, 2007 Author Share Posted September 17, 2007 Thanks, I looked up keys but don't know what you mean by number of keys. I need to keep the numbers in the array because I'll be rotating other numbers as well. Maybe it can be done without the cookies as well, since its random. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 17, 2007 Share Posted September 17, 2007 uh.. by looking at your php, shouldnt this be under php math? "intval" Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 17, 2007 Author Share Posted September 17, 2007 I've got this: <?php $text = Array(); $text[1] = "test1"; $text[2] = "test2"; $text[3] = "test3"; $get = $text[rand(1, 3)]; echo $get; ?> It's working, now I just need to keep the same one from occurring twice in a row. Not sure how. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 17, 2007 Share Posted September 17, 2007 should you do rand (1,3); and then say if it equals 1 then text[1] ?? idk what you want lol/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.