The database that we create using Notepad will consist of comma delimited values (CDV). In other words, we will use commas to separate the data fields in the file. The downside to using a CDV Notepad database is that, although you can use Internet Explorer to browse, search, and sort the database, you can only edit the database in Notepad. Of course, this is perfect if you want to put a read-only database on your website.
A Brief Introduction to Databases
There are basically two different kinds of databases; flat and relational. Flat databases consist of a table. The rows of the table are called "records". Each record contains several "fields", or to put it another way, the columns of the table are called "fields".
Each record in a database should be unique. It doesn't make any sense to have two records that are exactly the same. To make sure each record is unique, usually one field of each record is a unique ID number. That field is called the "key".
A relational database consists of several tables. One field in a record "points to" a record in another table. For example, a field named "address" in one table would point to a record in another table that actually contains the name, street, city, zip, and country fields of the address. This is called a "relation".
The value in the "address" field might, for example, be "AKNEY INDUSTRIES". Maybe AKNEY INDUSTRIES supplies you with thousands of different parts. Rather than repeating the same name, street, city, zip, and country fields in the record for each part supplied by AKNEY INDUSTRIES, the relational database has a relation to another table containing the address.
This eliminates all the repeated data sometimes found in a flat database. For example, in a flat database, if AKNEY INDUSTRIES changed their address, you would have go through the parts database and change the address in every record of every part that comes from AKNEY INDUSTRIES. Not too cool.
The database we will create for this example will be a flat database. So the purpose of this brief introduction to databases is to advise you that if you need a relational database, this will not be the optimal implementaion.
Creating the CDV Database
To create the database, we first need to determine what fields we need in each record. Below is a list of the field names and their descriptions.
|Item||A unique ID number|
|Desc||A Description of the item|
|Cost||What you paid for the item|
|ShipP||The shipping charge you paid to receive the item|
|Sale||The sales price you received for the item|
|ShipR||The amount you received to ship the item|
|ShipC||The amount you actually paid to ship the item|
The database will consist of comma delimited values (CDV). In other words, we will use commas to separate the data fields in the file. Open Notepad (in the Accessories menu) and type in the data for your CDV database exactly as shown below.
10015,Hip Hop Music,24.95,8.75,28.50,9.00,9.00
Of course, you could simply cut and paste the above test into Notepad. Then save the file with the name ebaydb.txt.
Next, we create a webpage with text boxes to display the data from one record of the database. Open a new Notepad file and enter the following html code.
Item ID:<input id="Item" type="text"><br>
Description:<input id="Desc" type="text"><br>
Cost:<input id="Cost" type="text"><br>
Shipping Cost:<input id="ShipP" type="text"><br>
Sale Price:<input id="Sale" type="text"><br>
Shipping Received:<input id="ShipR" type="text"><br>
Shipping Charged:<input id="ShipC" type="text"><br>
Again, you could simply cut and paste the above test into Notepad. Then save the file with the name ebaydb.htm in the same folder where you saved the CDV database file.
When you open the webpage in Internet Explorer, if you see seven text boxes with labels, you have coded it correctly. You will not see entries in the text boxes because you have not yet made the webpage "data aware".
Making Webpage Elements Data Aware
To make the webpage data aware, you must place a Data Source Object (DSO) on the webpage. Internet Explorer comes with a number of DSOs, including the Tabular Data Control (TDC). To add the TDC to the webpage, type the following code just below the body tag and above the first input text box.
The above code will connect the "data provider" to the web page. Next, we need to add attributes to the webpage text boxes to identify them as "data consumers". Add "datasrc" and "datafld" attributes to the code for the webpage text boxes as shown below.
Now when you open the webpage in Internet Explorer, the text boxes will be filled with entries from the first record in the database. If the text boxes are not filled, check your code for typos.
Next we will add some buttons that will let us browse through all the records of the database. In the webpage, just below the bottom text box, add the code for the buttons as shown below.
<input type="button" value=" < " onclick="Prev()">
<input type="button" value=" > " onclick="Next()">
Note that the onclick events of these buttons call the "Prev" and "Next" functions. Put the Java Script code for these two functions, as shown below, in the head section of the webpage.
alert("At First Record!");
alert("At Last Record!");
Now when you open the webpage in Internet Explorer, you should be able to use the "Prev" and "Next" buttons to step through all the records in the database.
The Repetition Agent
So far we have been binding the database fields to textboxes, which are single value consumers. We can also bind the database fields to an html table element, which is a tabular data consumer. This can be accomplished because Tabular Data Control (TDC) contains a funtion called the "Repetition Agent" that maintains synchronization between the Data Source Object (DSO) and the data consumers.
To demonstrate the operation of the repetition agent, open a new file in Notepad and type in the code shown below. Save the new webapge with the name ebaydb_table.htm in the same folder where you saved the CDV database file.
Open ebaydb_table.htm in Internet Explorer and it will display a single column listing the Item numbers from all the records in the database. The repetition agent uses the table row element as a template and repeats that template for every row supplied by the data source object. You can view this better by completeing the table row with the rest of the fields from the database, as shown below.
The webpage now displays the entire database as a table. This database has only 16 records. What happens when we use the repetition agent to display a database containing thousands of records? Obviously we need some way to limit the number of records displayed. One way to do that would be to use the "dataPageSize" attribute of the table tag as shown below.<table id="tblElements" datasrc="#ebaydb" dataPageSize=4>After setting the "dataPageSize" to 4, we can use code to add some buttons that will let us browse through all the database, except this time it will display 4 records at a time. The complete code for ebaydb_table.htm is shown below.