In This Section  |
|
|
|
|
|
|
|
|
Syndicate Your Content With ASP |
Article Index | Print | E-Mail |
Wednesday January 17, 2001
This tutorial demonstrates a simple way of implementing a tip of the day application that can be syndicated out to other sites. The method used could easily be applied to other tasks such as jokes, facts or quotes of the day.
This type of content is often used by small businesses that don't always have the resources to create regularly updated content, but want to start syndicating, as in theory you could create your database of tips in a day then sit back for the rest of the year!
The technologies used are ASP (Active Server Pages), HTML, JavaScript and a Microsoft Access database. If you are not familiar with ASP you may find this article a little difficult to understand, however there are working examples at the bottom of the page and an e-mail contact for questions.
To begin with you will need to create a Microsoft Access database. The database consists of one table called 'tips', containing three fields, tipid, tip and showdate:

The application will generate a different tip depending on what day it is, or alternatively, perhaps if you have less than 366 tips, you can generate a random tip from the database.
To achieve this we will use ASP as the backend to retrieve the necessary information from the database to be displayed.
Open a blank file and save it as track.asp. You don't need any special software to write ASP, notepad is fine.
Firstly you need to set-up a database connection string. This could also be included from another file if you prefer.
strConnect = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=yourdatabasepath.mdb"
You then need to declare the variables to be used:
DIM objConn DIM tipRec DIM today DIM tip DIM id
Next you need to request from the calling page which tip type you want, whether it be a daily tip or random one.
tiptype = request("type")
The following lines simply set-up the database connections.
Set objConn = Server.CreateObject ("ADODB.Connection") Set tipRec = Server.CreateObject("ADODB.Recordset") objConn.Open strConnect
At this point you need to generate a random number between 1 and 366. In our example we have 366 tips in the database for simplicity, but you may want to write a function to get back the max size of your database and generate the random number on that.
randomize id =int(rnd * 366)
Depending on whether the tip requested is a daily or random one, you are now ready to generate your SQL query.
If it it's a daily tip you get all records where showdate is today's date (knowing that you have only one tip for each day in the database). Otherwise you get the tip that matches the random id you generated previously.
if tiptype = "daily" then sqlText = "SELECT * FROM tips WHERE showdate = #" & CDate(Date) & "#;" elseif tiptype = "random" then sqlText = "SELECT * FROM tips WHERE tipid = "& id &";" end if
Then execute the query and fill the recordset. Set tipRec = objConn.execute(sqlText)
You just want the first record returned, even though there should only be one returned anyway. tipRec.MoveFirst
tip2 is setup as a temporary string used to hold the tip as you will need to modify it later. tip2 = tipRec("tip")
In order to create a string for the JavaScript function you may need to replace certain characters for it to work. In our example case we only need to check for apostrophes and add the escape character before each one found in the string. The line below replaces any " ' " found with " \' ". In reality there are many other special characters that could cause your JavaScript not to work, so if you are using special characters it is wise to check for any of these that you think may exist in your database.
tip = Replace(tip2, "'", "\'")
Finally you need to close your connections to the database.
Set tipRec = nothing objConn.close
Here is the piece of JavaScript that will actually write the tip to whomever requests it. This should be appended to the end of your ASP file.
function ShowTracker() { var tmpStr;
<%=tip%&> will write the ready formatted tip here and set a temporary string equal to it.
tmpStr = ('<%=tip%&>');
Then you need to actually write the tip out to the browser.
document.write(tmpStr); }
So far you have generated the tip and generated the code so you are now ready to let people add your tips to their sites. To do this you must create a few lines of JavaScript, which other people can then place on their pages to show the tip where they want. Once you have done this be sure not to move your asp file, as people will be calling it from the URL you have given to them in the following piece of JavaScript. Below is the code that someone would place on their page if they wanted your daily tips to appear on their site.
The first part includes your asp file and states whether a random tip or daily tip is required using the type field. You will have to change the URL to reflect your own site and where you have placed the script. Type is either going to be "random" or "daily".
<SCRIPT LANGUAGE="JavaScript" SRC="http://www.mysite.com/track.asp?type=random"> </SCRIPT>
These next lines just call the ShowTracker function from track.asp and write the tip to all the other peoples' sites.
<SCRIPT LANGUAGE="JavaScript"> <!-- ShowTracker(); //--> </SCRIPT>
An example (famous quotes about business and computers) is available for you to view:
http://www.purplepages.ie/examples/dailytip.html for the daily tip. http://www.purplepages.ie/examples/randomtip.html for the randon tip.
You can also view the full code of track.asp by going to the following url: http://www.purplepages.ie/examples/track.txt |
|
|
Author: © 2001 Alis Marsden, alis@purplepages.ie. |
|