projecting images in a folder into a table [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

projecting images in a folder into a table [solved]

Post 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
Last edited by andylyon87 on Sun Jun 19, 2005 3:40 pm, edited 1 time in total.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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>');
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 ;)
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post 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
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post 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>');
?>
Post Reply