displaying random images

Reply

Join Date: Feb 2008
Posts: 28
Reputation: alexgv14 is an unknown quantity at this point 
Solved Threads: 0
alexgv14 alexgv14 is offline Offline
Light Poster

displaying random images

 
0
  #1
Mar 1st, 2009
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.

  1. <?php
  2. function image()
  3. {
  4. $min = 1; //The first number one of your images has
  5. $max = 3; //The highest number you image has
  6. $number = rand($min, $max);
  7. $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
  8. echo "<img src='$url'>";
  9. }
  10. ?>
Last edited by alexgv14; Mar 1st, 2009 at 4:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 103
Reputation: jakx12 is an unknown quantity at this point 
Solved Threads: 4
jakx12's Avatar
jakx12 jakx12 is offline Offline
Junior Poster

Re: displaying random images

 
0
  #2
Mar 1st, 2009
ok, I suggest doing something like this:

  1. <?php
  2. function image()
  3. {
  4. $min = 1; //The first number one of your images has
  5. $max = 3; //The highest number you image has
  6. $number = rand($min, $max);
  7. $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
  8. echo "<img src='$url'>";
  9. }
  10. ?>

This means that all is needed is just a number for your images rather then having to name them all.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 28
Reputation: alexgv14 is an unknown quantity at this point 
Solved Threads: 0
alexgv14 alexgv14 is offline Offline
Light Poster

Re: displaying random images

 
0
  #3
Mar 1st, 2009
what if the images don't have a number?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 103
Reputation: jakx12 is an unknown quantity at this point 
Solved Threads: 4
jakx12's Avatar
jakx12 jakx12 is offline Offline
Junior Poster

Re: displaying random images

 
0
  #4
Mar 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,438
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 134
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: displaying random images

 
0
  #5
Mar 2nd, 2009
You could also just store the image names in an array and refer to the array with the number. An example is the following.
  1. <?php
  2. function image()
  3. {
  4. $min = 1; //The first number one of your images has
  5. $max = 3; //The highest number you image has
  6. $num = rand($min, $max);
  7. //set picture names
  8. $pic[1]='firstpic.jpg';
  9. $pic[2]='secondpic.jpg';
  10. $pic[3]='another_name.jpg';
  11. //end of setting picture names
  12. $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
  13. echo "<img src='$url'>";
  14. }
  15. ?>
  16.  
Very basic and hope it does the job.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 103
Reputation: jakx12 is an unknown quantity at this point 
Solved Threads: 4
jakx12's Avatar
jakx12 jakx12 is offline Offline
Junior Poster

Re: displaying random images

 
0
  #6
Mar 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,438
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 134
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: displaying random images

 
0
  #7
Mar 3rd, 2009
Originally Posted by jakx12 View Post
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.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 28
Reputation: alexgv14 is an unknown quantity at this point 
Solved Threads: 0
alexgv14 alexgv14 is offline Offline
Light Poster

Re: displaying random images

 
0
  #8
Mar 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC