Forum Moderators: open

Message Too Old, No Replies

How do i increment through the records in a table?

pullling my hair out with programming.

         

chompy

1:34 pm on Apr 21, 2003 (gmt 0)

10+ Year Member



Quick before i go bald can someone help me out? I'll sweep the floor or cook or do the dishes anything within reason!
I admit I can't get to grips with programming.

I want to increment throught a record set to call a new piece of data every 24 hrs from the db, which is to be added to my default page.

I decided on caching the default.asp page. I set the page cache for 24hrs and then when the page is next called after this time it is re-cached using the latest record. To do this i think a variable must be incremented i chose 'x' this is incremented to index through the recordset for the next record.

Can i simply add x = x+1 when the cache is re done and use this variable in the recordset? If so I am not sure where to place this x= x+1. At the moment I intialise X = 1 but can't figure out where it should be incremented. Places I tried didn't work is this because it can't be done or because i'm clueless. Obviously there is more to this i.e. when x exceeds the number of records but I figured i'd work that problem out next.

code below

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%const nCacheMins = 0
call subCacheCheck(nCacheMins)
x = 1
'
' This enables automatic time-based per-page caching.

sub subCacheCheck(nMins)

' if nocache param is set to 1, this code is all skipped!
if request("nocache") = "1" then exit sub
' do we have a cached version of this page less than (x) minutes old?
if fnCacheFresh(nMins) then
' yes ... redirect user to our cached HTML and terminate execution
Server.Transfer(fnCacheFilename)
else
' no ... cache the current HTML to a file, and continue execution
call subCachePage()

end if
end sub

'
' retrieves value of current URL
' "/default.asp?id=3&move=up"
'
function fnCacheURL
dim sQueryString
sQueryString = Request.ServerVariables("query_string")
' get rid of our internal params!
sQueryString = replace(sQueryString, "&nocache=1", "")
sQueryString = replace(sQueryString, "&recache=1", "")
sQueryString = replace(sQueryString, "nocache=1", "")
sQueryString = replace(sQueryString, "recache=1", "")
if sQueryString = "" then
fnCacheUrl = Request.ServerVariables("URL")
else
fnCacheUrl = Request.ServerVariables("URL") & "?" & sQueryString
end if
end function

'
' determine if page we are about to display has
' cached version created in last nMinutes
'
function fnCacheFresh(nMinutes)
dim dLastUpdated
' retrieve app var for current URL
dLastUpdated = Application(fnCacheURL)
' if there is no date, or user has requested recache, cache is invalid
if dLastUpdated = "" or request("recache") = "1" then
fnCacheFresh = false

else

fnCacheFresh = (DateDiff("n", dLastUpdated, Now()) < nMinutes)

end if
end function

'
' grab HTTP:// data from specified URL into a giant string
'
function fnCacheGetHTML(sURL)
dim oXML
set oXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
on error resume next
oXML.Open "GET", sURL, False
oXML.Send
fnCacheGetHTML = oXML.responsetext
end function

'
' turn current URL into a relatively safe filename
' input: "/default.asp?id=3&move=up"
' output: "cache__default_asp_id=3_move=up.htm"
'
function fnCacheFilename()
dim sTemp
sTemp = fnCacheURL
sTemp = Replace(sTemp, ".", "")
sTemp = Replace(sTemp, "/", "")
sTemp = Replace(sTemp, "&", "")
sTemp = Replace(sTemp, "?", "")
sTemp = Replace(sTemp,"=", "")

sTemp = sTemp & ".htm"
fnCacheFilename = sTemp
End Function

'
' write provided HTML to cache file
'
function fnCacheWriteFile(sText)
if sText = "" then exit function
on error resume next
dim objFSO, objFile
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(Server.MapPath(fnCacheFilename), 2, True, 0) ' write-only, overwrite, textfile
objFile.Write sText
objFile.Close
fnCacheWriteFile = (err = 0)

end function

'
' cache current page HTML to file
'
sub subCachePage()
dim sURL
dim sHTML
dim sCurrentURL

sCurrentURL = fnCacheURL

' Be SURE to include the nocache=1 param, or suffer the wrath of the infinite recursion gods!
sURL = "http://" & Request.ServerVariables("SERVER_NAME") & ":" & _
Request.ServerVariables("SERVER_PORT") & _
sCurrentURL
if instr(sURL, "?") > 0 then
sURL = sURL & "&nocache=1"
else
sURL = sURL & "?nocache=1"
end if

