perezf Posted September 10, 2006 Share Posted September 10, 2006 How do i make an image change on refresh of the pageim stuck without a clue Link to comment https://forums.phpfreaks.com/topic/20295-images-change/ Share on other sites More sharing options...
AndyB Posted September 10, 2006 Share Posted September 10, 2006 That would surely depend on whatever code you're using to generate the html code that displays the image. If the issue is cached images, then you can add a random number at the end of the image filename to make your browser think it's a different image and therefore fetch it from the server .... <img src="whatever.jpg?asdf1234"> Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89355 Share on other sites More sharing options...
perezf Posted September 10, 2006 Author Share Posted September 10, 2006 ok say i have <img src="image1.jpg" /> how can i make it where it changes with other images in a certain folder Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89386 Share on other sites More sharing options...
AndyB Posted September 10, 2006 Share Posted September 10, 2006 Get the image names into an array and select a random image from the array, then display it. Is there a small fixed number of images, a large fixed number of images, or could the number of images change frequently? Are the images all always in the same folder?You can use the glob() function to generate an array of specific filetypes from a folder. Examples in the online manual. rand() is also in the manual. Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89391 Share on other sites More sharing options...
perezf Posted September 10, 2006 Author Share Posted September 10, 2006 okay so lets say i have[code]$image = array("image1.jpg", "image2.gif", "image3.jpg");echo "<img src=\"$image\" />";[/code]how do i make it choose random Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89422 Share on other sites More sharing options...
extrovertive Posted September 10, 2006 Share Posted September 10, 2006 [code=php:0]$imgpath = 'path/to/my/crap/pic/';$dirp = opendir($imgpath); //open the directory$validimgtypes = array("gif", "jpg", "jpeg", "png", "bmp");$image = array(); //read the directory while($myfile = readdir($dirp)) { $picfileA = explode(".", $tutorfile); //delimit the file by . $picfileA = array_reverse($tutorfileA); //reverse array to get extension $ext = strtolower($picfileA[0]); //change ext to lowercase for case-sensitivity if(in_array($ext, $validimgtypes)) { $image[] = $myfile; } } closedir($dirp);//if there is at least 1 image in the folderif(count($image) > 1){$randimage = $image[array_rand($image)];echo "<img src=\"$randimage\" alt=\"$randimage\" />";}[/code] Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89443 Share on other sites More sharing options...
AndyB Posted September 10, 2006 Share Posted September 10, 2006 [quote author=perezf link=topic=107542.msg431690#msg431690 date=1157907199]okay so lets say i have ... how do i make it choose random[/quote][code]$image = array("image1.jpg", "image2.gif", "image3.jpg");$the_image = rand(0, count($image)-1);echo "<img src='". $the_image. "'/>";[/code] Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89465 Share on other sites More sharing options...
Barand Posted September 10, 2006 Share Posted September 10, 2006 Andy,$the_image = $image[array_rand($image)]; Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89475 Share on other sites More sharing options...
AndyB Posted September 10, 2006 Share Posted September 10, 2006 [quote author=Barand link=topic=107542.msg431745#msg431745 date=1157915196]Andy,$the_image = $image[array_rand($image)];[/quote]Aha! ... live and learn Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89498 Share on other sites More sharing options...
Barand Posted September 10, 2006 Share Posted September 10, 2006 Yours was just getting a random index, not the image. (I put it down to cocoa-deficiency ;D) Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89510 Share on other sites More sharing options...
extrovertive Posted September 10, 2006 Share Posted September 10, 2006 Hey, I posted that first! Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89546 Share on other sites More sharing options...
Barand Posted September 10, 2006 Share Posted September 10, 2006 Today, maybehttp://www.phpfreaks.com/forums/index.php/topic,93758.0.html Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89548 Share on other sites More sharing options...
extrovertive Posted September 10, 2006 Share Posted September 10, 2006 Of course, in this thread i mean. :P Link to comment https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89551 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.