Hello, Does anyone know a PHP script that displays things randomly? For example, I want a group of games to be displayed randomly and that will change on refresh. I'm sure it's a pretty short script... could anyone give me an example? Thank you
<?php // This is an array of games $games = array("Game 1", "Game 2", "Another Game", "Yet Another"); //This is an integer value of the number of elements in $game $count_games = count($games)-1; // This is the psuedo random element number of the $games array $random_game = rand(0,$count_games); // This echos (prints) the psuedo random game echo $games[$random_game]; ?> Code (markup): Hope that helps. I say psuedo random, because a computer cannot generate a truely random number as that number needs to be based off of something, and it's usually microtime, which isn't random as time is relatively constant (don't get me onto why I say relatively). Suffice to say, for your purpose, it's random enough. If you were attempting to test atomic formation and destruction, it wouldn't be random enough, AND you wouldn't be using PHP. Cheers, Mike
Thank you!! works great... However, sometimes, when you refresh, it does not display anything... any idea how that can be fixed so that it always shows something?
Sorry about that, check the count line. I forgot to include the -1, because count counts the number of elements and starts at 1. I fixed it in the above code. Where as an array index starts at zero. ie, array[0] = 1; Row 1 array[1] = 2; Row 2 array[2] = 3; Row 3. Sorry about that, beer is bad... But OH so good. Mike
Ahh, sorry... didn't know there was such system are you talking about the iTrade? because it keeps saying everytime I post this topic's URL. (new to DP )
Not sure what iTrade is... but rep points are reputation points. If you click on the scales of justice between #6 and Rate on the right of my above thread, it will ask you to Vote for reputation. That's how you do it. Mike