display random images from folder

Reply

Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: display random images from folder

 
0
  #11
Dec 10th, 2007
if thats the case, why not generate one more random number ?
  1. <?php
  2. $arr=array();
  3. for($i=0;$i<7;$i++){
  4. $arr[$i]="./img/images/".$i.".jpg";
  5. }
  6. $random=rand(0,6);
  7. $random_1=rand(0,6);
  8. echo "<img src=".$arr[$random].">";
  9. echo "<img src=".$arr[$random_1].">";
  10. ?>

This will do.. Don't you think ?
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: ezykiwi is an unknown quantity at this point 
Solved Threads: 0
ezykiwi ezykiwi is offline Offline
Newbie Poster

Re: display random images from folder

 
0
  #12
Dec 10th, 2007
that looks like it could do the trick with abit of working... basically....

i want there to be a folder of images... the names of those images could be anything at all.... number letters.. mixtures... there will be no logical order to them..... i just need the code to randomly pick say 10 images... out of that folder... no image being displayed twice etc...

if there isnt enough ... say there is 4 images.. it will just display 4

if there is no images... maybe just a statement showing there is no images to display at this time...
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: WebServant is an unknown quantity at this point 
Solved Threads: 0
WebServant WebServant is offline Offline
Newbie Poster

Re: display random images from folder

 
0
  #13
Mar 6th, 2008
Originally Posted by kkeith29 View Post
finished the code; i sent you a private message with the url of the test script
Hi, I am fairly new to web design and am trying to do exactly what you helped the other person on this post do, is there any way I could also get ahold of that code? I would be very grateful. Thanx.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1
Reputation: sreeneel is an unknown quantity at this point 
Solved Threads: 0
sreeneel sreeneel is offline Offline
Newbie Poster

Re: display random images from folder

 
0
  #14
Sep 30th, 2008
Hi,

I am new to Php. Iam unable to write a php code for random images on a webpage that displays new image each time it is refreshed. Im having images in a folder in my college FTP. Can any one post a code for me in Php ? Coz im unable to make the changes to my php file....
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 24
Reputation: angelic_devil is an unknown quantity at this point 
Solved Threads: 0
angelic_devil angelic_devil is offline Offline
Newbie Poster

Re: display random images from folder

 
0
  #15
May 2nd, 2009
plz post the script so that we understand what u did
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: display random images from folder

 
1
  #16
May 2nd, 2009
This is something I just typed up. Not the same as the one I get to the other poster, but close.

It will read a folder and get the images. Then randomize them and display in a paginated form.

  1. <?php
  2.  
  3. session_start();
  4.  
  5. $thispage = 'image.php';
  6. $imageDir = 'icons/';
  7. $perPage = 10; //total images to display per page
  8. $perRow = 2; //total images per row
  9.  
  10. if ( isset( $_GET['reset'] ) ) {
  11. unset( $_SESSION['chunks'] );
  12. }
  13.  
  14. if ( !isset( $_SESSION['chunks'] ) ) {
  15. if ( is_dir( $imageDir ) ) {
  16. if ( $dir = opendir( $imageDir ) ) {
  17. while ( ( $file = readdir( $dir ) ) !== false ) {
  18. if ( $file !== '.' && $file !== '..' ) {
  19. $images[] = $file;
  20. }
  21. }
  22. closedir( $dir );
  23. }
  24. else {
  25. die("Unable to read directory - {$imageDir}");
  26. }
  27. }
  28. shuffle( $images );
  29. $_SESSION['chunks'] = array_chunk( $images,$perPage );
  30. }
  31.  
  32. $page = 1;
  33. if ( isset( $_GET['page'] ) && $_GET['page'] > 0 ) {
  34. $page = (int) $_GET['page'];
  35. }
  36.  
  37. $chunk =& $_SESSION['chunks'];
  38. $total = count( $chunk );
  39. if ( isset( $chunk[$page-1] ) ) {
  40. $images = $chunk[$page-1];
  41. $html = "<table cellspacing=\"0\" cellpadding=\"3\">\n\t<tr>\n";
  42. $i = 0;
  43. foreach( $images as $image ) {
  44. if ( $i == $perRow ) {
  45. $html .= "\t</tr>\n\t<tr>\n";
  46. $i = 0;
  47. }
  48. $html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";
  49. $i++;
  50. }
  51. $html .= "\t<tr>\n";
  52. if ( $total > 1 ) {
  53. $nav = '';
  54. if ( $page > 1 ) {
  55. $nav .= "<a href=\"{$thispage}?page=1\">[FIRST]</a><a href=\"{$thispage}?page=" . ( $page - 1 ) . "\">[PREV]</a>";
  56. }
  57. $i = 1;
  58. while( $i < $total + 1 ) {
  59. $nav .= "<a href=\"{$thispage}?page={$i}\">[{$i}]</a>";
  60. $i++;
  61. }
  62. if ( $page < $total ) {
  63. $nav .= "<a href=\"{$thispage}?page=" . ( $page + 1 ) . "\">[NEXT]</a><a href=\"{$thispage}?page={$total}\">[LAST]</a>";
  64. }
  65. $html .= "\t<tr>\n\t\t<td colspan=\"{$perRow}\">{$nav}</td>\n\t</tr>\n";
  66. }
  67. $html .= "</table><br /><br /><a href=\"{$thispage}?reset=true\">Shuffle Images</a>";
  68. echo $html;
  69. }
  70.  
  71. ?>
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: poison23 is an unknown quantity at this point 
Solved Threads: 0
poison23 poison23 is offline Offline
Newbie Poster
 
0
  #17
Oct 21st, 2009
Hi..kkeith29
How can we make the image click able ?
wrt line 48 in your code
  1. $html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";
I tried
  1. $html .= "\t\t<td><a href="MYSITE.com/?v={$image}"<img src=\"{$imageDir}{$image}\" /></td>\n";

But I got error..
can you please help me to make the images displayed clickable in this format
mysite.com/?v={$image}

Thanks
Last edited by poison23; Oct 21st, 2009 at 2:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso
 
0
  #18
Oct 21st, 2009
Replace
  1. $html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";
with
  1. $html .= "\t\t<td><a href=\"http://www.mysite.com/?v={$image}\"><img src=\"{$imageDir}{$image}\" /></a></td>\n";
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: poison23 is an unknown quantity at this point 
Solved Threads: 0
poison23 poison23 is offline Offline
Newbie Poster
 
0
  #19
Oct 21st, 2009
Thanks
Reply With Quote Quick reply to this message  
Reply

Tags
image, random

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC