Computer Science Canada

Limit images displayed.

Author:  Amailer [ Tue Jul 22, 2003 8:51 pm ]
Post subject:  Limit images displayed.

K,

Im not using a database for this so i don't know how to display a limited number of images from a folder?

Suppose i got 10 files in a folder...
i want it to display 5 fils only.

Anyone?

Author:  PaddyLong [ Wed Jul 23, 2003 6:41 pm ]
Post subject: 

what I would do is load all the file names into an array then just run a for loop that's like
code:

for($i=0; $i<5; $i++){
print ("<img src=\"directory/".imagearray[$i].".jpg/" other img tags>")
}

Author:  Amailer [ Wed Jul 23, 2003 6:47 pm ]
Post subject: 

ok i got it, but i just got a lil prob..ill ask for help if i need.

BTW thx for helping...:/


EDIT:

k...odd it displays only the top 2 it happned before...AND ITS HAPPNING AGAIN!

k even searched on google...event hey got the same prob...
code:

                if (!is_dir($default_dir)) die("Cannot open images for type: $type");

                if (!($dp = opendir($default_dir))) die("Cannot open $default_dir.");

                while  (FALSE !=($file = readdir($dp)))

                        $file_array[] = $file;

                        for($i=0; $i<5; $i++) {

               
                //if ($file != '.' && $file != '..' && ereg("$type", $file))

echo  "<body></body><center><img border=\"0\" src=\"$default_dir/" . $file_array[$i] . "\"><br><b>File name:</b> $file</center><br><br></html>";
                }

Author:  PaddyLong [ Thu Jul 24, 2003 11:01 am ]
Post subject: 

well... it seems that you are loading into the array the . and .. and I think there's also one other funny little hidden file that's sometimes there it's like thumbs.db or something .... try something... instead of printing the img tag out just print the file name out so you can see what images it's trying to do...

the solution to this would be to put the if statement for !="." etc in your while loop where you load the file names into the array

Author:  Amailer [ Thu Jul 24, 2003 11:48 am ]
Post subject: 

Hmm no,
i got that...thing is that i commented the
code:

if ($file_array[$i] != '.' && $file_array[$i] != '..' && ereg("$type", $file_array[$i]))


Which actually...takes out the files that are not part of the extentions give...hehe :/

so i got it to work

but now....if i put the limit to 5..it will display 3..so to display 5..i need to put the limit to 7 :/

ah well :/

Author:  PaddyLong [ Fri Jul 25, 2003 2:20 pm ]
Post subject: 

it's exactly because of what I said... the first two indexes of your array are in fact the . and .. because you don't have the if statement inside of the loop where you are scanning the directory... all you are doing with the if statement in the for loop is skipping over outputting the img tag for those indexes of the array

make it this, and it should work...

code:

      if (!is_dir($default_dir)) die("Cannot open images for type: $type");

      if (!($dp = opendir($default_dir))) die("Cannot open $default_dir.");

      while  (FALSE !=($file = readdir($dp))){
         if ($file != '.' && $file != '..' && ereg("$type", $file))
         $file_array[] = $file;
      }

      for($i=0; $i<5; $i++) {
         echo  "<body></body><center><img border=\"0\" src=\"$default_dir/" . $file_array[$i] . "\"><br><b>File name:</b> $file</center><br><br></html>";
      }

Author:  Amailer [ Fri Jul 25, 2003 3:30 pm ]
Post subject: 

thx but i did it another way,


: