random pictures

  • hxcmusicisswell
  • Newbie
  • Newbie
  • hxcmusicisswell
  • Posts: 12

Post 3+ Months Ago

Whats the code to apply random images to a page?...Like when a person views a page, each time a diff. picture pops up.
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

First off, which type of coding will you be using?
PHP, PHP with MySQL if you want a large amount of different images, or Javascript?
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

For most beginers, I'd go with Javascript, because they work on any hosting solution. If you have a free site like geocities, PHP code isn't going to work.

Code: [ Select ]
<script language="JavaScript">

<!--

var imagenumber = 5 ;

var randomnumber = Math.random() ;

var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;

images = new Array

images[1] = "randomimage1.jpg"

images[2] = "randomimage2.jpg"

images[3] = "randomimage3.jpg"

images[4] = "randomimage4.jpg"

images[5] = "randomimage5.jpg"

var image = images[rand1]

// -->

</script>
  1. <script language="JavaScript">
  2. <!--
  3. var imagenumber = 5 ;
  4. var randomnumber = Math.random() ;
  5. var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;
  6. images = new Array
  7. images[1] = "randomimage1.jpg"
  8. images[2] = "randomimage2.jpg"
  9. images[3] = "randomimage3.jpg"
  10. images[4] = "randomimage4.jpg"
  11. images[5] = "randomimage5.jpg"
  12. var image = images[rand1]
  13. // -->
  14. </script>


That should do it.
  • TsX
  • Graduate
  • Graduate
  • User avatar
  • Posts: 167

Post 3+ Months Ago

I find php much easier for random pictures.

PHP Code: [ Select ]
$value = rand(0,3); //* 3 random numbers from 0-2 (including 0) *//
 
echo '<img src="imagebin/'.$value.'.gif">';
  1. $value = rand(0,3); //* 3 random numbers from 0-2 (including 0) *//
  2.  
  3. echo '<img src="imagebin/'.$value.'.gif">';


I use that ^ for random quote images on my site. works pretty good. Get more images, just increase your random value.
  • lomilmand
  • Student
  • Student
  • lomilmand
  • Posts: 91
  • Loc: Bournemouth, UK

Post 3+ Months Ago

I have this on a site I administer:
Its Javascript, and it is coded like this...

Code: [ Select ]
<SCRIPT LANGUAGE="Javascript"><!--

function image() {
};

image = new image();
number = 0;

// imageArray
image[number++] = "<img src='images/gallery/home_randoms/random1.jpg' border='0'>"
image[number++] = "<img src='images/gallery/home_randoms/random2.jpg' border='0'>"
image[number++] = "<img src='images/gallery/home_randoms/random3.jpg' border='0'>"
image[number++] = "<img src='images/gallery/home_randoms/random4.jpg' border='0'>"
// keep adding items here...

increment = Math.floor(Math.random() * number);

document.write(image[increment]);

//--></SCRIPT>
  1. <SCRIPT LANGUAGE="Javascript"><!--
  2. function image() {
  3. };
  4. image = new image();
  5. number = 0;
  6. // imageArray
  7. image[number++] = "<img src='images/gallery/home_randoms/random1.jpg' border='0'>"
  8. image[number++] = "<img src='images/gallery/home_randoms/random2.jpg' border='0'>"
  9. image[number++] = "<img src='images/gallery/home_randoms/random3.jpg' border='0'>"
  10. image[number++] = "<img src='images/gallery/home_randoms/random4.jpg' border='0'>"
  11. // keep adding items here...
  12. increment = Math.floor(Math.random() * number);
  13. document.write(image[increment]);
  14. //--></SCRIPT>


Obviously you can continue to add more pics by adding further lines. Gotta say its pretty much the same as the other example. Take your pick!
  • criserb
  • Student
  • Student
  • User avatar
  • Posts: 70
  • Loc: Romania

Post 3+ Months Ago

I know the both methods that you described up here, but i have a question about them. Which one works faster ? Anyone knows that ?
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

I would somewhat suggest PHP because it is most likely faster, and people can turn off Javascript on their browsers. You can also try this code for PHP which would be easier so you won't have to edit the script when u add a picture.
PHP Code: [ Select ]
<?
 
$dir = "images";
 
$images = scandir($dir);
 
$num = rand(2, count($images));
 
 
 
echo "<img src=\"".$dir."/".$images[$num]."\">";
 
?>
  1. <?
  2.  
  3. $dir = "images";
  4.  
  5. $images = scandir($dir);
  6.  
  7. $num = rand(2, count($images));
  8.  
  9.  
  10.  
  11. echo "<img src=\"".$dir."/".$images[$num]."\">";
  12.  
  13. ?>

With this you can just throw all your images in the folder images and it'll use all of them. I think that you will have to use PHP 5 to use the scandir() function. If you don't use PHP5 use this instead:
PHP Code: [ Select ]
<?
 
$dir = "images";
 
$od  = opendir($dir);
 
while (false !== ($image = readdir($od))) {
 
   $images[] = $image;
 
}
 
 
 
sort($images);
 
$num = rand(2, count($images));
 
 
 
echo "<img src=\"".$dir."/".$images[$num]."\">";
 
?>
  1. <?
  2.  
  3. $dir = "images";
  4.  
  5. $od  = opendir($dir);
  6.  
  7. while (false !== ($image = readdir($od))) {
  8.  
  9.    $images[] = $image;
  10.  
  11. }
  12.  
  13.  
  14.  
  15. sort($images);
  16.  
  17. $num = rand(2, count($images));
  18.  
  19.  
  20.  
  21. echo "<img src=\"".$dir."/".$images[$num]."\">";
  22.  
  23. ?>

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 133 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.