play_ Posted May 4, 2006 Share Posted May 4, 2006 Ok ill be honest. It's been quite a bit since ive done php and now im redoing my site. Instead of using the old scrips ive made, im making everything from scratch, to refresh the memory.Im making a script that pics a random image out of a directory. It looks like this:[code] <?php //Random image loader. $path = './images/logos/'; $dir = opendir($path) or die('Could not open directory for main image selection.'); $i = 0; // Loops through directory and append filenames to the array 'file'. while( ($file[$i] = readdir($dir)) ) { $i++; } // pick a random item (image name) out of the 'file' array :: $file[1] or $file[2], etc. $image = array_rand($file); if( ($file[$image] != '.') && ($file[$image] != '') && ($file[$image] != 'Thumbs.db') && ($file[$image] != '..') && ($file != ' ') ) { $all = $path.$file[$image]; echo '<img src="'. $all .'" />'; } else { echo $file[$image]; }[/code]As you can see, in the 'else' statement, it prints the name of the image/file (i put it there because sometimes the image doesnt show up). And so, when the image doesn't show up, it prints either 'Thumbs.db' or '.' or '..'.Why is that happening, when i have that long ass 'if' statement there telling the script to not print those files? Quote Link to comment https://forums.phpfreaks.com/topic/9025-can-someone-tell-me-whats-wrong-with-this-random-image-selector-i-made/ Share on other sites More sharing options...
Guest askjames01 Posted May 4, 2006 Share Posted May 4, 2006 i suggest you use elseif instead of else only...because sometimes if else alone doesn't work as you expected.try changing it if this doesn't work then just pm again.so[code] if( ($file[$image] != '.') && ($file[$image] != '') && ($file[$image] != 'Thumbs.db') && ($file[$image] != '..') && ($file != ' ') ) { $all = $path.$file[$image]; echo '<img src="'. $all .'" />'; } elseif('your condition here') { echo $file[$image]; }[/code]just don't gorget the new condition inside elseif.this might solve your problem Quote Link to comment https://forums.phpfreaks.com/topic/9025-can-someone-tell-me-whats-wrong-with-this-random-image-selector-i-made/#findComment-33193 Share on other sites More sharing options...
play_ Posted May 4, 2006 Author Share Posted May 4, 2006 Hey James. It just so happens i figured a way to make it work (right after you posted too....i waited a few minutes and no one replied, so i figured everyone was asleep).Here is what i came up with:[code] $path = './images/logos/'; $dir = opendir($path) or die('Could not open directory for main image selection.'); $images = array(); while ( !(($file = readdir($dir)) === false)) { if ( ($file != 'Thumbs.db') && ($file != '.') && ($file != '..') ){ array_push($images, $file); } } shuffle($images); print $images[0]; [/code]It's pretty small and so far works fine. If you can see something that might not be well written, let me know though.But, i still dont understand why in that first script i posted, php was selecting 'Thumbs.db' and '.' and '..' from the directory even though i had an IF statement telling it to skip those files. Quote Link to comment https://forums.phpfreaks.com/topic/9025-can-someone-tell-me-whats-wrong-with-this-random-image-selector-i-made/#findComment-33194 Share on other sites More sharing options...
.josh Posted May 4, 2006 Share Posted May 4, 2006 well umm.. i tested your code (from original post) on my server. the only thing i changed was change the $path to ./images/ instead of ./images/logos/ (to suit my own directories) and it worked just finep.s.- maybe you should try using || instead of && (or instead of and) in your if statement Quote Link to comment https://forums.phpfreaks.com/topic/9025-can-someone-tell-me-whats-wrong-with-this-random-image-selector-i-made/#findComment-33197 Share on other sites More sharing options...
play_ Posted May 4, 2006 Author Share Posted May 4, 2006 [!--quoteo(post=371146:date=May 4 2006, 12:25 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 4 2006, 12:25 AM) [snapback]371146[/snapback][/div][div class=\'quotemain\'][!--quotec--]well umm.. i tested your code (from original post) on my server. the only thing i changed was change the $path to ./images/ instead of ./images/logos/ (to suit my own directories) and it worked just finep.s.- maybe you should try using || instead of && (or instead of and) in your if statement[/quote]Oh.If you upload a directory full of images to your server, it till upload 'Thumbs.db' with it.Keep clicking refresh on your browser, and you'll see that sometimes the image won't show up. Quote Link to comment https://forums.phpfreaks.com/topic/9025-can-someone-tell-me-whats-wrong-with-this-random-image-selector-i-made/#findComment-33198 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.