![]() |
|
|
* This project touches on the Application Object, database access, JavaScript, and disk file access.
First, let's list the goals of this project:
2) The tip displayed should automatically change each day. 3) The tips should be stored in a database, so that people can submit tips from the Web. 4) The tip list should be easily resettable, so as to allow the list to be recycled when there are no new entries. 5) A complete list of tips to date should be available. Now we'll dig into some of the details. We're only going to touch specific details here. Other information about what's happening is available by looking at the actual files supplied. We need to know what tip is currently being displayed. In order to make transferring the current tip ID number, we'll store it in an Application variable. Also, in order to know when we need to change the tip, we will also store the date that the current tip ID number was allocated. Knowing that servers are occasionally rebooted, have power failures, etc, we need to store these values on the disk somewhere. I've decided to use the file TIPSTORE.DAT for this purpose. It contains two lines, one is the tip ID number and the other is the date. Since these values are to be kept in Application memory all of the time, we should restore them from TIPSTORE.DAT when the application starts up. A good place to do this is in GLOBAL.ASA, in the Application_OnStart procedure. The provided GLOBAL.ASA file restores these values. This is done, courtesy of the FileSystemObject and its support through the TextStreamObject methods: Note that we have opted to utilize Server.MapPath to tell us the physical path of the file, rather than hard coding it. This allows the same code to be used on multiple sites with no changes to the scripts that have to process the DAT file. FilSys is created as a FileSystemObject, and TipFil is created as a TextStreamObject. We then read the data from the file and close it when we're done. Next, we should attend to the page that updates the currently selected tip. This is also probably the place where we would give the users the option of displaying the tip. I would suggest putting this on your home page, but it really could be anywhere. So let's look at this feature in HOME.ASP:
So to this end, we check to see if the current date is more recent than the date we last set the tip. If were are the first person do visit this page on the new date, we become the lucky one to process the change. Locking the application, so we don't get double changes, we select the next tip ID number and store the current date so we'll have to wait for tomorrow. In keeping with the restart problem, this is also where we store the values into the DAT file. In order to popup a browser window, we need a little help from JavaScript. Here is the function we will be using to do this:
As you can see, we could select any URL we desire for this window to display. We also have control of the height and width of the window. There are some limitations to these sizes, and the location the window is placed, so check the Netscape JavaScript and MicroSoft Jscript references for the rules attendant. I've also pre-set some parameters as would be appropriate for a window such as that we want to create. Onto the creation of the window on demand. This is achieved by using a 'javascript:' URL:
The 'javascript:' protocol method tells the browser that this is really a command to be interpreted by the JavaScript engine, instead of being processed by the Web Server. So we suggest to the browser that it process our function with POPUP.ASP as our URL, and a size of 300 by 300 pixels. Perhaps we should look at POPUP.ASP at this time:
Were we want to display information from the record we use the construct:
By this method, we read the field 'TipTitle' from the current record and return it to the browser. When we are done displaying data from the record, we close it, and release the objects as appropriate: Included you will also find two additional ASP files. RESETTIPS.ASP forces the tip ID numbers to be reset to the beginning of the list. The other is TIPLIST.ASP, which displays all of the tips up to and including the current one in a pretty formatted list. Happy Programming!
Attachments:
Bart Silverstein has written several great articles for 4GuysFromRolla.com. When he is not writing articles, Bart is often found on the many ASP messageboards, answering questions.
|
||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||