1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Random script

Discussion in 'PHP' started by Aven, Mar 24, 2006.

  1. #1
    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 :)
     
    Aven, Mar 24, 2006 IP
  2. meikeric

    meikeric Peon

    Messages:
    58
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?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
     
    meikeric, Mar 24, 2006 IP
  3. Aven

    Aven Peon

    Messages:
    335
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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?
     
    Aven, Mar 24, 2006 IP
    meikeric likes this.
  4. meikeric

    meikeric Peon

    Messages:
    58
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    meikeric, Mar 24, 2006 IP
  5. Aven

    Aven Peon

    Messages:
    335
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ah, thanks a whole lot man :)
     
    Aven, Mar 24, 2006 IP
  6. meikeric

    meikeric Peon

    Messages:
    58
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can add rep points for me if you want. But I'm just trying to help the DP community.
     
    meikeric, Mar 24, 2006 IP
    falcondriver likes this.
  7. Aven

    Aven Peon

    Messages:
    335
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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 :p)
     
    Aven, Mar 24, 2006 IP
  8. meikeric

    meikeric Peon

    Messages:
    58
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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
     
    meikeric, Mar 25, 2006 IP
  9. Aven

    Aven Peon

    Messages:
    335
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ahh ok, gave you a rep :)
     
    Aven, Mar 25, 2006 IP
  10. meikeric

    meikeric Peon

    Messages:
    58
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks! I just learned about the iTrade thing. Very interesting.
     
    meikeric, Mar 25, 2006 IP