396,316 Members | 4,978 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 396,316 IT Pros & Developers. It's quick & easy.

help to display images from directory in a table style

jearnizck
P: 2
hi everyone! is there someone who can help me, i am a beginner in php, i have a code that displaying images from my images directory but i want it to be in a table style...my output of my code is displying in a single row...i want it to display a limit of 4 columns per row...here's my code...please help me! thanks in advance!
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3. //  Define  the  full  path  to  your  folder,  shouldn't  need  changing 
  4. $path  =  "./";
  5.  
  6. //  Open  the  folder 
  7. $dir_handle  =  @opendir($path)  or  die("Unable  to  open  folder");  
  8.  
  9. //create table
  10. echo '<table border=1 cellspacing=1 cellpadding=2 width=150%>';
  11.  
  12. //  Loop  through  the  files 
  13. while  (false  !==  ($file  =  readdir($dir_handle)))  {  
  14.  
  15. //  Prevent  this  file  itself  being  shown 
  16. //  If  you  want  it  to  not  display  any  other  files 
  17. //  enter  more  of  these 
  18.  
  19. if($file  ==  "index.php")  
  20. continue;  
  21. //Prevent  folders  showing 
  22. if($file  ==  ".")  
  23. continue;  
  24. if($file  ==  "..")  
  25. continue;
  26. if($file  ==  "bannerfinal.swf")  
  27. continue;  
  28. if($file  ==  "display2.php")  
  29. continue;  
  30. if($file  ==  "mu.jpg")  
  31. continue;
  32. if($file  ==  "LP_site.jpg")  
  33. continue;
  34. if($file  ==  "sabi.jpg")  
  35. continue;
  36. if($file  ==  "frenster.jpg")  
  37. continue;
  38. if($file  ==  "eriksite.jpg")  
  39. continue;
  40. if($file  ==  "bg2.jpg")  
  41. continue;
  42. if($file  ==  "bg3.jpg")  
  43. continue;
  44. if($file  ==  "md.jpg")  
  45. continue;
  46. if($file  ==  "gall.jpg")  
  47. continue;
  48.  
  49. //  Display  the  results 
  50.  
  51. //echo '<tr>';
  52.     echo '<td>' . "<a href='$file' target=_blank><img  src='$file'  alt='$file'  width='220' height='220'></a>" . '</td>';
  53. //echo '</tr>';
  54.  
  55.  
  56. }  
  57. echo '</table>';
  58. //  Close  it 
  59. closedir($dir_handle);  
  60.  
  61. ?>
  62.  
Jan 23 '08 #1

✓ answered by Atli

Hi. Welcome to TSDN!

First of all, I would recommend putting the images you want to be shown in another folder, separate from the files you do not want shown.
This would eliminate the need to filter out the files you don't want to show.

Also, you may want to check out the glob function to replace the readdir function. The glob function is much easier to work with and it gives you the option to filter the search results by a pattern.
And, different from the readdir function, the glob function returns all it finds as an array, so it can be sorted and/or modified as you want.

For example, if you want to list all files in a specific directory:
Expand|Select|Wrap|Line Numbers
  1. # The * in the glob pattern is used as a wildcard.
  2. $fileList = glob("path/to/dir/*.*")
  3.  
  4. # Print each file
  5. echo "Files found:";
  6. foreach($fileList as $file) {
  7.   echo " - ". $file;
  8. }
  9.  
Or, if you only want to see jpg or png images:
Expand|Select|Wrap|Line Numbers
  1. # Create pattern
  2. $globPattern = "{*.jpg, *.jpeg, *.png}";
  3.  
  4. # Get a list of files
  5. $fileList = glob($globPattern, GLOB_BRACE);
  6.  
  7. # Print each file
  8. echo "Files found:";
  9. foreach($fileList as $file) {
  10.   echo " - ". $file;
  11. }
  12.  
And, finally, getting to your actual question :P

You can use the modulus operator (%) to divide your table into four columns.
Like so:
Expand|Select|Wrap|Line Numbers
  1. echo "<table><tr>";
  2. # Loop through 15 times, each time incrementing $i by one.
  3. for($i = 0; $i < 15; $i++) 
  4. {
  5.   # Create a new row every four columns
  6.   if($i % 4 == 0 and $i != 0) 
  7.   {
  8.     echo "</tr><tr>";
  9.   }
  10.  
  11.   # Add a column
  12.   echo "<td>Column nr: $i</td>";
  13. }
  14. echo "</tr></table>";
  15.  
The modulus operator essentially divides the first number with the second number and returns the leftovers.

Another way to think of it is; the second number is subtracted from the first number until the first number is less then the second number, then what is left of the first number is returned.

So, by using how many columns have already been added as the first number, and 4 as the second number, the number returned will always be 0 every 4 rows.

Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,051
Hi. Welcome to TSDN!

First of all, I would recommend putting the images you want to be shown in another folder, separate from the files you do not want shown.
This would eliminate the need to filter out the files you don't want to show.

Also, you may want to check out the glob function to replace the readdir function. The glob function is much easier to work with and it gives you the option to filter the search results by a pattern.
And, different from the readdir function, the glob function returns all it finds as an array, so it can be sorted and/or modified as you want.

For example, if you want to list all files in a specific directory:
Expand|Select|Wrap|Line Numbers
  1. # The * in the glob pattern is used as a wildcard.
  2. $fileList = glob("path/to/dir/*.*")
  3.  
  4. # Print each file
  5. echo "Files found:";
  6. foreach($fileList as $file) {
  7.   echo " - ". $file;
  8. }
  9.  
Or, if you only want to see jpg or png images:
Expand|Select|Wrap|Line Numbers
  1. # Create pattern
  2. $globPattern = "{*.jpg, *.jpeg, *.png}";
  3.  
  4. # Get a list of files
  5. $fileList = glob($globPattern, GLOB_BRACE);
  6.  
  7. # Print each file
  8. echo "Files found:";
  9. foreach($fileList as $file) {
  10.   echo " - ". $file;
  11. }
  12.  
And, finally, getting to your actual question :P

You can use the modulus operator (%) to divide your table into four columns.
Like so:
Expand|Select|Wrap|Line Numbers
  1. echo "<table><tr>";
  2. # Loop through 15 times, each time incrementing $i by one.
  3. for($i = 0; $i < 15; $i++) 
  4. {
  5.   # Create a new row every four columns
  6.   if($i % 4 == 0 and $i != 0) 
  7.   {
  8.     echo "</tr><tr>";
  9.   }
  10.  
  11.   # Add a column
  12.   echo "<td>Column nr: $i</td>";
  13. }
  14. echo "</tr></table>";
  15.  
The modulus operator essentially divides the first number with the second number and returns the leftovers.

Another way to think of it is; the second number is subtracted from the first number until the first number is less then the second number, then what is left of the first number is returned.

So, by using how many columns have already been added as the first number, and 4 as the second number, the number returned will always be 0 every 4 rows.
Jan 24 '08 #2

jearnizck
P: 2
wow! i've learned a lot from you ATLI, thank you so much! i can reconstruct my code to more easy way! much appreciated! thanks!
Jan 24 '08 #3

Atli
Expert 5K+
P: 5,051
Glad I could help :)

Let us know if you run into any other problems or questions we can help with.
Jan 24 '08 #4

aptproductions
P: 1
Alti : just wanted to leave a note of thanks as your little bit of PHP is just what I was looking for to display various logos of awards for a school I'm working in on the footer of our new website instead of having to hard code them into it.
Nov 2 '09 #5

rfnobari
P: 1
Dear Atli , Thank you very mach that's very nice help for me

@Atli
Jan 23 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.