•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,488 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,718 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 653 | Replies: 5
![]() |
•
•
Join Date: Mar 2006
Location: UK
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Hi
I know it's possible to have random backgrounds with php (day of week), but is it possible to have random images in a photo gallery?
I've written the html below:
<div class="thumb">
<a href="image01.html"><img src="images/image01_thumb.gif" alt="Image01" />
<span><img src="images/pre_image01.gif" alt="Image01" /><em>Image01</em></span></a>
</div>
Is there any php that will change the image randomly?
Thanks in advance
Tankedup
I know it's possible to have random backgrounds with php (day of week), but is it possible to have random images in a photo gallery?
I've written the html below:
<div class="thumb">
<a href="image01.html"><img src="images/image01_thumb.gif" alt="Image01" />
<span><img src="images/pre_image01.gif" alt="Image01" /><em>Image01</em></span></a>
</div>
Is there any php that will change the image randomly?
Thanks in advance
Tankedup
•
•
Join Date: Apr 2005
Location: New York state
Posts: 487
Reputation:
Rep Power: 5
Solved Threads: 73
The simplest would be something like this
Then wherever you wanted the image to display you would just do
php Syntax (Toggle Plain Text)
<?php function randomImage($numImages = 10) { srand( time(NULL)); $rand = rand()%$numImages; $ranImage = '<div class="thumb"> <a href="image'.$rand.'.html"> <img src="images/image'.$rand.'_thumb.gif" alt="Image'.$rand.'" /> <span><img src="images/pre_image01.gif" alt="Image01" /> <img src="images/pre_image'.$rand.'.gif" alt="Image'.$rand.'" /> <em>Image'.$rand.'</em> </span> </a> </div> '; $echo $ranImage; } ?>
Then wherever you wanted the image to display you would just do
<?php randomImage(10); ?>. Given that you have 10 images that is. Also note that there is most likely a way to format a number to have the leading zeroes but I can't think of it off the top of my head. GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h-->++ r z+*
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h-->++ r z+*
•
•
Join Date: Apr 2005
Location: New York state
Posts: 487
Reputation:
Rep Power: 5
Solved Threads: 73
•
•
Join Date: Aug 2007
Location: Italy
Posts: 42
Reputation:
Rep Power: 2
Solved Threads: 4
Yet another function to display random images:
$path is where you put your images, then I check if $path is set and if it's ok I use glob to find all JPEG in the path ($dir variable, you can change it with gif or whatever). Finally, I use shuffle to randomize the order of the array ($filez variable). Bye
php Syntax (Toggle Plain Text)
<?php function randomize() { $path = $_SERVER['DOCUMENT_ROOT'] . "images/"; $dir = isset($_SERVER['DOCUMENT_ROOT']) ? $path : NULL; $filez = glob($dir . "*.jpg"); shuffle($filez); echo '<img src="/images/' . str_replace($dir , '', $filez[0]) . '" />'; } randomize(); ?>
$path is where you put your images, then I check if $path is set and if it's ok I use glob to find all JPEG in the path ($dir variable, you can change it with gif or whatever). Finally, I use shuffle to randomize the order of the array ($filez variable). Bye
•
•
Join Date: Mar 2006
Location: UK
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
I suspect this may be a lot more complex than I envisaged. It may help if I give you the gallery address http://www.tankedup-imaging.com/css_dev/opacity.html
The problem I'm having is changing both the thumbnail and center preview to the same image. Maybe it's not possible as is, I've had a go with no luck, but am suffering from an England football post match hangover
I'll have another try tomorrow.
Thanks for all the feedback.
The problem I'm having is changing both the thumbnail and center preview to the same image. Maybe it's not possible as is, I've had a go with no luck, but am suffering from an England football post match hangover

I'll have another try tomorrow.
Thanks for all the feedback.
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Random Images on Refresh (JavaScript / DHTML / AJAX)
- Implementing Random Images (Java)
- Need help making simple random images (PHP)
Other Threads in the PHP Forum
- Previous Thread: Form is wicked
- Next Thread: Getting Phone number


Linear Mode