PDA

View Full Version : Random Code?



blueguy
8th April 2006, 4:21 AM
Hi,

I was wondering if anybody knows if there is a way to randomize coding... For example, is there a code that will allow me to enter multiple images and have one randomly appear upon each refresh?

Virtual Headache
8th April 2006, 4:38 AM
There is a random function in PHP, take a look: http://at.php.net/manual/en/function.rand.php

You could use if statements depending on the random number.
I didn't try this out though and there might be a better way to do that (I'm not a PHP expert^^;).

Dragonfree
8th April 2006, 4:43 AM
Well, since you're on Freewebs, you'd have to use Javascript for it...

Just do something like this where you want the image to appear (just whipped up, so it may not work perfectly):


<script type="text/javascript">
var images = new Array();

images[0] = "image1.gif";
images[1] = "image2.gif";
images[2] = "image3.gif";

// Just go on like that, always increasing the number in the []s after images

var random = Math.floor(Math.random() * (images.length - 1) + 0.5);

document.write('<img src="' + images[random] + '" alt="">');
</script>

The only part you should have to edit is the list of images...

EDIT: Tested it, and yup, it's working fine. Hope I helped.

blueguy
8th April 2006, 4:51 AM
Well, since you're on Freewebs, you'd have to use Javascript for it...

Just do something like this where you want the image to appear (just whipped up, so it may not work perfectly):


<script type="text/javascript">
var images = new Array();

images[0] = "image1.gif";
images[1] = "image2.gif";
images[2] = "image3.gif";

// Just go on like that, always increasing the number in the []s after images

var random = Math.floor(Math.random() * (images.length - 1) + 0.5);

document.write('<img src="' + images[random] + '" alt="">');
</script>

The only part you should have to edit is the list of images...

EDIT: Tested it, and yup, it's working fine. Hope I helped.

I'll go and try this out! Thank you very much! ^_^ This was just the type of thing I was looking for. There was the same sort of code for a forum hack, but I didn't know where to find one that would work on a website.