Randome Image

Discussion in 'PHP' started by ottodo, Aug 17, 2006.

  1. #1
    Hello,
    I want to display randome image on my index page, is there any script can display randome image??

    Thanks
     
    ottodo, Aug 17, 2006 IP
  2. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #2
    I made a simple script that displays a random image. You need to create a folder called "images" and place all the images you want to randomize in it.
    
    <?
    $images_dir = "images/";
    $handle = opendir($images_dir);
    $r = 0;
    
    
    
    while ($file = readdir($handle)){
    		if(!is_dir($file) && !is_link($file)){
    			
    				$image[] = $file; 
    				$r++;
    		}
    	}
    
    $i = mt_rand(0,$r-1);
    echo "<img src=".$images_dir.$image[$i]." />";
    
    ?>
    
    
    Code (markup):
     
    danielbruzual, Aug 17, 2006 IP
  3. ottodo

    ottodo Guest

    Messages:
    2,055
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey Man,
    You doing great!!!

    thanks alot
    i'll test it now

    thanks :)
     
    ottodo, Aug 17, 2006 IP
  4. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #4
    Oops, I forgot to add the part that generates the random number :p.

    I edited my previous post, and the script should work now.
     
    danielbruzual, Aug 17, 2006 IP
  5. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $i = rand (0, $r-1);
    PHP:
    if there are 5 pictures in the array, your range would be 0 to 4 :) Oh, and mt_rand is 4 times faster than rand according to the manual.
     
    exam, Aug 17, 2006 IP
  6. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #6
    Thanks, I fixed it. I also change rand() with mt_rand().
     
    danielbruzual, Aug 17, 2006 IP
  7. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #7
    While we're on the subject, you should also initialize $image before the while loop, otherwise a user could manipulate it via the URL.
    $image = array ();
    PHP:
     
    exam, Aug 17, 2006 IP
  8. DemonMaster

    DemonMaster Active Member

    Messages:
    397
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    68
    #8
    How would you get like 4 random images and make it clickable
     
    DemonMaster, Aug 19, 2006 IP
  9. Caesar

    Caesar Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    or.

    tablo.image
    ++++++++++++++++++
    ++id+++image_name+++
    ++1+++12345.jpg+++++
    ++2+++453.jpg+++++++
    ++4+++def.jpg+++++++

    
    $query=mysql_query("select id,image_name from image order by rand() limit 0,1");
    $img=mysql_result($query,0,'image_name');
    echo "<img src='img/$img' border='0' />";
    
    PHP:
     
    Caesar, Aug 23, 2006 IP