Rotating images in PHP or JS

Discussion in 'Programming' started by lopes, Oct 1, 2009.

  1. #1
    Hello, I need to create a function that makes 3 images change the position randomly every time a page is refreshed. The picture explains it better.
     

    Attached Files:

    lopes, Oct 1, 2009 IP
  2. frantech

    frantech Greenhorn

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    20
    #2
    Hey there,

    You could try something like this I guess:

    
    <?php
    
    $images = array("image1.jpg", "image2.jpg", "image3.jpg");
    
    shuffle($images);
    
    foreach($images as $i) {
         echo '<img src="' . $i . '">';
    }
    
    ?>
    
    Code (markup):
    Hope that helps :)

    Francisco
     
    frantech, Oct 2, 2009 IP
  3. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #3
    Francisco's code should work just fine, but here is the valid XHTML version, just incase you worried about that stuff.

    
    <?php
    
    $images = array("image1.jpg", "image2.jpg", "image3.jpg");
    
    shuffle($images);
    
    foreach($images as $i) {
         echo '<img src="' . $i . '" alt="" />';
    }
    
    ?>
    
    Code (markup):
     
    crath, Oct 3, 2009 IP
  4. lopes

    lopes Well-Known Member

    Messages:
    230
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Thanks for the replies! Btw I wrote a big function in PHP to do that before seeing your tips, but at least now I know the easy way :)
     
    lopes, Oct 3, 2009 IP
  5. Nei

    Nei Well-Known Member

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    170
    #5
    Lopes, would you like to show your function please. It's interesting :)
     
    Nei, Oct 4, 2009 IP
  6. lopes

    lopes Well-Known Member

    Messages:
    230
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    120
    #6
    My function is very bad. I don't have much programming skills. :D
     
    lopes, Oct 6, 2009 IP
  7. wallstreet2009

    wallstreet2009 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks for the info was very useful :)
     
    wallstreet2009, Oct 7, 2009 IP