473,769 Members | 6,926 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 11899
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
2180
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 shown method is called, the table gets skewed and the presentation of the data on the page goes horribly wrong. I don't think this is a table issue as I have spent alot of time staring at this code. Here is the html...
5
3001
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 <<<<<<<<<<<<<<CODE>>>>>>>>>>>>>>>> <html>
8
5481
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
23
2803
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 place a link on there site so people can vote for their program, "ad a click". eg someone clicks the link on some page and it counts +1 click on my page. if some one clicks the link below it will count a click on my page.
36
3110
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 highlights this line: <form name="frmCurrency" action="" method="post"> Page source....
0
5576
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
0
1891
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 shifts from it correct position i am setting its x and y position i havent done it before , always used the prototypes for such things ,here is my code snippet <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
2
1631
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 language="JavaScript" type="text/JavaScript"> <!-- function MM_reloadPage(init) { //reloads the window if Nav4 resized
0
2847
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. Here is the website: holtz-realty And also the html file and css file. Anny help will by mostly appreciated. I did try everything I can think of. HTML:
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.