|
Some Standard Include Files
Page 5 of 22 - Chapter
14
Web
page and scripting creation do not easily lend themselves to code reuse.
However, one way of accomplishing code reuse is server-side include files
whereby a file is included within a page by use of the <!--#include
file=""--> directive available with NT Server via ASP.
The
next piece of scripting we will write is an include file to create a dynamic
list of music categories available for browsing. Taking the information from the
Category table in the database, this include file creates a table of links which
when clicked takes the user to a list of goods available within that category.
Using
the server-side include directive we will incorporate this file in a number of
pages in the web site demonstrating code reuse.
The CategoryList Include File
The
first thing our ASP code does is create an ADODB Recordset to obtain a list of
categories from the database. We use the ASP Server object's CreateObject method
to create a new ADODB.Recordset object. The Recordset's Open method fetches the
data we need by executing the ListCategories stored procedure we created
earlier. sdbConnString is a global variable defined outside of the include file,
in another server-side include file that is inserted into every ASP page in the
web site, but we will come to this in good time.
Next
we have the starting tags of our HTML table. The ASP code following is the start
of our while loop which loops though the recordset rows and creates our table's
rows and cells:
HTML
with embedded ASP script comes next. Here we write the HTML of the table cell,
inserting the hyperlink which the user will click to view goods within that
music category. We append the CatId and Category Description to the end of the
hyperlink's URL. The ASP page which this link navigates to can pick up the data
from the ASP Request object's QueryString collection. Note that
Description has been coded with the escape method to ensure no confusion occurs
when it is passed as part of a URL. Characters such as the ampersand
(&) are converted into their ASCII value so they are not confused with an
ampersand which indicates another item of data in the URL.
Finally
we come to the end of the while loop and table:
Save
the include file as CategoryList.inc into the MusicMad directory you created
earlier. The .inc extension is not compulsory for include files but it is the
one recommended by Microsoft.
The Global Include File
Our
next task is to create another include file which will be added at the start of
all our ASP pages using the include directive. It contains a number of global
constants and general functions.
At
the very top of the file on a separate line by itself is an ASP preprocessing
directive. This must be the first line and it must be separate from other ASP
code or the page will fail. There are a number of ASP preprocessing directives,
some of which we will come to later, but this particular one sets the default
language for all ASP on this page to JScript:
Next
a couple of global constants used in the web site are declared. The
sdbConnString is our connection string we use in our ADO statements to connect
to the database. I have used a DSN-less connection for ease of set up (there is
no set up needed!). If you prefer, you can set up an ODBC data source which does
make it easier to change data sources without changing any code. Even if
you never change your database you may change its location to another server,
something you can easily do with an ODBC data source.
Most
of the database connection string is self-explanatory. However some needs
further explanation. The Provider is our database, here SQL Server 7. Initial
Catalog is our database. In Data Source we have used a period (full stop). This
is an alias for the local server, similar to using localhost for the local web
server. If you find this does not work, for example if your SQL Server database
is not installed or you are using a Win95/98 machine, then replace the period
with the name of the SQL Server your database is installed on.
Response.Buffer
= true buffers the sending of page content and has been used to allow us to
change cookie values once the HTTP header has been written. When the response is
buffered, the result of the ASP code (HTML and client-side script) is stored in
the server's memory until either the end of the page is reached or a
Response.Flush or Response.End methods are called. Buffering also means
that we can change a cookie in server-side ASP code even after the <HTML>
tag, something otherwise disallowed.
Next
we come to the code for the first of the two functions included with the include
file. This is part of our web site's shopping basket functionality. It extracts
cookie data for a particular stock item contained within the basket – the
ItemId, which is the same as that defined in the database, is used for
identifying items. Data held within the cookie for a particular item includes
ItemId, Qty, ArtistName, Item Description and the Price of an individual item.
It is contained in a single string which uses & to delimit data within an
item and the £ to indicate the end of the item's data.
The
getItemFromCookie functions counterpart is the setItemToCookie function. Its
parameters are ItemData, an array containing ItemId, ArtistName, item
Description and Price, and the current value of the basket cookie.
There
are three situations the routine deals with:
-
The
item already exists in the basket in which case we need to update its
quantity.
-
If
the updated quantity is zero then the item needs to be deleted from the
basket, otherwise we override the existing value.
-
If
the item is not already in the basket then we need to concatenate it to
the end of the basket cookie string.
Regular
Expressions, a powerful feature of JavaScript versions 1.2 and above, and the
string object's replace method are used to update an existing quantity. For
example, if we had an ItemId of 12, with a current Qty of 2, ArtistName
"REM", Description "Monster" and Price of 9.99 then the
cookie for that item will look like:
Our
regular expression created by the line new RegExp(lItemId +
"&[0-9]+"), will be:
This
will match any occurrences of ID12& and any number of digits (which make up
the quantity). [0-9] indicates any character in the range 0 – 9 (i.e. any
digit). The + indicates that one or more of the previous character, i.e. a
digit, must follow.
Save
this page as ServerSideGlobalDef.inc.
The Homepage
The homepage is the first
page displayed in the main frame when the user browses to the web site. It
displays the site name and lists the music categories available for browsing
using Category List.inc we created above.
<!--#include file="ServerSideGlobalDef.inc"-->
<HTML>
<HEAD></HEAD>
<BODY>
<BASEFONT SIZE=3 COLOR=#004080 FACE="Comic Sans MS,MS Sans
Serif,Arial">
<H1 ALIGN="CENTER">
<FONT COLOR=#004080 FACE="Comic
Sans MS">MusicMadOnline.com</FONT>
</H1>
<H3 ALIGN="CENTER">
<FONT COLOR=#004080 FACE="Comic
Sans MS">
Prices so low we must
be insane!!!
</FONT>
</H3>
<DIV align="center">
<FONT FACE="Comic Sans MS"
color="#FF8040">
Browse our categories below for great
bargins, <BR>
all available to purchase online with
our secure online ordering
system.
</FONT>
<P>
<!--#include file="CategoryList.inc"-->
</P>
</DIV>
</BODY>
</HTML> |
Save the file as Home.asp
|