How to change a home page picture on each refresh / reload

phoenix2010

Member
Messages
315
Reaction score
0
Points
16
ok guys !
im after picking your brains for an easy way to create a picture box that displays say a folder full of 20 pics 1 pic at a time and chages the picture in the box on every page refresh. !
ive seen some different forms of code to impliment such boxes but after trying a few times to get it to work ive given up temporaraly and thought i would ask here as you guys rock when it comes to helping with stuff like this !
again thanks for your time everyone and look forward to your hints and tips
 
Last edited:

farscapeone

Community Advocate
Community Support
Messages
1,165
Reaction score
27
Points
48
I know an easy way to do that with PHP.
PHP:
//path to the image directory
$directory = "images/";
 
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");

// get random image index
$rand_img = array_rand($images, 1);

// display the image
echo '<img src="'.$images[$rand_img].'" alt="" />';
 

phoenix2010

Member
Messages
315
Reaction score
0
Points
16
firstly friend thanks for the response as im wasnt sure really how to get this kind of thing to work

secondly i can see the php code uve posted up but how would i use it ?

is the txt put into the page with the display as html?

do i have to have the certain jpg,s i wanna use in 1 folder?

sorry for being a pain its just im not used php that im aware of anyway ?
 

farscapeone

Community Advocate
Community Support
Messages
1,165
Reaction score
27
Points
48
OK I'll try to explain it as simple as I can and if you want more you'll have to learn PHP. I will only help you make this particular peace of code work.

PHP code is written inside of HTML. The only thing you'll have to do is rename the extension of your ".html" (or ".htm") file to ".php".

PHP scripts have to be enclosed in "<?php ... ?>" tags where "..." is the actual PHP code. The above code should look like this:
PHP:
<?php
//path to the image directory 
$directory = "http://x10hosting.com/forums/images/"; 
  
//get all image files with a .jpg extension. 
$images = glob("" . $directory . "*.jpg"); 

// get random image index 
$rand_img = array_rand($images, 1); 

// display the image 
echo '<img src="'.$images[$rand_img].'" alt="" />'; 
?>

PHP code inside the HTML file will be executed on the server before it returns the result to the browser. This means that your browser will only see the HTML.

The lats line in the script above (before "?>" closing tag) begins with the "echo" statement. This is the PHP output function (similar to print in other languages) that will inject any text into the HTML.

Down to the script.
"$directory" variable contains the path to the directory where you have your pics. This can be any folder on your site.

$images is an array that contains filenames of all the pics in the specified folder that have ".jpg" extension. If you would like to have more then one extension (like .gif or .png) you just have to add them to the $images variable like so:

PHP:
$images = glob("" . $directory . "*.jpg") + glob("" . $directory . "*.gif");

$rand_img gets the random index form the array of pics.

Finaly we have to inject a random image into our page. Wherever you put your "echo" function in your HTML that is where the the result will be injected. To understand it better take a look at the PHP "Halo World!" example below.

File: halo_world.php
PHP:
<html>
<head>
  <title>PHP Halo World</title>
</head>

<body>
  <h1><?php echo "Helo World!"; ?></h1>
</body>

</html>

As you can see the "echo" function will inject the "Halo World!" text into the HTML wherever you put it. The result page (the one browser gets) would look like this:

PHP:
<html>
<head>
  <title>PHP Halo World</title>
</head>

<body>
  <h1>Helo World!</h1>
</body>

</html>

In my script I echo-ed the "img" tag with "src" value containing the random image. You can put the code wherever you want your image to be displayed in your HTML page.

I hope this was clear enough and that you will be able to use it. Just remember to rename your HTML file into PHP.
 
Last edited:

phoenix2010

Member
Messages
315
Reaction score
0
Points
16
thank you so much for taking the time out and will have a play with this stuff later in the week
 
Last edited:
Top