Jump to content

Creating filters over results of an ASP page


ember

Recommended Posts

Hello everyone. i've been learning a lot here in the past few weeks, but unfortunately it's not enough for what i need now...Here it goes:I have an ASP script wich scans a given directory and returns information on the files inside it (name, type, size, date last mod, owner and author). Everything works fine, but now i need a new element on this - a way to filter the results. someone i know has told me something about recordsets, but all the info i can find is about connections with Access databases.so, i'd like to ask if there is a way to sort my problem out, and if you know any links or online documentation to do so.best regards and thanks in advance, ember

Link to comment
Share on other sites

i've thought about that, also, but as you may have noticed, i'm a newbie on asp... it has to display the results and only after that be able to filter them, using something like javascript dropdown navigation menus or something similar.i didn't quite understand your question about the files though...anyway, thanks for your reply :)

Link to comment
Share on other sites

Are you talking about filtering, which is changing which files to show, or sorting, which is changing the order that they show up in? If you want to use Javascript to do it, you will probably have to wrap each file in a div with an ID, and then write Javascript functions using ASP that will show and hide specific files for each filter function. It's not the easiest thing, but it will work. You could also instead write out all of the file information into a javascript array, and then have functions that will display, sort or filter the data. That will be more complex, but more powerful.

Link to comment
Share on other sites

yep, filtering, the page already has a sort function in it.the main problem is that the index has to be dynamic, so you don't have to be changing it each time you add or modify a file in the directory...maybe if i show you the code you can see the main idea better?thanks again :)

Link to comment
Share on other sites

yep, filtering, the page already has a sort function in it.the main problem is that the index has to be dynamic, so you don't have to be changing it each time you add or modify a file in the directory...maybe if i show you the code you can see the main idea better?thanks again :)
So are you going to show us the code?
Link to comment
Share on other sites

sure, here it goes :)

