| Previous | Table of Contents | Next |
The first step of any query that you wish to set up is the configuration of query-related form variables. In this case, because we want to do a keyword search, there are three form variables to consider. First, you need to allow users to enter their keywords to search on. Second, you should give users the option to make the search case-sensitive, and third, you should also give users the option to make the search match whole words only. The following HTML contains a sample definition for these form variables:
<INPUT TYPE = "text" NAME = "keywords"
SIZE = "40" MAXLENGTH = "40">
<P>
<INPUT TYPE=checkbox NAME="exact_match">
Exact Match Search (Whole Words Only)
<P>
<INPUT TYPE=checkbox NAME="case_sensitive">
Case Sensitive Search
<HR>
The next step is to set up the @sc_db_query_criteria so that there is one element of the array that corresponds to the keyword query. Recall that you only need one element for matching against the keywords form variable. The other two form variables are not actually compared they merely modify how string searches are performed by web_store.cgi. Thus, the first field which corresponds to the form variable name will be keywords.
The next field must contain a comma-delimited list of all the database fields that a general keyword search should be performed on. Since this is a general keyword search, you will want to match against all the text fields in the database. Thus, this field value should be 0,1,2,3,4,6. In this example, field number 5 is left out because it is an image URL that should not be searched because an image is a picture, not text. Remember, fields in Perl start counting at 0.
Finally, the operator is set to equal (=) and the data type is set to string for doing string-based keyword searches. The resulting @sc_db_query_criteria code appears below:
@sc_db_query_criteria
= ("keywords|0,1,2,3,4,6|=|string)
Another popular search can be done to let the user choose a specific category of the database to browse. In this case, although only one form variable will be used, there are many different ways to present the choice of several different categories to the user.
One possibility is that you may wish to present the user with a static list of HTML hypertext references to a page representing each product category. This would be represented with the following HTML in the front page and is discussed in Chapter 4:
<A HREF=web_store.cgi?cart_id=&product=Vowels> Vowels</A><BR> <A HREF=web_store.cgi?cart_id=&product=Letters> Letters</A><BR> <A HREF=web_store.cgi?cart_id=&product=Numbers> Numbers</A>
Another possibility is that the user may want to select from a drop-down menu of items as part of a <SELECT> tag. This option is presented below:
<SELECT NAME=product> <OPTION VALUE=Vowels>Vowels <OPTION VALUE=Letters>Letters <OPTION VALUE=Numbers>Numbers </SELECT>
If neither of these methods will be used, it can suffice to have a standard text <INPUT> tag to allow the user to enter a product name just like the keyword search that was discussed above.
Once the HTML form has been coded using the above tags, the @sc_db_query_criteria is easy to construct. The form variable name is product. There is only one database field that corresponds to the product category. In our example, the database index of the product category is 1. Finally, just like the keyword search, the data type is string and the comparison operator is equals (=). The code below shows a sample of what @sc_db_query_criteria will look like:
@sc_db_query_criteria
= (product|1|=|string);
Querying on one specific value, such as the price of the product, is similar to the previous query on the product category. First, you need to create a form input tag in order to allow the user to enter a price. This tag is demonstrated below:
<INPUT TYPE = "text" NAME = "product_price"
SIZE = "10" MAXLENGTH = "10">
Next, the @sc_db_query_criteria element for matching the product_price is created. The first field in the query criteria element is product_price. The second field is 3 because that is the index number of the price field in this database. The third field is equals (=) and the fourth field is number, since this query involves a numeric comparison. The code for the @sc_db_query_criteria follows:
@sc_db_query_criteria
= (product_price|3|=|number);
| Previous | Table of Contents | Next |