I need to display all the images in a directory, inside a table no more that 4 columns wide (but I would like it to be easily customizable if I need to change it). It can be any number of rows, obviously depending on how many images there are.
The images will be stored in a different folder to the .asp page.
Here is some bare bones code that you can use to traverse you loop and display your images. This is set up for 4 columns and will go for ever. In our case it will only loop untill the For Next loop is satisified. If you want more columns just increase the i MOD 5 to something else. The number of columns will be 1 less than the # after MOD.
If your images are not in a Database then you should be able to use FSO (FileSystemObject) to traverse your directory. Here is a link to a quick reference of FSO http://www.tutorial-web.com/asp/fso/drive.asp
Code:
<%
option explicit
Dim i
Response.Write "<table border=1>"
Response.Write "<tr>"
For i = 0 To 10
If i Mod 5 Then
Response.Write "<td>TEST</td>"
Else
Response.Write "</tr><tr>"
End if
Next
Response.Write "</table> "
%>
I've managed to come up with the following code, but can't seem to get it to spread over 4 columns.
Code:
<%
Dim strThisPage
strThisPage = Request.ServerVariables("SCRIPT_NAME")
strThisPage = Right(strThisPage, Len(strThisPage) - 1)
'Path To Folder That holds Files To Download Here
'Default is the current Folder
FILE_FOLDER = StripFileName(Request.ServerVariables("PATH_TRANSLATED"))
%>
<html>
<head>
<title>Images</title>
<style type="text/css">
.TabHeader { Font-Family: Arial; Font-Weight: Bold; Font-Size: 12px; Background: Silver }
.DataCol { Font-Family: Verdana; Font-Size: 12px }
</style>
</head>
<body>
<table id=tblFileData background="">
<tr>
<td class=TabHeader></td>
</tr>
<%
Call GetAllFiles()
%>
</table>
</body>
</html>
<%
Sub GetAllFiles()
Dim oFS, oFolder, oFile
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
'Set Folder Object To Proper File Directory
Set oFolder = oFS.getFolder(FILE_FOLDER)
Dim intCounter
intCounter = 0
Dim FileArray()
ReDim Preserve FileArray(oFolder.Files.Count, 1)
For Each oFile in oFolder.Files
strFileName = oFile.Name
FileArray(intCounter, 0) = strFileName
FileArray(intCounter, 1) = "<a href=" & strFileName & "><img src=" & strFileName & "></a>"
intCounter = (intCounter + 1)
Next
intRows = uBound(FileArray, 1)
intCols = uBound(FileArray, 2)
For x = 0 To intRows -1
Echo("<tr>")
For z = 0 To intCols
If z > 0 Then
BuildTableCol(FileArray(x, z))
End If
Next
Echo("</tr>")
Next
EchoB("<B>" & oFolder.Files.Count & " Files Available</B>")
Cleanup oFile
Cleanup oFolder
Cleanup oFS
End Sub
Function Echo(str)
Echo = Response.Write(str & vbCrLf)
End Function
Function EchoB(str)
EchoB = Response.Write(str & "<br>" & vbCrLf)
End Function
Sub Cleanup(obj)
IF isObject(obj) Then
Set obj = Nothing
End IF
End Sub
Function StripFileName(strFile)
StripFileName = Left(strFile, inStrRev(strFile, "\"))
End Function
Sub BuildTableCol(strData)
Echo("<td class=DataCol>" & strData & "</td>")
End Sub
%>
The only changes i made were in the second For Each loop where you were displaying the images.
All i did was added a <table> tag (which you might need to remove. you may have your page set up differently.
I also pulled the <tr> tag to the outside of the loops. If you left them in the loop then on each loop it was creating a new row.
Then i added this if statement.
If x Mod 5 Then
'do nothing, we just need to display the image.
Else
Echo("</tr><tr>")
end if
Which basically says if x Mod 5 then Dispaly the image,
Code:
Sub GetAllFiles()
Dim oFS, oFolder, oFile
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
'Set Folder Object To Proper File Directory
Set oFolder = oFS.getFolder(FILE_FOLDER)
Dim intCounter
intCounter = 0
Dim FileArray()
ReDim Preserve FileArray(oFolder.Files.Count, 1)
For Each oFile in oFolder.Files
strFileName = oFile.Name
FileArray(intCounter, 0) = strFileName
FileArray(intCounter, 1) = "<a href=" & strFileName & "><img src=" & strFileName & "></a>"
intCounter = (intCounter + 1)
Next
intRows = uBound(FileArray, 1)
intCols = uBound(FileArray, 2)
Echo("<table>")
Echo("<tr>")
For x = 0 To intRows -1
For z = 0 To intCols
If z > 0 Then
If x MOD 5 Then
'Do nothing
else
Echo("</tr><tr>")
end if
BuildTableCol(FileArray(x, z))
End If
Next
Next
Echo("</tr>")
Echo("</table>")
EchoB("<B>" & oFolder.Files.Count & " Files Available</B>")
Cleanup oFile
Cleanup oFolder
Cleanup oFS
End Sub
You could do it multiple ways. You just need to check to see what the last 3 characters are. You could Split the strFileName @ the . and then use the upper bound of that array or you could just do a right like this.
If Right(strFileName, 3) = "JPG" then
FileArray(intCounter, 0) = strFileName
FileArray(intCounter, 1) = "<a href=" & strFileName & "><img src=" & strFileName & "></a>"
End if
NOW by doing this it will throw your count off. Your loop that displays the records is going to loop to intRows which is set to the total number of files in that directory. You will need to come up with a way. With out trying it what might work is to do this.
Code:
If Right(strFileName, 3) = "JPG" then
FileArray(intCounter, 0) = strFileName
FileArray(intCounter, 1) = "<a href=" & strFileName & "><img src=" & strFileName & "></a>"
intCounter = (intCounter + 1) 'place the counter in here that way you only count the records that matched your criteria.
End if
'Then isstead of using intRows = uBound(FileArray, 1) set your Rows to the counter.
intRows = intCounter
intCols = uBound(FileArray, 2)
Well an easy fix is this. This is tested and it works.
But i dont necessarily like it. But for a quick kill it will work.
The problem you are having is that when x = 0 then
x MOD 5 is not true so it drops in the else statement and ends the </tr> tag and starts a new one. Try this is should work.
If x > 0 then
If x MOD 5 Then
Else
Echo("</tr><tr>")
End If
End if
BuildTableCol(FileArray(x,z))
One very last question... on the filtering of file extensions, i'm using
Code:
If Right(strFileName, 3) = "bmp" or Right(strFileName, 3) = "gif" or Right(strFileName, 3) = "jpg" or Right(strFileName, 3) = "jpeg" or Right(strFileName, 3) = "png" or Right(strFileName, 3) = "tiff" Then
to pick up all 6 file types.
Is there a neater way of doing this? I've tried "bmp, gif" and "bmp", "gif" and "bmp" or "gif", all of which don't work.
Bookmarks