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

Randomly selected line of script?

Options
  • 23-11-2009 5: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 Posts: 39,851 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 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 Posts: 6,509 ✭✭✭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 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