' loopback and rip the HTML for the current page
sHTML = fnCacheGetHTML(sURL)

' write ripped HTML to file
if fnCacheWriteFile(sHTML) then
' mark page cache as valid as of now
Application.Lock
Application(sCurrentURL) = Now()
else
' mark page cache invalid as of now
Application.Lock
Application(sCurrentURL) = ""

end if
Application.Unlock

end sub

%>

<!--#include file="Connections/widget.asp" -->
<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = x
If (Application("x") <> "") Then
Recordset1__MMColParam = Application("x")
End If
%>
<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_widget_STRING
Recordset1.Source = "SELECT * FROM [daily update] WHERE fac_counter = " + Replace(Recordset1__MMColParam, "'", "''") + ""
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

sullen

1:55 pm on Apr 21, 2003 (gmt 0)

10+ Year Member



Blimey!

I'm afraid I don't have time to look at your code very carefully, but it does look very much like you are using a sledgehammer to crack a nut.

Why bother with all the writing to file? Why not simply have an index.asp which updates every 24 hours?

Anyway I would use either an application variable which gets incremented automatically by global asa (or incremented in the function where you create the new page), or simply use the day of the month (so it'll be 1 greater every day)

So, given your existing code, you could just use:

Recordset1__MMColParam = day(Now)

and hey presto! rotating pages

yeah, well, if you don't want it to revert back to 1 at the beginning of every month you could always have:

x = DateDiff(Now,date_you_started_this_thing)
Recordset1__MMColParam = x

[but you might want to check how to use datediff - I'm doing this off the top of my head]

chompy

4:47 pm on Apr 21, 2003 (gmt 0)

10+ Year Member



Thanks sullen,

The reason for all the code is to cache the page for 24 hrs so that any calls to the page are from the cache and not the database.

I thought this would help with speed etc. Also i wanted to utilise this code as it checks to see if the page has been updated to set a variable which i can then use to index a new record when the page is re cached.

I think i need to be looking at stored procedures but I will look at your code, thank you. My biggest problem is learning how to program which i have never been good at! so much of what i read just flies over my head :-(

Thanks for the help, I'm not looking for someone to do the programming for me but more help in going in the right direction and a hand along the way.

cheers

sullen

5:08 pm on Apr 21, 2003 (gmt 0)

10+ Year Member



Right.

As it goes, reading and writing text files is much harder work for the server. As long as you close the database connection properly, there really shouldn't be any problem with just looking up the page from the database every time. (you can set the "expires" metatag to 24 hours ahead to prevent people from hitting the database several times in one session though)

Don't use a stored procedure unless you are getting lots of traffic - if you only get 1 visitor every 5 minutes or so you will find it runs more slowly.

Seriously - I think you are asking for trouble doing it this way. Try just using an asp page and time it - I would be very surprised if you have much of a difference in speed. If there is a big difference in speed, then parhaps you should look at other hosting options.

HyperGeek

5:22 pm on Apr 21, 2003 (gmt 0)

10+ Year Member



If all you want to do is show a new page every 24 hours, you should simply use includes.

Create a directory called "/include/" and then throw in your daily pages like this: "/include/homepage_##-##-####.asp"

##-##-#### would be your date.

So now you can write code similar to this:

<%
'*** THIS IS ALL OF YOUR FRAME CODE_
'(The top part of your page, and then beginning of_
' any framework you might keep the content within.) ***
%>

<% IF DATE() = "04-21-2003" THEN %>
<!--#include virtual="/include/homepage_04-21-2003.asp" -->
<% ELSE %>
<!--#include virtual="/include/homepage_default.asp" -->
<% END IF %>

<%
'*** THIS IS ALL OF YOUR INDICIA CODE_
'(The bottom part of your page, and the end of_
' any framework you might keep the content within.) ***
%>

This is, of course, the simplest solution. It requires you to update only two things though: The IF THEN statements above (which, if you're creating a whole new page for each day, what's another single line of code to edit?) and if you don't, then it's no sweat since the page will load the default content if no other content exists... and your daily page.

I work a similar thing as above for holidays in which I have the page display a different graphic for that specific holiday. Only difference is, no dates need editing.

[edited by: HyperGeek at 5:32 pm (utc) on April 21, 2003]

HyperGeek

5:31 pm on Apr 21, 2003 (gmt 0)

10+ Year Member



Also be aware that holding values day-to-day in an Application variable might cause problems. If the server resets for any reason (and you're on a Windows-based server to run ASP, so you can guarantee it will on occassion) all Application-wide variables are reset.

You shouldn't really create app variables for anything that's more than temporary (about the length of your average user session) unless it's something that inconsequential to the overall app such "There are currently 3982 users on this web site" type of function.

Anything you want to keep as a permanent, dynamic part of your web site, you should always store it in your DB or (in your your case) as a separate page.

chompy

5:43 pm on Apr 21, 2003 (gmt 0)

10+ Year Member



hi thanks guys.

ur hum I am not sure about some of the things written because i might not have explained myself clearly so just to be sure and safe i will again if that's ok.

I have a basic page. in that page i use three recordsets from the database.
The database is a two dim array with fields: title, brief description, photo, a url and type.

1st recordset calls a record to show title description and photo this is then displayed on the page. after 24 hrs i wanted to show the next record until and so on until it reached theend of the records eg 50 records after 50 days it would then restart at the beginning.- however the number of records will increase bradually as more are added.

2nd recordset shows the last three records title only

3rd recordset shows similar to 1 but it is filtered by type and this limits it to a 1/4 of the records shown in recordset 1 so it would cycle quicker than recordset 1 through the db.

So what i was/am trying to do was/is change to a new record from the db daily. I couldn't see how to do this but I thought by limiting the call to the database(db) i could utilise the times the db was called to increment through the db.

And I thought that as I only need to access the database once why not cache the page?
I was also musing that if by caching the page I could generate a semi static .html file which, if I had pages that were prior static.html pages listed in eg Google I could maintain those same links with the dynamically generated static pages.yes /no?

However i am realising that if the static pages are generated using my code unless the .asp page is called once every day at least it will never update the static page..rght/wrong?

Anyway I hope that clarifies what I am trying to do with the db and proves that i'm in muddy water possibly drowning!

really thanks guys.

txbakers

6:27 pm on Apr 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It took the Wright brothers nearly 5 years to perfect their airplane, and that is much more complicated than writing a web page.

But the techniques for learning are exactly the same.

Don't take the big step until all the smaller steps in between are mastered. If your water is muddy, then clean it out.

Start with one recordset and test. Add something simple and test, etc. etc. That way you'll see your errors as you go, rather than having to debug an indecipherable mess of code.

markusf

12:23 am on Apr 22, 2003 (gmt 0)

10+ Year Member



Put all of your cache stuff into ram!

chompy

9:17 am on Apr 22, 2003 (gmt 0)

10+ Year Member



txbakers.

The recordsets and code I am using work fine. The data displays on the site fine.

All im trying to learn is how to change the information daily and remember where i am in the record so that each day a different one appears until the end of the records.

I have learnt and am learning every day since /i started this a week ago. But I think all of us at some point get confused and need direction?

but thanks anyway

txbakers

12:40 pm on Apr 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have learnt and am learning every day since I started this a week ago. But I think all of us at some point get confused and need direction?

A week already! Congratulations. You must be a pro by now.

When you get confused is when you need to stop and take things one item at a time. What is the basic issue you are after? Is the question clear? Are the steps clear?

Computers still read only 0s and 1s and can only do one thing at any one time. It's your job to sort out the problem into simple steps.

The key to your problem lies in what you posted above. It's there for you to discover.

chompy

2:06 pm on Apr 22, 2003 (gmt 0)

10+ Year Member



sarcasm is the lowest form of wit.

markusf

4:33 pm on Apr 22, 2003 (gmt 0)

10+ Year Member



I also suggest you take a step back and try and learn more ASP.net before you go wild and code a bunch of stuff. Because you are bound to make mistakes and you could cause yourself a great deal of pain later on

davegerard

6:00 am on May 23, 2003 (gmt 0)

10+ Year Member



If you can write to the database you might consider inserting a timestamp for the record of the day. When querying the recordset the next day you could grab anything later than that timestamp or grab the first record that has no timestamp.