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 Quote Link to comment 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"> Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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] Quote Link to comment 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] Quote Link to comment Share on other sites More sharing options...
Barand Posted September 10, 2006 Share Posted September 10, 2006 Andy,$the_image = $image[array_rand($image)]; Quote Link to comment 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 Quote Link to comment 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) Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 10, 2006 Share Posted September 10, 2006 Hey, I posted that first! Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.