User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Mar 2006
Location: UK
Posts: 6
Reputation: tankedup is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tankedup tankedup is offline Offline
Newbie Poster

random images

  #1  
Sep 12th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2005
Location: New York state
Posts: 487
Reputation: ShawnCplus will become famous soon enough ShawnCplus will become famous soon enough 
Rep Power: 5
Solved Threads: 73
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: random images

  #2  
Sep 12th, 2007
The simplest would be something like this
  1. <?php
  2. function randomImage($numImages = 10)
  3. {
  4. srand( time(NULL));
  5. $rand = rand()%$numImages;
  6. $ranImage = '<div class="thumb">
  7. <a href="image'.$rand.'.html">
  8. <img src="images/image'.$rand.'_thumb.gif" alt="Image'.$rand.'" />
  9. <span><img src="images/pre_image01.gif" alt="Image01" />
  10. <img src="images/pre_image'.$rand.'.gif" alt="Image'.$rand.'" />
  11. <em>Image'.$rand.'</em>
  12. </span>
  13. </a>
  14. </div>
  15. ';
  16. $echo $ranImage;
  17. }
  18. ?>

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+*
Reply With Quote  
Join Date: Mar 2006
Location: UK
Posts: 6
Reputation: tankedup is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tankedup tankedup is offline Offline
Newbie Poster

Re: random images

  #3  
Sep 12th, 2007
Thanks ShawnCplus for the (very) quick reply! I'll have a go at it later tonight.

Thanks again, I must come here more often
Reply With Quote  
Join Date: Apr 2005
Location: New York state
Posts: 487
Reputation: ShawnCplus will become famous soon enough ShawnCplus will become famous soon enough 
Rep Power: 5
Solved Threads: 73
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: random images

  #4  
Sep 12th, 2007
whoops, delete line 9, that was a little booboo
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+*
Reply With Quote  
Join Date: Aug 2007
Location: Italy
Posts: 42
Reputation: cereal is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
cereal cereal is offline Offline
Light Poster

Re: random images

  #5  
Sep 13th, 2007
Yet another function to display random images:

  1. <?php
  2.  
  3. function randomize() {
  4. $path = $_SERVER['DOCUMENT_ROOT'] . "images/";
  5. $dir = isset($_SERVER['DOCUMENT_ROOT']) ? $path : NULL;
  6. $filez = glob($dir . "*.jpg");
  7. shuffle($filez);
  8. echo '<img src="/images/' . str_replace($dir , '', $filez[0]) . '" />';
  9. }
  10.  
  11. randomize();
  12.  
  13. ?>

$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
Reply With Quote  
Join Date: Mar 2006
Location: UK
Posts: 6
Reputation: tankedup is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tankedup tankedup is offline Offline
Newbie Poster

Re: random images

  #6  
Sep 13th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 3:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC