Database Front End: JavaScript arrays | WebReference

Database Front End: JavaScript arrays

Logo

Database Front End:
using JavaScript multi-dimensional arrays for record storage


The simplest way to store field/record information for quick retrieval is in a JavaScript array where each array element is in turn an array itself.

In database lingo:

We will create a database with 5 records. Each record will have 6 fields, corresponding to the six text fields in our example:

    arRecord = new Array()
    arRecord[1] = new Array()
    arRecord[1][1] = "CYNIC-001"                 // Sampling Code Number
    arRecord[1][2] = "F67-94-4/17"               // Museum Catalog Number
    arRecord[1][3] = "Filia"                     // Provenance (location)
    arRecord[1][4] = "Neolithic II / 5000-3750"  // Period-Chronology
    arRecord[1][5] = "Dark on Light"             // Style-Typology
    arRecord[1][6] = "open-shape"                // Shape
    arRecord[2] = new Array()
    arRecord[2][1] = "CYNIC-002"
    arRecord[2][2] = "F68/78"
    arRecord[2][3] = "Philia"
    arRecord[2][4] = "Neolithic II / 5000-3750"
    arRecord[2][5] = "Dark on Light"
    arRecord[2][6] = "open-shape"
    arRecord[3] = new Array()
    arRecord[3][1] = "CYNIC-003"
    arRecord[3][2] = "KM87/1495"
    arRecord[3][3] = "Ambelikou-Aghios Georghios"
    arRecord[3][4] = "Late Chalcolithic / 2800-2300"
    arRecord[3][5] = "Red on White"
    arRecord[3][6] = "stand"
    arRecord[4] = new Array()
    arRecord[4][1] = "CYNIC-004"
    arRecord[4][2] = "C416"
    arRecord[4][3] = "Unknown"
    arRecord[4][4] = "Cypro-Classical II / 400-325"
    arRecord[4][5] = "Bichrome VII"
    arRecord[4][6] = "rhyton"
    arRecord[5] = new Array()
    arRecord[5][1] = "CYNIC-005"
    arRecord[5][2] = "1935 C38/C65/B4"
    arRecord[5][3] = "Unknown"
    arRecord[5][4] = "Cypro-Archaic I / 700-600"
    arRecord[5][5] = "Bichrome"
    arRecord[5][6] = "figurine"

We did not reference the first element of the array, namely arRecord[0]. We started our definitions with the second element, arRecord[1], because it will make life easier later. It is more natural to refer to the first of anything as one, and not as zero.

The Database as a Separate File

It is best to keep our database JavaScript separate, in its own external file. In that way, we can modify the fields, add new records, without intrusion into the actual HTML page. As we shall see further on, our display functions will adapt automatically to any number of records. Also, the same database can be used by other pages which may display the records differently.

So, let's think of a unique name for our database file, like...uh...records.js and place the appropriate HTML early in our <HEAD>:

<SCRIPT LANGUAGE="JavaScript" SRC="records.js"></SCRIPT>

Now, we can insert the appropriate HTML.


Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: 09/09/97
Revised: 09/28/97

URL: http://www.webreference.com/dhtml/column4/arData.html