Jump to content


Photo

Can someone tell me what's wrong with this random image selector i made?


  • Please log in to reply
4 replies to this topic

#1 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 04 May 2006 - 03:11 AM

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:

        <?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];
        }

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?
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#2 Guest_askjames01_*

Guest_askjames01_*
  • Guests

Posted 04 May 2006 - 04:10 AM

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

    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];
   }

just don't gorget the new condition inside elseif.
this might solve your problem


#3 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 04 May 2006 - 04:22 AM

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:

                $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];        

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.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 04 May 2006 - 04:25 AM

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 fine

p.s.- maybe you should try using || instead of && (or instead of and) in your if statement
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 04 May 2006 - 04:26 AM

[!--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) View Post[/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 fine

p.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.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users