<%' In this demo, at least, we don't allow user to change directories...' Change the DIRECTORY to point to any virtual directory of your choice.CONST DIRECTORY = "/" ' relative path in virtual directories'Option Explicit 'These consts are only valid for default file-folders 'virtual folders may have their own ids! Const FILE_NAME = 0 CONST FILE_SIZE = 1 CONST FILE_TYPE = 2 CONST FILE_MODIFIED = 3 Const FILE_CREATED = 4 Const FILE_ACCESSED = 5 Const FILE_OWN = 8 Const FILE_AUT = 10 ' get requested sort order, if not first time here...' (forward by name is default)req = Request("sortBy")If Len(req) < 1 Then sortBy = 0 Else sortBy = CInt(req)req = Request("priorSort")If Len(req) < 1 Then priorSort = -1 Else priorSort = CInt(req)'' did user ask for same sort? to reverse the order?' but if so, then zap priorSort so clicking again will do forward!If sortBy = priorSort Then	reverse = true	priorSort = -1Else	reverse = false	priorSort = sortByEnd If' now start the *real* code...'Dim path Dim sh Dim oShellFolder Dim theFiles Dim info path = Server.MapPath( DIRECTORY ) Set sh = CreateObject("Shell.Application") Set oShellFolder = sh.Namespace( path ) theFiles = getItemsInfo(oShellFolder) 'dump result, replace WSH.Echo for ASP For Each info in theFiles					   		'WSH.Echo join(info ,"*") Next 'WSH.StdIn.ReadLine() ' Function getItemsInfo (oShF) 		Dim ######em	   		Dim currentSlot 		Dim p 		Dim fname 		Dim fext 		Dim ftype	   		Dim fcreate 		Dim fmod 		Dim faccess 		Dim fsize 		Dim fauthor 		Dim fowner 		ReDim theFiles(oShF.Items.Count-1) 		currentSlot = -1 		For Each ######em in oShF.Items 		 'if ######em.isFolder Then ######em.GetFolder		 		 						 fname = ######em.Name	 				p = instrrev(fname,".") 				If (p> 0) Then fext = Mid(fname, p+1,len(fname)) Else fext = fname				ftype = oShF.GetDetailsOf(######em, FILE_TYPE)	 				fsize = oShF.GetDetailsOf(######em, FILE_SIZE)			 				fmod = oShF.GetDetailsOf(######em, FILE_MODIFIED)								 				fcreate = oShF.GetDetailsOf(######em, FILE_CREATED) 				faccess = oShF.GetDetailsOf(######em, FILE_ACCESSED) 				fauthor = oShF.GetDetailsOf(######em, FILE_AUT)   				fowner = oShF.GetDetailsOf(######em, FILE_OWN) 				currentSlot = currentSlot + 1 				theFiles(currentSlot) = Array(fname, fext, ftype, fsize, fmod, fauthor, fowner) 		 		 'end ifNext		getItemsInfo = theFiles 'End Function '' files are now in the array...'' As noted, it is actually an ARRAY *OF* ARRAYS. Which makes' picking the column we will sort on easier!'' ...size and sort it...fileCount = currentSlot ' actually, count is 1 more, since we start at 0ReDim Preserve theFiles( currentSlot ) ' really not necessary...just neater!' First, determine which "kind" of sort we are doing.' (VarType=8 means "string")'If VarType( theFiles( 0 )( sortBy ) ) = 8 Then	If reverse Then kind = 1 Else kind = 2 ' sorting strings...Else	If reverse Then kind = 3 Else kind = 4 ' non-strings (numbers, dates)End If'' A simple bubble sort for now...easier to follow the code...'For i = fileCount TO 0 Step -1	minmax = theFiles( 0 )( sortBy )	minmaxSlot = 0	For j = 1 To i		Select Case kind ' which kind of sort are we doing?		' after the "is bigger/smaller" test (as appropriate),		' mark will be true if we need to "remember" this slot...		Case 1 ' string, reverse...we do case INsensitive!			mark = (strComp( theFiles(j)(sortBy), minmax, vb script:reSort(0);">File name</A></TH>	<TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(1);">Extension</A></TH>	<TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(2);">Type</A></TH>	<TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(3);">Size</A></TH>	<TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(4);">Last modified</A></TH>	<TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(5);">Author</A></TH>	<TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(6);">Owner</A></TH></TR><%'With the array nicely sorted, this part is a piece of cake!For i = 0 To fileCount   Response.Write "<TR class=normal>" & vbNewLine	For j = 0 To UBound( theFiles(i) )		Response.Write "	<TD><a href= """ & path & "/" & fname & """ >"& theFiles(i)(j) & "</a></TD>" & vbNewLine	'End Function	Next	Response.Write "</TR>" & vbNewLineNextEnd Function%>

i still have some tweaking to do, that's to make the script also scan folders inside the root folder, and where it displays the name of the file for it to have a link to the file.thanks everyone!

Link to comment
Share on other sites

There are some strange things going on here. There are a lot of control statements commented out, it makes it a little difficult to see what's going on. The block that starts with this line:mark = (strComp( theFiles(j)(sortBy), minmax, vb script:reSort(0);">File name</A></TH>confuses me. The paren that comes before strComp never closes, and it looks like you just start writing out HTML in the middle of the HTML, and without closing the ASP tag. After the block you re-open the ASP tag, but it never closed in the first place. So there are some issues with the code that you probably need to work out before you get to filtering.To filter the files, first figure out how you want to filter them (based on size? name? type? etc). It's probably easiest to use an IF statement where you display the files to determine if the file meets your criteria to display.

Link to comment
Share on other sites

