473,513 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help to display images from directory in a table style

2 New Member
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 24 '08 #1
5 11877
Atli
5,058 Recognized Expert Expert
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
2 New Member
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
5,058 Recognized Expert Expert
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
1 New Member
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
1 New Member
Dear Atli , Thank you very mach that's very nice help for me

@Atli
Jan 23 '12 #6

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

Similar topics

5
2162
by: Zambien | last post by:
Hi all, Here's my problem. I have tables that are using the menu/submenu idea for hiding rows. This works fine in IE (of course) and does show/hide correctly in netscape, but as soon as the...
5
2964
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
8
5452
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
23
2758
by: casper christensen | last post by:
Hi I run a directory, where programs are listed based on the number of clicks they have recieved. The program with most clicks are placed on top and so on. Now I would like people to be apple to...
36
3068
by: aljamala | last post by:
Hi, I keep getting this warning on a page, but I do not know what the problem is...does anyone have an idea about what could be wrong? line 88 column 7 - Warning: missing </formbefore <td> it...
0
5522
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
1875
omerbutt
by: omerbutt | last post by:
hi there i have tried to make my own AJAX tooltip it works all right and every things fine and the tooltip display right under the element i want to display untill unless i switch my resolution, it...
2
1610
by: registry | last post by:
<%@Language="VBSCRIPT" CODEPAGE="1252"%> <html> <head> <title>Registry Network Hospital Detail</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script...
0
2830
by: richard12345 | last post by:
Hi Guys I have problem with site I am building. The sidebar with menu and other thinks is overlapping footer. The footer move with the content and but it dos it dos not move with the sidebar. ...
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.