
Review of
Our Database File
The
first concept we are going to learn is searching a database to show
content on a web page. To search a database, we have to first have
a database... (duh) So, here's our first database, a simple listing
of some residential real estate listings.
listings.db
Address City Zip Bedrooms Baths Sq_Ft
123 West Main Parsippany 07054 3 2.5 2000
13 Oak Street Parsippany 07054 1 1 950
48 Rock Ave Huntsville 08943 2 2 1700
836 Main St Dover 07652 4 3 2400
99 Century Blvd Rockaway 07924 2 2 1500
7 Lucky Lane Dover 07652 5 5.5 3500
432 Beverwyck Dover 07652 3 1 2200
88 Eighth Ave Parsippany 07054 2 3 880
62 Valley View Rockaway 07924 1 2 1200
Route 3 Box 52 Newton 08876 3 2.5 2200
Searching
a Database
Ok,
now that we have our initial database (also called a data table),
we want to allow our visitors to search it looking for a house to
buy. This will require two web pages. The first page allows the
site visitor to specify what they are looking for, and the second
page shows the results. This example uses an embedded search (also
called a search context). For this example, we are going to start
off with the simplest possible search, and then improve it with
a few additions.
Page
1 will be search.tpl
<HTML>
<head><title>Search for your future house</title></head>
<body><form action="results.tpl" method="post">
Show houses in: <input type="text" name="_City"><br>
<input type="submit" name="submit">
</form></body>
</HTML>
In
the above example, you should be familiar with just about everything
shown. The form action tells the webserver to ask webcatalog (by
refering to a file that ends in .tpl) for a page called results.tpl.
The input name "_City" names a variable that we will call
on the next page. The underscore is not necessary, but it helps
to keep the code of the next page understandable for you and for
WebCatalog.
Page
2 will be results.tpl
<HTML>
<head><title>Here are some choices</title></head>
<body>
Here are the properties we found that match your search...<BR>
[search db=listings.db&eqCITYdata=[_City]]
Address, City, Bedrooms, Bathrooms <BR><BR>
[founditems]
[Address], [City], [Bedrooms], [Baths]<BR>
[/founditems]
[/search]
</body>
</HTML>
OK,
this page has a lot of things to learn. Starting with the embedded
[search].
We use a [search] context
to tell webcatalog what to do. The first thing to notice about the
search context is the db=listings.db
text. This specifies what database should be searched. This also
implies that the database listings.db is in the same directory as
the results.tpl page. (Databases can be in other directories, and
can still be utilised, but this requires some path information to
tell webcatalog where to find the database; this will be covered
later.) After the database to be used is named, we need to tell
webcatalog what to search for. Before we can do that, we have to
put in an ampersand (&) to join the search criteria together.
The ampersand is required between each element of the criteria for
a search, and tells webcatalog you have another modifier for the
search.
The
next criteria tells webcatalog what records you are searching for
(called selecting in other database systems.) In this
case, we want our website visitor to be able to enter the name of
the city s/he is looking in, and we need to give that value back
to Webcatalog for it to perform the search. On our search.tpl page,
we named the input field _City,
so
we need to search the database for records that match whatever was
entered into the _City
field
against what we have stored in the database in the City field. (this
seems logical, yes?) We tell webcatalog what field to search by
using it's name, and the word data. (i.e. CITYdata)
We also have to tell Webcatalog how to compare the data we are searching
for. In this case, I used eq which stands for Equals. The search
line read outloud would say "search for data (or stored values)
in the database in which the CITY field values equal the data entered
into the form field _City." You can search for data in multiple
fields and AND or OR these searches together, we'll cover that later.
The
reason I added an underscore to the input field on the first page
(the search form) is to keep it easy to understand which City is
which. Compare what you see above to [search
db=listings.db&eqCITYdata=[CITY]]. Without
the underscore, it is hard to tell which City you are using (the
one passed into the page, or the values in the city field in the
database.)
One
final note about the [search],
it
has to have an ending tag [/search].
Leaving this tag off will give unpredictable results, and you'll
realize your error the first time you run your page.
The
[founditems]
is the next WebDNA tag to explain. [founditems]
is primarily used inside of a search. A common beginners mistake
is to use the founditems tag properly, but forget the search, or
use founditems outside of the search, which will not work.
[founditems]
is a loop with one cycle for each found item of the search. Since
[founditems]
is a
looping type of command, if we entered Parsippany into the search,
we should get three iterations of whatever code we have in between
the [founditems]
and
the [/founditems].
In this case, we choose to display the address a comma and space
then the city etc. The fields to be used from the database (displayed)
are the choosen by using their names surrounded by square brackets.
This again will only work inside of a founditems loop. Our output
of the founditems would look like this in a web browser:
123 West Main, Parsippany,3,2.5
13 Oak Street, Parsippany, 1, 1
88 Eighth Ave, Parsippany, 2, 3
Also,
I included a line of visible text between the search context and
the found items loop. This text will be shown to the browser only
once, because is is not inside the founditems loop, and thus the
output of the page would be:
Here are the properties we found that match your
search...
Address, City, Bedrooms, Bathrooms
123 West Main, Parsippany,3,2.5
13 Oak Street, Parsippany, 1, 1
88 Eighth Ave, Parsippany, 2, 3
Perhaps
not the best looking output, but it's a start...
<--
Previous Index Next
-->
HOME | Basics | Advanced | Training | Rants+Raves | Links | Feedback
This
whole page, and everything on it ©2001 Brian
B. Burton. This page and the text contained herein may not be
reproduced on any other site, or via any other means. Really, how
hard is it just to link to this site, so people get to see the latest
updates? http://www.burtons.com/webdna/ Thank You.
Site design by Clint Davis.
Thank You Clint!
|