Where'd They Come From?: Recording Referrals in ASP - What's Involved? (Page 2 of 4 )
To be able to understand what is going to be happening in this tutorial, you will need to have a basic grasp of ASP, but since this is an ASP tutorial, I will take for granted that you have some level of ASP knowledge. That, really, is all that is required, other than a database program; I used MS Access, on Windows XP. A simple text editor like notepad will suffice for the coding, that is all I have ever used, and I’ve done just fine.
Lets Begin!
Generally I like to begin my projects with a template or at least have each page already created, even if they are empty. So we will create our files now, in the same directory as your index.asp file (or equivalent) create an ASP page name referrals.asp, another ASP page named ref.asp, and a database named ref.mdb, we are now ready to begin.
First we will play about with our database and set names. Create a new table in the database and name it reftable, in this table we require columns named ‘Ref_ID’ (this should be your key) this should have the Data Type of AutoNumber, ‘URL’ with Data Type of Memo, and finally ‘Counter’ with Data Type of Number.
To begin with, we must set a control URL, so that we don’t end up with an error when trying to run the code for the first time (we will get an error stating that the database is empty, this can be avoided by adding a little extra code, but I find this method faster and easier). Simply enter “Control” in the URL column, and 0 in the counter column. The database is now set, we should not need to touch this again.
The Coding
Open the referrals.asp page we created earlier; this is the page that is actually displayed to the user (or Administrator, whoever you allow access) so make it look all pretty if you wish, but for now lets just concentrate on making it function properly.
In the body of your file, create a simple table like the one below that I use:
The above creates the table and appropriate headings; Referring URL and Count. Below we will display the details from the database, first we need to connect to the database...
This step is a little more advanced so we will make it a little easier by making it a DSNless connection (this saves us time required to make a DSN connection, then coding that to look at the correct database, then getting our code right to use this DSN connection...too much effort I believe, for the same end result!)
Any of you who have dabbled in ASP would have come across a database connection before, so we will fly through the connection, and not worry too much on what is involved.
We connect to the database using this DSNless connection:
We select all the rows ordered by the counter with the highest count first. (If you want the lowest count first, just replace DESC with ASC.)
SQL_query = "SELECT * FROM RefTable ORDER BY Counter DESC" Set RS = MyConn.Execute(SQL_query)
Then we loop through every row and write it out:
WHILE NOT RS.EOF
The next little bit of code lets us get different background color for every other row, just to make it easier to see what count is with what URL, and also to make it more appealing to the eye:
IF BGColor = "#F7F7E7" THEN BGColor = "#E7E7D6" ELSE BGColor = "#F7F7E7"   END IF Db_URL = RS("URL") %>
Now we write the details from the database, one row at a time to the table.
The below should be fairly familiar to you, it is simply the end of a while loop, it’ll keep writing the above code until there are no more URLs left in the database.
<% RS.MoveNext WEND RS.Close Set RS = nothing MyConn.Close Set MyConn = nothing %>