Images Randomly Suffle on Load!? (HTML, PHP, and JS)

Images Randomly Suffle on Load!?

Hey guys!
I’m looking to get this code to work. I have it up on another site and its working but cant get it to work on my new site. Basically just a simple image load that is a link. The way I previously had it was it grabbed the image and image name took bob.jpg and turned the image link into bob.com. I hop you guys can help! Please keep in mind the original was on index.php the new is index.html.

HTML:
[php]Image[/php]

JS:
[php]

<?php function getImagesFromDir($path) { $images = array(); if ( $img_dir = @opendir($path) ) { while ( false !== ($img_file = readdir($img_dir)) ) { // checks for gif, jpg, png if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { $images[] = $img_file; } } closedir($img_dir); } return $images; } function getRandomFromArray($ar) { mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed $num = array_rand($ar); return $ar[$num]; } function getRandomFromArray2($ar2) { mt_srand( (double)microtime() * 100000 ); // php 4.2+ not needed $num2 = array_rand($ar2); return $ar2[$num2]; } ///////////////////////////////////////////////////////////////////// // This is the only portion of the code you may need to change. // Indicate the location of your images $root = $_SERVER['DOCUMENT_ROOT']; // use if specifying path from root //$root = $_SERVER['DOCUMENT_ROOT']; $path = '/ca/images/rand/'; // End of user modified section ///////////////////////////////////////////////////////////////////// // Obtain list of images from directory $imgList = getImagesFromDir($root . $path); $img = getRandomFromArray($imgList); $img2 = getRandomFromArray($imgList); if ($img == $img2) { //echo ' this is the same image'; $img2 = getRandomFromArray($imgList); } $filelink = basename($img, ".jpg"); $filelink2 = basename($img2, ".jpg"); ?> [/php] Countdown.js [php]// Andrew Urquhart : CountDown Timer : http://andrewu.co.uk/clj/countdown/ function CD_T(id,e){var n=new Date();CD_D(+n,id,e);setTimeout("if(typeof CD_T=='function'){CD_T('"+id+"',"+e+")}",1100-n.getMilliseconds())};function CD_D(n,id,e){var ms=e-n;if(ms<=0) ms*=-1;var d=Math.floor(ms/864E5);ms-=d*864E5;var h=Math.floor(ms/36E5);ms-=h*36E5;var m=Math.floor(ms/6E4);ms-=m*6E4;var s=Math.floor(ms/1E3);if(CD_OBJS[id]){CD_OBJS[id].innerHTML=d+" day"+(d==1?" ":"s ")+CD_ZP(h)+"h "+CD_ZP(m)+"m "+CD_ZP(s)+"s"}};function CD_ZP(i){return(i<10?"0"+i:i)};function CD_Init(){var pref="countdown";var objH=1;if(document.getElementById||document.all){for(var i=1;objH;++i){var id=pref+i;objH=document.getElementById?document.getElementById(id):document.all[id];if(objH&&(typeof objH.innerHTML)!='undefined'){var s=objH.innerHTML;var dt=CD_Parse(s);if(!isNaN(dt)){CD_OBJS[id]=objH;CD_T(id,dt.valueOf());if(objH.style){objH.style.visibility="visible"}}else {objH.innerHTML=s+"*"}}}}};function CD_Parse(strDate){var objReDte=/(\d{4})\-(\d{1,2})\-(\d{1,2})\s+(\d{1,2}):(\d{1,2}):(\d{0,2})\s+GMT([+\-])(\d{1,2}):?(\d{1,2})?/;if(strDate.match(objReDte)){var d=new Date(0);d.setUTCFullYear(+RegExp.$1,+RegExp.$2-1,+RegExp.$3);d.setUTCHours(+RegExp.$4,+RegExp.$5,+RegExp.$6);var tzs=(RegExp.$7=="-"?-1:1);var tzh=+RegExp.$8;var tzm=+RegExp.$9;if(tzh){d.setUTCHours(d.getUTCHours()-tzh*tzs)}if(tzm){d.setUTCMinutes(d.getUTCMinutes()-tzm*tzs)};return d}else {return NaN}};var CD_OBJS=new Object();if(window.attachEvent){window.attachEvent('onload',CD_Init)}else if(window.addEventListener){window.addEventListener("load",CD_Init,false)}else {window.onload=CD_Init}; [/php]

PHP has to be a php file in order to excute. So, it having a .html extension will not work regardless.

Okay I have found an alternate way to get the images to load within the page but cant figure out how to make the image a individual link. I can rename the files “ralfs.jpg” then can someone help me with the php to associate that into a link?

New HTML
[php][/php]

New JS in Head
[php][/php]

Maybe change it to something like this?:
[php]Image[/php]

Just ignore what I said, I have no idea what I’m talking about, like you don’t even need php to fix this in the first place; and your JavaScript is 20 years old, document.write isn’t used anymore.

Helpful?

I saw what you read but that was more info rather than helping with a solution. I am just trying to get it to work at this point. I’m using previous codes with new ones I’m finding online. I have got the images to shuffle but haven’t found a solution to making it a link as well. I took what you said and tried to go another route.

Any help on fixing this problem?

Is there anyway to make this a link via the image name?
I’m hoping I can change ‘<img src="’; to something like this ‘<img src="’; " alt=“Test”/>’;

[php] [/php]

[php]<!doctype html>

<p id='tag'></p>


<script type="text/javascript">
var imageURLs = [
     "http://www.#.com/images/ads/hill.jpg"
   , "http://www.#.com/images/ads/wilshire.jpg"
   , "http://www.#.com/images/ads/ger.jpg"
   , "http://www.#.com/images/ads/lan.jpg"
];
function getImageTag3() {
  var img = '<img src=\"';
 var randomIndex = Math.floor(Math.random() * imageURLs.length);
 img += imageURLs[randomIndex];
 img += '\" alt=\"Test\"/>';
 document.getElementById("tag").innerHTML = img;

}

[/php]

I tried using that code but I don’t get a link still. Also when I try to add the code to another section on the page it only will show one banner out of the 3. I tried changing the

to and also in the JS but that gives me the same problem.

Do you know how to use your developer tools?

[php]<!doctype html>

<p id='tag'></p>


<script type="text/javascript">
var imageURLs = [
     "http://www.#.com/images/ads/hill.jpg"
   , "http://www.#.com/images/ads/wilshire.jpg"
   , "http://www.#.com/images/ads/ger.jpg"
   , "http://www.#.com/images/ads/lan.jpg"
];
function getImageTag3() {
  var img = '<img src=\"';
 var randomIndex = Math.ceil(Math.random() * (imageURLs.length - 1));
 img += imageURLs[randomIndex];

 for ( var i = 0; i < 50; i++){
        console.log(imageURLs[Math.ceil(Math.random() * (imageURLs.length - 1))]);
 }
 img += '\" alt=\"' + imageURLs[randomIndex] + '\"/><a href="' + imageURLs[randomIndex] + '">' + imageURLs[randomIndex] + '</a>';
 document.getElementById("tag").innerHTML = img;

}

[/php]

This sort of gives you what you want, it could be a lot cleaner, but this is your code. If you check the console, you will see 50 iterations of the random call. This shows that you are not getting the same image every time.

I don’t think Ive ever used them before. I tried that code and its getting close. I have the images rotating and it adds a link below the banner but the link is http://www.#.com/images/ads/ad.jpg not http://www.ad.com. Also I’m trying to make the image a link not a text link below. Thanks again for all your help!

I am almost there!
I have the corrct links associated to the image I just need the to be linked to the image not a text link.
Heres what I have so far:

[php][/php]

Ive tried moving the tags around but I get an “Undefined” link.

Also I’m having a problem with adding multiple banners. For testing I put the

in 3 spots on the page, only the first tag shows rotating images the others are no where to be found. Can I duplicate the script and change tag to tag2? Or and an extra ? Maybe ?

Cleaned it up a bit, but only because, well, it needed it.

[php]<!doctype html>

<p id='tag'></p>


<script>
function getAd() {
    var ads = {};
	
	ads[0] = { image: "http://lanierlawfirm.com/wp-content/uploads/2016/05/logo_top2.png",
		       url: "http://lanierlawfirm.com/",
		       };

	ads[1] = { image: "http://www.#.com/images/ads/ger.jpg",
		             url: "http://www.geracilawfirm.com/",
		             };

	ads[2] = { image: "http://www.#.com/images/ads/hill.jpg",
             url: "http://www.hill-law-offices.com/",
             };

	ads[3] = { image: "http://www.#.com/images/ads/wilshire.jpg",
             url: "http://wilshirelawfirm.com/",
             };

 var randomIndex = Math.ceil(Math.random() * (Object.keys(ads).length - 1));
 link = '<a href="' + ads[randomIndex].url + '"><img src="' + ads[randomIndex].image + '" alt="' + ads[randomIndex].url + '"></a>';
 document.getElementById("tag").innerHTML = link;

}

[/php]

And you don’t need to replicate the code to use it more than once, but it is a problem if you mind duplicates.

Your awesome! Working great now! Thank you so much!

Sponsor our Newsletter | Privacy Policy | Terms of Service