📄 ch15.htm
字号:
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
For Listing 15.1, you also need to include an <TT><FONT FACE="Courier">INTER_MAIN</FONT></TT> subroutine in the application, though the subroutine itself can be empty. The subroutine is used by CGI32.BAS, but what it does doesn't matter as long as it's
there.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<HR>
<BLOCKQUOTE>
<B>Listing 15.1. Visual Basic source code for MOVIES.BAS.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Main Module--<BR>
Attribute VB_Name = "Movies"<BR>
Sub CGI_Main()<BR>
If CGI_Request_method = "GET" Then<BR>
SendError<BR>
Exit Sub<BR>
Else<BR>
SendData<BR>
Exit Sub<BR>
End If<BR>
End Sub<BR>
--<BR>
<BR>
SendData() subroutine--<BR>
Sub SendData()<BR>
' Set up some variables for use later<BR>
Dim Db As Database<BR>
Dim tmpSet As Dynaset<BR>
Dim category As String<BR>
Dim title As String<BR>
Dim director As String<BR>
Dim year As String<BR>
Dim SQLtext As String<BR>
Dim textpart() As String<BR>
Dim mycount As Integer<BR>
Dim where As String<BR>
' Uses the GetSmallField function (CGI32.BAS) to retrieve information
<BR>
category = GetSmallField("category")<BR>
title = GetSmallField("title")<BR>
director = GetSmallField("director")<BR>
year = GetSmallField("year")<BR>
<BR>
' Send back the basic header<BR>
Send ("Content-type: text/html")<BR>
Send (" ")<BR>
Send ("<title>Movie Finder</title>")<BR>
Send ("You were looking for: <p>")<BR>
' Start checking those values...<BR>
If category <> Null Then<BR>
Send ("Category: " & category & "<br>")
<BR>
mycount = mycount + 1<BR>
textpart(mycount) = "category like " & category
<BR>
End If<BR>
If title <> Null Then<BR>
Send ("Title: " & title & "<br>")
<BR>
mycount = mycount + 1<BR>
textpart(mycount) = "title like " & title<BR>
End If<BR>
If director <> Null Then<BR>
Send ("Director: " & director & "<br>")
<BR>
mycount = mycount + 1<BR>
textpart(mycount) = "director like " & director
<BR>
End If<BR>
If year <> Null Then<BR>
Send ("Year: " & year & "<br>")
<BR>
mycount = mycount + 1<BR>
textpart(mycount) = "year = " & year<BR>
End If<BR>
If mycount < 1 Then<BR>
Send ("<P>You didn't send any data!")<BR>
Exit Sub<BR>
End If<BR>
' Open the Access Database, using Db as the pointer to it<BR>
Set Db = OpenDatabase("cgi-win\movies.mdb", False, True)
<BR>
' Join the array of search criteria together to form a single
condition<BR>
For a = 1 To mycount<BR>
If a = mycount Then<BR>
where = where & textpart(mycount)<BR>
Else<BR>
where = where & textpart(mycount) & " AND "
<BR>
End If<BR>
Next a<BR>
' Create one big SQL Query<BR>
SQLtext = "Select * from MovieList where " & where
<BR>
' Go out and search the database, storing the results temporarily
in tmpSet<BR>
Set tmpSet = Db.CreateDynaset(SQLtext)<BR>
' Check for results, and act on them accordingly<BR>
If tmpSet.RecordCount = 0 Then<BR>
Send ("No such luck, we don't have what you want.<p>")
<BR>
Else<BR>
Send ("Here are the movies which matched your search:<br>")
<BR>
Do While Not tmpSet.EOF<BR>
Send ("Title: " & tmpSet("title"))<BR>
Send ("Category: " & tmpSet("category"))
<BR>
Send ("Year: " & tmpSet("year"))<BR>
Send ("Director: " & tmpSet("director"))
<BR>
Send ("Cost: " & tmpSet("cost"))<BR>
Send ("Summary: " & tmpSet("summary"))
<BR>
Send ("Starring: " & tmpSet("starring"))
<BR>
Send ("------------------------------ <br>")<BR>
tmpSet.MoveNext<BR>
Loop<BR>
End If<BR>
End Sub<BR>
<BR>
SendError Subroutine----<BR>
<BR>
Sub SendError()<BR>
Send ("Content-type: text/html")<BR>
Send (" ")<BR>
Send ("<title>Sorry..</title>")<BR>
Send ("This program only accepts the ")<BR>
Send ("POST method")<BR>
Send ("<br> Please use the 'Back' button ")<BR>
Send ("to go to the form, and make sure ")<BR>
Send ("it uses the POST method")<BR>
<BR>
End Sub</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you're interested in the details of CGI32.BAS, open it up in a text editor or in Visual Basic, and you can see what happens in each of the possible calls. It's very well commented, so you can see the underpinnings such as the <TT><FONT
FACE="Courier">GetPrivateProfileString</FONT></TT> calls, which are doing the bulk of the real work for you.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Every time one of the search fields contains some information,
a little portion of an eventual SQL query is formed and stored
in part of an array (named <TT><FONT FACE="Courier">textpart</FONT></TT>
in this example). At the end, this array is checked and joined
together into one long string of data appropriate for a query
condition. The whole SQL query, including the list of conditions,
is communicated to the database, and the results come back.
<P>
If no matches are made, regrets are sent back to the users. If
results are found, though, they're looped through one by one to
generate some HTML for sending back to the users.
<H3><A NAME="UsingtheDatabase">Using the Database</A></H3>
<P>
After all your work, you should look at everything in action.
First, you need an HTML form to let users enter search data. Remember
that the users can search for four primary fields: Title, Year,
Director, or Category. You therefore need four input boxes in
which users can enter searches if they want to.
<P>
Listing 15.2 shows the HTML code used for the search form, and
Figure 15.2 shows the resulting form. As you can see, it doesn't
have to be anything fancy to get the point across, but you can
always spruce up the HTML code once the search does what you want
it to.
<P>
<A HREF="f15-2.gif" ><B>Figure 15:2:</B> <I>The Movie search form</I></A>.
<HR>
<BLOCKQUOTE>
<B>Listing 15.2. HTML front end for searching.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><title>Movie-o-Rama!</title>
<BR>
<body bgcolor=#FFFFFF><BR>
<center><BR>
<h2>Movie Database</h2><BR>
To find what you're looking for in our online catalog,<BR>
please enter some search criteria in the boxes provided<BR>
below.<BR>
<hr><BR>
<form method=POST action=/cgi-win/movies.exe><BR>
Movie Title: <input name=title> <br><BR>
Year Released: <input name=year> <br><BR>
Director: <input name=director> <br><BR>
Category: <input name=category> <br><BR>
(Available Categories: Family, Childrens, Sci-Fi, Horror, Action,
Classics)<p><BR>
<input type=submit value="Search"><BR>
</form><BR>
<hr></FONT></TT>
</BLOCKQUOTE>
<HR>
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A>
</H2>
<P>
Though the method covered in this chapter is one way of approaching
databases, you can do the same job in any number of different
ways or with different tools. The best implementation of a database
is one that meets your current needs and does so in a way that
benefits you the most. Don't aim for SQL and ODBC if all you need
is a phone book, and don't shy away from more advanced programming
when you need the power these languages give you. Dozens of database
books are available for all sorts of situations, whether you're
using a proprietary tool or something more popular, and the methodology
holds true from one to the next. If you're short on ideas, go
to a search engine and look for any of the following words: <I>catalog</I>,
<I>database</I>, <I>Delphi</I>, <I>Visual Basic</I>, or <I>search</I>.
These words will take you to a variety of resources, and from
there you can branch out to whatever destinations suit your interests.
<P>
Here's one last thought: Databases don't just have to be the traditional
"What's your question?" type. You can create a database
that contains entire HTML pages and serve them up to users dynamically.
Make use of the <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>
environment variable by embedding data in your navigation links
as follows:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><a href=/cgi-win/makepage.exe?newstuff></FONT></TT>
</BLOCKQUOTE>
<P>
You can then evaluate where people are calling from, along with
a host of other information, and serve these users the freshest
information in town. The limits are only the boundaries of your
imagination-everything on the Internet is data.
<P>
<HR WIDTH="100%"></P>
<CENTER><P><A HREF="ch14.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch16.htm"><IMG
SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>
<P>
<HR WIDTH="100%"></P>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -