-
Requirements:
1) Your webhost allows PHP scripting.
2) Your web page(s) have the extension .php
3) You have a directory with all of your photos uploaded
by number ... example: photo_1.jpg, photo_2.jpg, photo_3.jpg
At the beginning of your web page, right after <body>, insert this script:
Code:
<?php
srand(time());
$max=50; // This is the number of photo you have in your directory (eg. numbered 1-50)
$arr = array();
while ( count($arr) < $max ) {
$x = mt_rand(1,$max);
if ( !in_array($x,$arr) ) { $arr[] = $x; }
}
$p1 = "photo_".$arr[0].".jpg";
$p2 = "photo_".$arr[1].".jpg";
$p3 = "photo_".$arr[2].".jpg";
?>
In the script above, you have defined the number of photos you have in
your directory, in this case, there are 50 of them numbered 1-50 (photo_1.jpg, photo_2.jpg)
You will have 3 randomly picked photos ready to be used at any time.
You can display them by using any of the three $p variables ...
Giving the directory path, and then the PHP variable (which is the filename of your photo)...
<img src="./myphotos/<?=$p1?>" alt="my title">
or
<img src="./myphotos/<?=$p2?>" alt="my title">
Those three photos created in the script will all be different and random.
If you need more than 3 photos, add more:
$p4 = "photo_".$arr[3].".jpg";
$p5 = "photo_".$arr[4].".jpg";
$p6 = "photo_".$arr[5].".jpg";
Each time the page is refreshed or displayed, the 3 photos will randomly change.
Here's a working example:
http://www.pierreandclaudia.com
-
nice tut, easy to follow. Cool
-
Though it's probably more than yo uare trying to demonstrate here, I do stuff like that like this:
$d = dir("gallery/$folder");
while (false !== ($entry = $d->read())) {
if (preg_match('/jpg/',$entry)) {
array_push($files, $entry);
}
}
$d->close();
IE get a list of the .jpg files in a folder then open them randomly as you have done above... you dont need to worry about the names then...
If you only wanted a specific number you could jump out of the loop...
:-)
-Hagen
-
Hagen: That is a good idea in theory, probably won't work for many sites as it is very processor intensive, getting every file from a folder. :D
-
Hi, dchesterton intresting... :-)
I guess you appreciate I am not getting the files but a file list....?
It's not something I had thought chould be significant...
Is this really going to be slow compared with connecting to a database for example?
Or with the internet latency itself?
Cheers
-H
-
system architecture
Here is a largish example with 70ish files....
http://www.taichiclub.co.uk/tai-chi-gallery.php
would you not expect the processor to make a single IO request to the "storage device"?
I guess it could be waiting around unable to do anything until the results come back...?
But then servers are multithreaded arn't they so would just run another job in the case of multiple users?
Let me know what you think! I know plenty about processors having worked on the design of a number of them, DSP's and microcontrollers over the years, but very little about system atchitecture... :nervous:
Cheers all
-Hagen
-
This is really a bandwidth issue?
If any PHP (or Perl) scripts are a burden, wouldn't the webhosts
find a way to charge the bandwidth ... that being my limitation to
how much system use I'm allowed?
Unless I'm amazon.com, or ebay, my site is probably not much of
a burden to a server, even if I get 1000 unique visitors per day.
If it was difficult for me to name my .jpegs in sequence, I would think
using Hagen's 'read directory' snippet would be a great way to grab
the list of .jpegs.
But then, that's what makes this forum the best ... an exchange of
ideas that get people thinking (and learning some new things).
This never ceases to amaze me.
-
I'm not completely sure if there is a bandwidth issue. I was just trying to put ideas out there, I would guess it could get quite processor intensive with a lot of requests, to be completely honest i'm not sure :D but I would suspect it would be. Maybe someone knows for sure :confused:
-
well it got me thinking!! :-)
...and if things started to grind to a halt and I had this kind of structure it would probably be one of the first places I could check now...
Cheers -H
P.S. I hope we will be designing sites on the same scale as eBay and friends very soon! :-)
-
You could also store the names of the images in your database and pick one randomly using this mysql statement:
select * from my_image_table order by rand() limit 1