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.

Randomly selected line of script?

  • 23-11-2009 05:17AM
    #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!

    MODS: I accidentally placed this in the design forum first, delete the other one if you like!


Comments

  • Moderators, Politics Moderators, Paid Member Posts: 44,042 Mod ✭✭✭✭Seth Brundle


    use a case option along with a random number (between 1 and 10)
    case 1
    img#1
    link#1
    case 2
    img #2
    link#2
    etc.


  • Closed Accounts Posts: 54 ✭✭quotaj


    After a bit of researching on PHP rand thanks to a suggestion over on the design forum, 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.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    quotaj wrote: »
    After a bit of researching on PHP rand thanks to a suggestion over on the design forum, 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.
    Better again if all images are in the same directory you can get an array of the directory files back and select a random one of these.

    This way it eliminates the need for hard coding images into your script. Simply copy a new image into directory and script will handle it. (Not tested).

    Something like

    [php]
    srand ((double) microtime() * 1000000);
    $dirHandle =opendir('/path');
    $fileNames = array();

    while (false !== ($file = readdir($$dirHandle)))
    array_push($fileNames, $file);

    $randomFileName = rand(0,count($fileNames)-1);

    $html = '<a href="image.php?showLarge='.$randomFileName.'" target="_blank"><img src="'.$randomFileName.'" border="0">';

    echo $html;
    [/php]


  • Registered Users, Registered Users 2 Posts: 6,652 ✭✭✭daymobrew


    Webmonkey wrote: »
    Better again if all images are in the same directory you can get an array of the directory files back and select a random one of these.

    This way it eliminates the need for hard coding images into your script. Simply copy a new image into directory and script will handle it. (Not tested).
    The fact that each image links to a different page could be a problem, unless there is a correlation between the image file name and the destination page e.g. sales.jpg goes to sales.html.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    daymobrew wrote: »
    The fact that each image links to a different page could be a problem, unless there is a correlation between the image file name and the destination page e.g. sales.jpg goes to sales.html.
    Yeah I noticed that after posting. If this is the case, then a hard code mapping would be required as before unless as you say, the destination is somewhat correlated or encoded by the filename.

    But then another option may be to create a mapping file in the directory that you can edit and load this in for a mapping.


  • Advertisement
Advertisement