I don't know what happened, but a part of the code was eaten over there!i'll try to post it again:

	<%' In this demo, at least, we don't allow user to change directories...' Change the DIRECTORY to point to any virtual directory of your choice.CONST DIRECTORY = "/" ' relative path in virtual directories 'These consts are only valid for default file-folders 'virtual folders may have their own ids! Const FILE_NAME = 0 CONST FILE_SIZE = 1 CONST FILE_TYPE = 2 CONST FILE_MODIFIED = 3 Const FILE_CREATED = 4 Const FILE_ACCESSED = 5 Const FILE_OWN = 8 Const FILE_AUT = 10 ' get requested sort order, if not first time here...' (forward by name is default)req = Request("sortBy")If Len(req) < 1 Then sortBy = 0 Else sortBy = CInt(req)req = Request("priorSort")If Len(req) < 1 Then priorSort = -1 Else priorSort = CInt(req)'' did user ask for same sort? to reverse the order?' but if so, then zap priorSort so clicking again will do forward!If sortBy = priorSort Then    reverse = true    priorSort = -1Else    reverse = false    priorSort = sortByEnd If' now start the *real* code...'Dim path Dim sh Dim oShellFolder Dim theFiles Dim info path = Server.MapPath( DIRECTORY ) Set sh = CreateObject("Shell.Application") Set oShellFolder = sh.Namespace( path ) theFiles = getItemsInfo(oShellFolder) For Each info in theFiles                       Next Function getItemsInfo (oShF)         Dim ######em               Dim currentSlot         Dim p         Dim fname         Dim fext         Dim ftype               Dim fcreate         Dim fmod         Dim faccess         Dim fsize         Dim fauthor         Dim fowner         ReDim theFiles(oShF.Items.Count-1)         currentSlot = -1         For Each ######em in oShF.Items 		                                   fname = ######em.Name                     p = instrrev(fname,".")                 If (p> 0) Then fext = Mid(fname, p+1,len(fname)) Else fext = fname                ftype = oShF.GetDetailsOf(######em, FILE_TYPE)                     fsize = oShF.GetDetailsOf(######em, FILE_SIZE)                             fmod = oShF.GetDetailsOf(######em, FILE_MODIFIED)                                                 fcreate = oShF.GetDetailsOf(######em, FILE_CREATED)                 faccess = oShF.GetDetailsOf(######em, FILE_ACCESSED)                 fauthor = oShF.GetDetailsOf(######em, FILE_AUT)                   fowner = oShF.GetDetailsOf(######em, FILE_OWN)                 currentSlot = currentSlot + 1                 theFiles(currentSlot) = Array(fname, fext, ftype, fsize, fmod, fauthor, fowner)          Next        getItemsInfo = theFiles '' files are now in the array...'' As noted, it is actually an ARRAY *OF* ARRAYS. Which makes' picking the column we will sort on easier!'' ...size and sort it...fileCount = currentSlot ' actually, count is 1 more, since we start at 0ReDim Preserve theFiles( currentSlot ) ' really not necessary...just neater!' First, determine which "kind" of sort we are doing.' (VarType=8 means "string")'If VarType( theFiles( 0 )( sortBy ) ) = 8 Then    If reverse Then kind = 1 Else kind = 2 ' sorting strings...Else    If reverse Then kind = 3 Else kind = 4 ' non-strings (numbers, dates)End If'' A simple bubble sort for now...easier to follow the code...'For i = fileCount TO 0 Step -1    minmax = theFiles( 0 )( sortBy )    minmaxSlot = 0    For j = 1 To i        Select Case kind ' which kind of sort are we doing?        ' after the "is bigger/smaller" test (as appropriate),        ' mark will be true if we need to "remember" this slot...        Case 1 ' string, reverse...we do case INsensitive!            mark = (strComp( theFiles(j)(sortBy), minmax, vbTextCompare ) < 0)        Case 2 ' string, forward...we do case INsensitive!            mark = (strComp( theFiles(j)(sortBy), minmax, vbTextCompare ) > 0)        Case 3 ' non-string, reverse ...            mark = (theFiles( j )( sortBy ) < minmax)        Case 4 ' non-string, forward ...            mark = (theFiles( j )( sortBy ) > minmax)        End Select        ' so is the current slot bigger/smaller than the remembered one?        If mark Then            ' yep, so remember this one instead!            minmax = theFiles( j )( sortBy )            minmaxSlot = j        End If    Next    ' is the last slot the min (or max), as it should be?    If minmaxSlot <> i Then        ' nope...so do the needed swap...        temp = theFiles( minmaxSlot )        theFiles( minmaxSlot ) = theFiles( i )        theFiles( i ) = temp    End IfNext' Ta-da! The array is sorted!'%>

