Need a random rotating script

Discussion in 'HTML & Website Design' started by Seannal, Aug 16, 2006.

  1. #1
    Hey there,

    I'm looking for a script that will randomly select between different frames for my website.

    For example, my homepage has a "Featured e-book", "featured DVD", "featured website" etc... and I would like to create a bunch of different featured items and have a script that will display them randomly.

    Any ideas?

    Thanks,

    Sean
     
    Seannal, Aug 16, 2006 IP
  2. ablaye

    ablaye Well-Known Member

    Messages:
    4,024
    Likes Received:
    97
    Best Answers:
    0
    Trophy Points:
    150
    #2
    You can easily code this if you know a little bit of PHP/MySQL. This job would not take more than 1 hour for a php programmer.
    I am not aware of any script that would allow you to do so.
     
    ablaye, Aug 16, 2006 IP
  3. honey

    honey Prominent Member

    Messages:
    15,555
    Likes Received:
    712
    Best Answers:
    0
    Trophy Points:
    325
    #3
    I think I can do it or get it done for you. Should be easy.
     
    honey, Aug 16, 2006 IP
  4. brian394

    brian394 Well-Known Member

    Messages:
    226
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #4
    It's unclear from what you wrote, but you could either be talking about IFRAME's or FRAMESET's (two different things). Also, you didn't mention whether you are looking for a Javascript (client-side) solution or a PHP (server-side) solution. I'll go over each one just in case...

    In the case of IFRAME's, let's say I have 5 IFRAME's that I want to rotate (that is, 5 individual HTML files). In PHP I could do this...

    
    <?php
     $iframe_urls[] = "iframe1.html";
     $iframe_urls[] = "iframe2.html";
     $iframe_urls[] = "iframe3.html";
     $iframe_urls[] = "iframe4.html";
     $iframe_urls[] = "iframe5.html";
    
     $random_number = rand(0, count($iframe_urls) - 1);
    
    ?>
    
    <iframe frameborder="0" width="200" height="200" src="<?php echo $iframe_urls[$random_number]; ?>">Your browser doesn't support iframes.</iframe>
    
    
    Code (markup):
    Pretty simple, right? Obviously you would need to change the width and height of your iframe to suit your needs.

    You could also do via Javascript like this...

    
    <script type="text/javascript">
     var iframe_urls = new Array("iframe1.html", "iframe2.html", "iframe3.html", "iframe4.html", "iframe5.html");
     var random_number = parseInt(Math.random() * iframe_urls.length);
    
     document.write('<iframe frameborder="0" width="200" height="200" src="' + iframe_urls[random_number] + '">Your browser doesn\'t support iframes.</iframe>');
    </script>
    
    Code (markup):
    However, I should point out in both cases that since search engines don't read javascript (or the contents of iframes), your "included" frames won't be indexed.

    So that's IFRAME's. But what about with FRAMESET's?

    Well, with FRAMESET's, it's the same deal only the HTML changes a little bit.

    PHP
    
    <?php
     $iframe_urls[] = "iframe1.html";
     $iframe_urls[] = "iframe2.html";
     $iframe_urls[] = "iframe3.html";
     $iframe_urls[] = "iframe4.html";
     $iframe_urls[] = "iframe5.html";
     $random_number = rand(0, count($iframe_urls) - 1);
    ?>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
       "http://www.w3.org/TR/html4/frameset.dtd">
    <html>
    <head>
    <title>example title</title>
    </head>
    <frameset>
     <frame src="<?php echo $iframe_urls[$random_number]; ?>" marginwidth="0" marginheight="0" frameborder="0">
    </frameset>
    </html>
    
    Code (markup):
    Javascript
    
    <html>
    <head>
    <title>example title</title>
    <script type="text/javascript">
    <!--
     var iframe_urls = new Array("iframe1.html", "iframe2.html", "iframe3.html", "iframe4.html", "iframe5.html");
     var random_number = parseInt(Math.random() * iframe_urls.length);
    // -->
    </script>
    </head>
    <script type="text/javascript">
    <!--
    document.write('<frameset>');
    document.write('<frame src="' + iframe_urls[random_number] + '" marginwidth="0" marginheight="0" frameborder="0">');
    document.write('</frameset>');
    // -->
    </script>
    </html>
    
    
    Code (markup):
     
    brian394, Aug 17, 2006 IP
  5. Seannal

    Seannal Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Brian!

    Highly appreciated.

    I'll give these a try.
     
    Seannal, Aug 17, 2006 IP
  6. Seannal

    Seannal Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'm using the javascript Iframe... but for some reason I can only use it once on any given page. If I try to use the script in multiple places it will only display one of them.

    Any ideas?
     
    Seannal, Aug 17, 2006 IP
  7. brian394

    brian394 Well-Known Member

    Messages:
    226
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #7
    You could try re-generating the random number before displaying each iframe like this...

    
    <script type="text/javascript">
     var iframe_urls = new Array("iframe1.html", "iframe2.html", "iframe3.html", "iframe4.html", "iframe5.html");
    </script>
    
    <!-- This is the first IFRAME on the page -->
    <script type="text/javascript">
     var random_number = parseInt(Math.random() * iframe_urls.length);
     document.write('<iframe frameborder="0" width="200" height="200" src="' + iframe_urls[random_number] + '">Your browser doesn\'t support iframes.</iframe>');
     var last_index = random_number;
    </script>
    
    <!-- This is the second/third/fourth IFRAME on the page -->
    <script type="text/javascript">
     random_number = parseInt(Math.random() * iframe_urls.length);
     if (random_number == last_index) { random_number = last_index + 1; }
     if (random_number == iframe_urls.length) { random_number = 0; }
     document.write('<iframe frameborder="0" width="200" height="200" src="' + iframe_urls[random_number] + '">Your browser doesn\'t support iframes.</iframe>');
     last_index = random_number;
    </script>
    
    Code (markup):
     
    brian394, Aug 17, 2006 IP
  8. slickricky

    slickricky Active Member

    Messages:
    240
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Here make this easy on yourself and use a lot less code. Just a touch of php...

    http://yoursite/random/frame<?php printf(mt_rand(1,11))?>.html

    will return a random number where the php is including and between 1 - 11

    Just change the second number to the number of files you have, and name your files frame1.html, frame2.html, frame3.html and so on.
     
    slickricky, Aug 17, 2006 IP
  9. EcJughead

    EcJughead Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I am new and am still learning Javascript. I used the above iframe javascript brian posted and now I'm curious how to add two more features. One being a wipe affect (or other scriptaculous affects) and a user interface button outside the iframe to change the iframe's .html file. Thoughts? Thanks!
     
    EcJughead, Aug 26, 2006 IP
  10. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    slickricky you still around? I want to try some of these including yours.
    http://yoursite/random/frame<?php printf(mt_rand(1,11))?>.html
    For instance would if I have two files named
    http://mysite/frame1.html and
    http://mysite/frame2.html
    how does that work with your just above example you gave.
    Please let me know, I really need to implement this thanks, Jen
     
    Jen-, Aug 29, 2006 IP
  11. slickricky

    slickricky Active Member

    Messages:
    240
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #11
    Yup, you've got it exactly correct. For your example the php would be...
    <?php printf(mt_rand(1,2))?>

    (I just updated it to say 1,2 instead of 1,11, because you only have two files.) You just need to update so that the second number matches the largest number of your random files.
     
    slickricky, Aug 30, 2006 IP