954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

displaying random images

I have this code which display random images from any folder on a webserver. You just have to give each picture the same name but with a number at the end of it (make foo.bar foo1.bar footype.bar foo2.bar).

Does anybody have any idea how to make this work accept not have any numbers or have the same name on the pictues. I just want it to display any picture in that folder in the webserver regardless of the name. Here is the code.

<?php
function image()
{
	$min = 1; //The first number one of your images has
	$max = 3; //The highest number you image has
	$number = rand($min, $max);
	$url = "http://website.com/~ub/_ubr/wp-content/themes/ubcmi/images/icons/image" . $number . ".jpg"; //change the url to your url //for the image split the url into two parts by separating in between the image name and //extension
	echo "<img src='$url'>";
}
 ?>
alexgv14
Light Poster
49 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

ok, I suggest doing something like this:

<?php
function image()
{
	$min = 1; //The first number one of your images has
	$max = 3; //The highest number you image has
	$number = rand($min, $max);
	$url = "http://website.com/~ub/_ubr/wp-content/themes/ubcmi/images/icons/" . $number . ".jpg"; //change the url to your url //for the image split the url into two parts by separating in between the image name and //extension
	echo "<img src='$url'>";
}
 ?>


This means that all is needed is just a number for your images rather then having to name them all.

jakx12
Junior Poster
125 posts since Jan 2009
Reputation Points: 10
Solved Threads: 5
 

what if the images don't have a number?

alexgv14
Light Poster
49 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

well the whole idea of the script that you have is that it generates the random image through a random number generator in php based on the numbers used to name your images. Therefore using your script you must have numbered images. This is pretty much the easiest way in generating your random images.
If you do not understand the script you are using i suggest going to www.tizag.com and reading up on some php.

jakx12
Junior Poster
125 posts since Jan 2009
Reputation Points: 10
Solved Threads: 5
 

You could also just store the image names in an array and refer to the array with the number. An example is the following.

<?php
function image()
    {
    $min = 1; //The first number one of your images has
    $max = 3; //The highest number you image has
    $num = rand($min, $max);
    //set picture names
    $pic[1]='firstpic.jpg';
    $pic[2]='secondpic.jpg';
    $pic[3]='another_name.jpg';
    //end of setting picture names
  $url = "http://website.com/~ub/_ubr/wp-content/themes/ubcmi/images/icons/" .$pic[$num]."; //change the url to your url //for the image split the url into two parts by separating in between the image name and //extension
	echo "<img src='$url'>";
}
 ?>

Very basic and hope it does the job.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

arrays is a good idea, but then requires alot of manuel work, of constantly updating the code when there is a new image. Unless you do it atuomaticaly but your script does not support that.

jakx12
Junior Poster
125 posts since Jan 2009
Reputation Points: 10
Solved Threads: 5
 
arrays is a good idea, but then requires alot of manuel work, of constantly updating the code when there is a new image. Unless you do it atuomaticaly but your script does not support that.

What I posted was just what the user wanted. However, there are 2 alternative options. There is the option of php automatically detecting the pictures in the folder and assign them to an array (requires a bit of cpu) or another option is to fetch the arrays from a mysql database.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Thank you for you input cwarn23. That solution did work for me. And Im doing something in Javascript which is very similar excepts it fades through images, and I think I am going to use the same concept. Thanks again.

alexgv14
Light Poster
49 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You