healthbasics Posted June 16, 2010 Share Posted June 16, 2010 Could I code something in php that would take a static image on my website and randomly rotate (whenever someone loads the page) among 3 or 4 or 5 different selections of image all of the same format, size, dimension, etc? If so how could I do that. Thanks. Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/ Share on other sites More sharing options...
hcdarkmage Posted June 16, 2010 Share Posted June 16, 2010 Fairly simple code: <?php $pic = array( "image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg" ); echo "<img src='img/".$pic[rand(1,5)]."' />"; ?> Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/#findComment-1073080 Share on other sites More sharing options...
healthbasics Posted June 16, 2010 Author Share Posted June 16, 2010 Awesome! Thanks again! Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/#findComment-1073081 Share on other sites More sharing options...
healthbasics Posted June 16, 2010 Author Share Posted June 16, 2010 I guess I screwed something up. Can I call it like this or no? Do i have to cram that whole statement together as one into the spot where I want the image or did I just put a quote in the wrong place or something? <?php $pic = array( "shot1.jpg", "shot2.jpg", ); ?> </div> <div id="right-column"> <div id="main-image"><img src="<?php echo "/img/'.$pic[rand(1,2)].'" ?>" height="240px" width="240px" alt="" /></div> <div class="sidebar"> <ul id="menu"> My pics are stored in a subdir called img/ from the root. mydomain.com/img/ for example. Thanks. Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/#findComment-1073093 Share on other sites More sharing options...
Alex Posted June 16, 2010 Share Posted June 16, 2010 <?php echo "/img/" . $pic[rand(1,2)]; ?> Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/#findComment-1073096 Share on other sites More sharing options...
healthbasics Posted June 16, 2010 Author Share Posted June 16, 2010 halfway there. This time it appears to work half the time. Half the time it returns: <div id="main-image"><img src="/img/" height="240px" width="240px" alt="" /></div> and the other half <div id="main-image"><img src="/img/shot2.jpg" height="240px" width="240px" alt="" /></div> It seems like whenever it selects shot1.jpg from the random function it returns nothing.... Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/#findComment-1073102 Share on other sites More sharing options...
hcdarkmage Posted June 16, 2010 Share Posted June 16, 2010 try: <?php echo "/img/" . $pic[rand(0,2)]; ?> I always had a problem with array syntax. Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/#findComment-1073105 Share on other sites More sharing options...
Alex Posted June 16, 2010 Share Posted June 16, 2010 Didn't even notice that. The array indices start at 0, so it should be: rand(0, 1) Or you can just use: $pic[array_rand($pic)]; Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/#findComment-1073110 Share on other sites More sharing options...
hcdarkmage Posted June 16, 2010 Share Posted June 16, 2010 There we go! That's what I was looking for. Try AlexWD's suggestion: $pic[array_rand($pic)]; Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/#findComment-1073111 Share on other sites More sharing options...
healthbasics Posted June 16, 2010 Author Share Posted June 16, 2010 Not quite but following your logic I got it. what i needed was <?php echo "/img/" . $pic[rand(0,1)]; ?> <?php echo "/img/" . $pic[rand(0,2)]; ?> gave me each picture sometimes and once in a while a blank. I figure 0,2 must be an array of 3 with values of 0, 1, 2... Thanks! Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/#findComment-1073114 Share on other sites More sharing options...
healthbasics Posted June 16, 2010 Author Share Posted June 16, 2010 an even more elegant solution. thanks Alex! Link to comment https://forums.phpfreaks.com/topic/204970-php-code-for-random-image-rotation/#findComment-1073117 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.