i don't know why, but a part of the code was munched when i pasted it!so here comes the first part, and in the next reply will be the second.here comes the second part:

<FORM Name="doSort" Method="Get"><INPUT Type=Hidden Name=priorSort Value="<% = priorSort %>"><INPUT Type=Hidden Name=sortBy Value="-1"></FORM><script Language="JavaScript">function reSort( which ){    document.doSort.sortBy.value = which;    document.doSort.submit( );}</SCRIPT><CENTER><b></b><FONT class=normal bgColor=#cecfce width="100%"><font face="Arial" size="4"></b></FONT></font><p> </p><p><FONT class=normal bgColor=#cecfce width="100%"><font face="Arial" size="4"> Showing <% = (fileCount+1) %> files from directory <% = path %></FONT></p><P></P><P></P><p> </p><p> </p><TABLE Border=0 CellPadding=3><TR>    <TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(0);">File name</A></TH>    <TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(1);">Extension</A></TH>    <TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(2);">Type</A></TH>    <TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(3);">Size</A></TH>    <TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(4);">Last modified</A></TH>    <TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(5);">Author</A></TH>    <TH bgcolor=#CECFCE class=normal align=center><A HREF="java script:reSort(6);">Owner</A></TH></TR><%'With the array nicely sorted, this part is a piece of cake!For i = 0 To fileCount   Response.Write "<TR class=normal>" & vbNewLine    For j = 0 To UBound( theFiles(i) )        Response.Write "    <TD><a href= """ & path & "/" & fname & """ >"& theFiles(i)(j) & "</a></TD>" & vbNewLine    Next    Response.Write "</TR>" & vbNewLineNextEnd Function%>

about the filters, the user should be able to filter the results on every item that appears. i'm a little stuck with this, it's in these time that i realise how little i know about ASP =/thanks everyone,ember

Link to comment
Share on other sites

This forum software has a habit of doing strange things to code. This is the part you need to edit to filter:

	For j = 0 To UBound( theFiles(i) )		Response.Write "	<TD><a href= """ & path & "/" & fname & """ >"& theFiles(i)(j) & "</a></TD>" & vbNewLine	Next

That loop displays the files. So, you need to include IF statements there to test if you want to display the file or not based on whatever conditions you want to filter.

Link to comment
Share on other sites

i see what you mean, but in that case the user, when loading the page, will get the results already filtered, is that correct? because what i need is that the users are allowed to filter the results after they're displayed.

Link to comment
Share on other sites

In that case, you're going to have to restructure a lot of the page. You will need to set up a large javascript function to do the filtering. Instead of each file being in a table row, each file will need to be inside a div with an ID on it. The javascript function will need to have a large data structure like an array or a generic object that will store information about each file, such as the div ID that the file is in, and each piece of information that you would need to filter on, such as name, size, type, etc. Different links on the page would have to execute the javascript function and tell it what to filter on, and the function would have to loop through the file data structure and show or hide each div that contains a file.It's fairly complicated, but if you want it done client-side there's not really an easier way. Also, in order to see new files, the page would have to be reloaded. If you wanted it to pick up new files without a reload, then you're talking about an Ajax script to send a request to the server for the list of files.

Link to comment
Share on other sites

Well, not really. You will want to use the Object object to hold information about your files though:http://devguru.com/technologies/javascript/10762.aspFor whatever reason, w3schools does not have a reference for that. It is a generic object, and is the basis for all other objects.You will need to have PHP create javascript code that looks something like this:

files = new Array();new_file = new Object();new_file.filename = "filename outputted by PHP";new_file.filesize = "size outputted by PHP";...files.push(new_file); // add the file to the arraynew_file = new Object(); // the next file...

This will set up all of the files in a javascript data structure (in this case, an array). You will need to write an additional function to do the actual filtering, so that when they click on a link to run the javascript function, the function will loop through the list of files and will show or hide each one based on the filter condition.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...