Page 1 of 1

projecting images in a folder into a table [solved]

Posted: Sun Jun 19, 2005 1:26 pm
by andylyon87
i have a folder of jpg images and i would like to put them into a table of 4images wide without using mySQL. would it be possible to count them and then divid the number by 4 and insert a seperate table for each row of 4.

If theres a more efficient way please tell me.

Andy

Posted: Sun Jun 19, 2005 1:39 pm
by Skara

Code: Select all

print('<table>');
$dir = opendir('images/');
$cols = 0;
while ($file = readdir($dir)) {
  if (!$cols) print('<tr>');
  print("<img src='{$file}' alt='{$file}' />");
  if ($cols > 3) { // adjust this number to change the number columns. 
    print('</tr>');
    $cols = 0;
  }
  else $cols++;
}
print('</table>');

Posted: Sun Jun 19, 2005 3:24 pm
by andylyon87
ok I have managed to get my files in an order on the page but i am having problems as there seems to be 2 file that I cannot see that are named "." and ".." what are these and how do I get them from the folders.

Posted: Sun Jun 19, 2005 3:27 pm
by timvw
. is a link to the current directory
.. is a link to the parent directory

So you don't need to get them... Just ignore them ;)

Posted: Sun Jun 19, 2005 3:29 pm
by andylyon87
but in the thing they get into the table so how do i remove them from the table as they are replacing the first 2 images

Posted: Sun Jun 19, 2005 3:38 pm
by andylyon87
fixed it

Code: Select all

<?print('<TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir("$id/");
$cols = 0;
while ($file = readdir($dir)) {
  if (!$cols){ print('<tr>');}
  if(is_file("$id/$file")){
  list($width, $height)=getimagesize("$id/$file");
  if($height<$width){
  print("<TD><img src=$id/$file alt=$file width=120></TD>");
  }
  if($height>=$width){
  print("<TD><img src=$id/$file alt=$file width=80></TD>");
  }
  if ($cols > 2) {
    print('</tr>');
    $cols = 0;
  }else{
  	$cols++;
  }}
}
print('</table>');
?>