Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Random line of script?

  • 23-11-2009 04:15AM
    #1
    Closed Accounts Posts: 54 ✭✭


    Hey,

    I want to place an random image on my website. There is a pool of 10 images that it can choose from. Each image will be linked to a different page.

    Is there a way of randomly picking a bit of few lines of code each time? Have the image and hyperlink html part of the code randomly selected from a choice of ten...

    Thanks for any help!


Comments

  • Closed Accounts Posts: 7,144 ✭✭✭DonkeyStyle \o/


    Here's a way to do it in javascript, it's probably better to do it in a server-side language, but you didn't mention what you use.

    [HTML]<html>
    <head>

    <script type="text/javascript">
    function randomlink()
    {
    var images = new Array ('image1.jpg',
    'image2.jpg',
    'image3.jpg',
    'image4.jpg',
    'image5.jpg',
    'image6.jpg',
    'image7.jpg',
    'image8.jpg',
    'image9.jpg',
    'image10.jpg'
    );

    var urls = new Array ( 'link1.html',
    'link2.html',
    'link3.html',
    'link4.html',
    'link5.html',
    'link6.html',
    'link7.html',
    'link8.html',
    'link9.html',
    'link10.html'
    );

    var x = Math.floor(Math.random()*(images.length));
    document.write("<a href=\""+urls[x]+"\"><img src=\""+images[x]+"\" alt=\"\"></a>");
    }
    </script>

    </head>

    <body>
    <script>randomlink();</script>

    </body>
    </html>[/HTML]
    Just move <script>randomlink();</script> to where ever you want the image to appear.


  • Closed Accounts Posts: 54 ✭✭quotaj


    Thanks a lot... I must give that a try.

    I use PHP, is there a better way to do it in PHP?


  • Closed Accounts Posts: 270 ✭✭CoNfOuNd


    You could do it in PHP using the rand() function.

    Lots of examples on that page.


  • Registered Users, Registered Users 2 Posts: 569 ✭✭✭none


    I'm using almost the same JavaScript as DonkeyStyle \o/, just multidimensional array instead of several simple ones. Also, I'd add description (alt/title).


  • Closed Accounts Posts: 54 ✭✭quotaj


    After a bit of researching on PHP rand thanks to the suggestion above, this is what I've ended up with and it's perfect thanks!

    [php]<?php

    $quotes[] = '<a href="http://www.LINK1.com/&quot; target="_blank"><img src="image1.gif" border="0">';

    $quotes[] = '<a href="http://www.LINK2.com/&quot; target="_blank"><img src="image2.gif" border="0">';

    $quotes[] = '<a href="http://www.LINK3.com/&quot; target="_blank"><img src="image3.gif" border="0">';


    srand ((double) microtime() * 1000000);

    $randomimage = rand(0,count($quotes)-1);


    echo "" . $quotes[$randomimage] . "";



    ?>[/php]

    And then include this where I want to place the random linked image on the webpage...
    [PHP]<?php include "random_image.php"; ?>[/PHP]

    I basically altered a "random quote" script a bit. I must tidy up the "" at the end and add the title and alt tags now.


  • Advertisement
Advertisement