Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Random line of script?

Options
  • 23-11-2009 5: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,145 ✭✭✭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 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