944,108 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 15382
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 10th, 2007
0

Re: display random images from folder

if thats the case, why not generate one more random number ?
php Syntax (Toggle Plain Text)
  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 ?
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Dec 10th, 2007
0

Re: display random images from folder

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...
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ezykiwi is offline Offline
2 posts
since Dec 2007
Mar 6th, 2008
0

Re: display random images from folder

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
WebServant is offline Offline
1 posts
since Mar 2008
Sep 30th, 2008
0

Re: display random images from folder

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....
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sreeneel is offline Offline
1 posts
since Sep 2008
May 2nd, 2009
0

Re: display random images from folder

plz post the script so that we understand what u did
Reputation Points: 10
Solved Threads: 1
Newbie Poster
angelic_devil is offline Offline
24 posts
since May 2009
May 2nd, 2009
1

Re: display random images from folder

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.

PHP Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 21st, 2009
0
Re: display random images from folder
Hi..kkeith29
How can we make the image click able ?
wrt line 48 in your code
PHP Syntax (Toggle Plain Text)
  1. $html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";
I tried
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
poison23 is offline Offline
2 posts
since Oct 2009
Oct 21st, 2009
0
Re: display random images from folder
Replace
PHP Syntax (Toggle Plain Text)
  1. $html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";
with
PHP Syntax (Toggle Plain Text)
  1. $html .= "\t\t<td><a href=\"http://www.mysite.com/?v={$image}\"><img src=\"{$imageDir}{$image}\" /></a></td>\n";
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 21st, 2009
0
Re: display random images from folder
Thanks
Reputation Points: 10
Solved Threads: 1
Newbie Poster
poison23 is offline Offline
2 posts
since Oct 2009
Sep 10th, 2010
0
Re: display random images from folder
Can I put this in an asp.net page?

I'm not sure where to put this php code.. ? Sorry
Last edited by keltoid; Sep 10th, 2010 at 1:55 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
keltoid is offline Offline
19 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Updating a record by replacing it
Next Thread in PHP Forum Timeline: Get variables from Action Script using PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC