Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Order based on count in a txt file 1

Status
Not open for further replies.

gwillr

IS-IT--Management
Nov 4, 2003
267
CA
I have a list of images with link tags around them, that when clicked to view the fullsize, some code imcrements a hit count stored in an individual txt file. (img001.jpg would store its count in 001.txt)

I want to have the images listed order based on their respective counter file, from highest to lowest.

I can not get my head around how this can be done.

I am not looking for the code to be written for me, but just for some ideas on how to best accomplish this.

Thanks in advance.

Gary

 
Shoulda read the WHOLE set of comments. It appears that it still does work.

Lee
 
hmm, thats a clever script, cheers to you!

not quite sure it will work for my purpose though.

I need something that looks trough the list of textfiles, figures out which one holds the highest count, then write the code to show them all in order from highest to lowest. goal is to list the more popular thumbnails at the top.

Gary

 
You originally wrote that you wanted to store a hit count of the photos in text files. The script I referred you to can be modified to do that before delivering the graphic. Then when you load the page you can read the files and put the numbers into an array to figure out which graphics to display as most popular. Once you have the script to increment the hit counters, you shouldn't have any problem reading the data in those files and using that. You can either get a directory listing of the .txt files with the script, or keep the names hardcoded in an array, and check those files for the numbers they contain.

Lee
 
Thanks for the response. I already have em couting away, it is just the reading of the txt files, sorting, and listing that I am having trouble getting my hear around. sorry for being unclear, some of the concepts of asp are still a bit foggy, and perhaps this may be over my head.


Gary

 
If the images are named numerically, then save the hit count data in an array when the page loads, sort it out into another array by which has the highest hit count, using the number of the picture. If the images aren't named numerically, like your example, then hardcode a list of the photo names in an array and compare to that for the hit counts.

For example, if you're displaying 10 photos on a page, use something like this:

Code:
Dim displayphotos(10)
Dim photonames

'since you're using one based counting rather than zero based, I made the zeroth element blank
photonames=Array("", "img001", "imgnewpic", "imgoldpic")

Use a loop to find the highest hit count, element #1 would be the hit count for photonames(1), element #2 would be the hitcount for photonames(2), etc.

Code:
'if imgnewpic.jpg has highest hit count
displayphotos(1) = 2

or

Code:
'if imgnewpic.jpg has highest hit count
displayphotos(1) = "imgnewpic"

Then loop through the displayphotos() array and display the corresponding photo from the information stored there.

Lee
 
This makes perfect sense. Yes, the pics are numbered: 01.jpg, 02.jpg...., and they correspond with numbered txt files 01.txt, 02.txt.....

I am not sure how to get the counts from the files in to the array?

Gary

 
Open each file consecutively and read them in with the FileSystemObject. If you store the numbers as binary, you can convert to digits, but most likely you're just storing the digits themselves.

Lee
 
Thank you for the great help. I have managed to get the code written so that it retrieves the valies from the txt files, and saves them in an array. (see code below so far..is there a better way to write that?)

How can i arrange/sort the counts now so that they are in ascending numerical order?

Code:
<%
dim a,b,c,d
Set FS = Server.CreateObject("Scripting.FileSystemObject")
Set RS = FS.OpenTextFile(Server.MapPath("/devroot/photosite/tempg/counts") & "\01.txt",1)
While not rs.AtEndOfStream
a=RS.Readline
Response.Write("The text in the file is: " & a & "<br />")

Wend 

Set FS = Server.CreateObject("Scripting.FileSystemObject")
Set RS = FS.OpenTextFile(Server.MapPath("/devroot/photosite/tempg/counts") & "\02.txt",1)
While not rs.AtEndOfStream
b=RS.Readline
Response.Write("The text in the file is: " & b& "<br />")

Wend 

Set FS = Server.CreateObject("Scripting.FileSystemObject")
Set RS = FS.OpenTextFile(Server.MapPath("/devroot/photosite/tempg/counts") & "\03.txt",1)
While not rs.AtEndOfStream
c=RS.Readline
Response.Write("The text in the file is: " & c& "<br />")

Wend 

Set FS = Server.CreateObject("Scripting.FileSystemObject")
Set RS = FS.OpenTextFile(Server.MapPath("/devroot/photosite/tempg/counts") & "\04.txt",1)
While not rs.AtEndOfStream
d=RS.Readline
Response.Write("The text in the file is: " & d& "<br />")
response.write(a*b+c+d/b+c & "<br /><br /><br /><br />")

Wend 

Dim numArray(5),gg
numArray(0) = ""
numArray(1) = a
numArray(2) = b
numArray(3) = c
numArray(4) = d

For gg = 1 to 5
      response.write(numarray(gg) & "<br />")
      
next


%>

Gary

 
PS - excuse some of the response.write lines in there, they are just in there to test whether the retrieval was successful, and they will be removed later.

Gary

 
Here's a quick and dirty method to sort through the array to display the images with the top hitcounts. I wrote it to be flexible, and the only numbers you actually need to hardcode in are the number of images, for the array length, and the number of images you want to display.

Code:
Dim numArray(5)
Dim counter, imgindex, hitcount, FS, RS, filename, imgcounter, maximages

Set FS = Server.CreateObject("Scripting.FileSystemObject")

For counter = 1 to UBound(numArray)
  filename = counter  & ".txt"
  If counter < 10 Then filename = "0" & filename
  Set RS = FS.OpenTextFile(Server.MapPath("/devroot/photosite/tempg/counts") & "\" & filename,1)
  numarray(counter)=RS.Readline
Next

maximages = thenumberofimagesyouwishtodisplay

For imgcounter = 1 to maximages
  hitcount = -1
  For counter = 1 to UBound(numArray)
    If numArray(counter) > hitcount Then
      hitcount = numArray(counter)
      imgindex = counter
      'reset this so you don't count it again
      numArray(counter) = -1
    End If  
  Next
  If imgindex < 10 Then imgindex = "0" & imgindex
  
  Response.Write "<img src=""devroot/photosite/tempg/images/img" & imgindex & ".jpg""><br>"
  Response.Write "the other stuff you want to display<br>"
Next

You should be able to adapt this to what you want.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top