Searching Databases and Displaying Results
Introduction
Search criteria may be drawn from known field values, dynamic
form submissions (post arguments), path argument values and/or
from page variable values. In this step-by-step tutorial, a keyword
search using a form is described. Search results are placed at
the location of the mgiSearchDatabase tag on the page with the
format that you define in the body of the mgiSearchDatabase tag
MGI Tags
Steps
- Create a database with one or more indexed fields.
- Create a search form in a text editor.
- Save the search form.
- Create a page to display search results and open the page
in a text editor.
- Insert the mgiSearchDatabase tag and parameters.
- Save the search results page.
- FTP the search form and search results page to the web server
running MGI.
- View the search form in a browser and perform a search.
Step 1: Create a database with one or more indexed fields.
- Create a database with one or more indexed fields. A field
must be indexed in order to use the field for searching or for
ordering search results. Get
more information about Creating and Populating databases.
Step 2: Create a search form in a text editor.
- Create a keyword search form on your page. Enter a label
and a text field. Name the text field "keyword". The
name of the text field is the name of the post argument that
is created when the text field is submitted using the "post"
method. Enter a submit button with the value "Search".
Enter HTML <FORM> tags. The form action should be the name
of the search results page (which you will create in step 3 -
let's call it "results.mgi") and the form method should
be "post".
<form action="results.mgi" method="post">
<center>
<h2>Keyword Search</h2>
<p>Enter your keywords in the field below, then click the
"Search" button</p>
<p>Keywords: <input type="text" name="keyword">
<input type="submit" name="submit" value="Search"></p>
</center>
</form>
- This keyword search form will display on the page as:
Step 3: Save the search form.
- Save the search form and name it "search.mgi".
Step 4: Create a page to display search results and open
the page in a text editor.
- Create a new page to display search results named "results.mgi"
and open the search results page in a text editing program that
allows you to view and modify the HTML and code of the page.
Step 5: Insert the mgiSearchDatabase tag and parameters.
- Insert your cursor in the HTML of the search results page
where you want the search results to display. Enter the beginning
mgiSearchDatabase tag, databaseName parameter, keyFieldName parameter,
pageVariableName parameter, orderByField parameter, resultsPerPage
parameter and ending mgiSearchDatabase tag. Enter the page parameter
and resultVariableName parameter if the number of search results
may exceed the specified results per page. The page and resultVariableName
parameters are used to construct dynamic "Previous"
and "Next" buttons for browsing search results.
- In the databaseName parameter, enter the case-sensitive
name of the database that you wish to search.
- In the keyFieldName parameter, enter the name of the
case-sensitive database field name to search for the value of
the post argument that follows.
- In the pageVariableName parameter, enter the name
of the page variable that contains the search criteria for the
preceeding key field name search. In this example, a page variable
is used to add wildcards to the beginning and end of a keyword
search.
- In the orderByField parameter, enter the case-sensitive
name of the database field to use for ordering search results.
If this parameter is not included, search results are dispalyed
in the order in which they were entered in the database.
- In the resultsPerPage parameter, enter the number
of search results to display on each screen. If this parameter
is not included, 25 results are displayed per screen.
- In the page parameter, enter the number for the set
of search results to display. In this example, the page number
is dynamically entered by embedding a page variable value.
- In the resultVariableName parameter, enter the name
to prepend to all result page variables that are used to construct
dynamic "Previous" and "Next" buttons. In
this examples, all result page variables are prepended with "ProductResults"
(e.g., "ProductResults_Page).
- In the body of the mgiSearchDatabase tag, enter text, HTML,
and MGI database field placeholders to create a template for
the search results. The template is repeated for each search
result. To retrieve a database field value, use the placeholder
format of &mgiDBFieldEnterFieldNameHere; in the body of the
mgiSearchDatabase tag. Each placeholder will be replaced with
the specific information in that database field for each result.
For example, to include values from the ProductName field, you
would enter the following in the body of the mgiSearchDatabase
tag:
&mgiDBFieldProductName;
- To display the unique internal database ID for a result,
use the field name "MGIIDFIELD".
-
- Any text, HTML or MGI tags that are static (i.e., not repeated
for each result) should be kept outside of the mgiSearchDatabase
tags.
-
- In this example, visitors enter a keyword into a text field
to search the "ProductName" field of the database.
Since visitors will not enter the exact full product name as
it appears in the database, you must append their search criteria
with wildcard searches. To accomplish this, enter an mgiSet tag
named "SearchCriteria" above the mgiSearchDatabase
tag with the value of the keyword and wildcard searches on both
sides of the keyword. Use this page variable as the search criteria
in the mgiSearchDatabase tag.
- If it is possible for the number of search results to exceed
the value listed in the resultsPerPage parameter, you must construct
dynamic "Previous" and "Next" buttons that
allow visitors to browse through different sets of search results.
Below the mgiSearchDatabase tags, enter a hidden input for the
value of the previous page of results and the value of the next
page of results using the result variables (ResultVariableName_PrevPage
and ResultVariableName_NextPage). Below the hidden inputs, display
a "Previous" button if you are not currently displaying
the first set of search results and display a "Next"
button if there is more than one page of results available. In
addition, enter a hidden input that contains the original search
criteria. You must provide this information on each screen of
the search results so that mgiSearchDatabase tag can perform
a search.
-
- Above the mgiSearchDatabase tags, perform a conditional comparison
to determine which set of results should be displayed. If the
visitor selected the "Previous" button during a search,
then display the previous set of results. If the visitor selected
the "Next" button during a search, then display the
next set of results. Otherwise, display the first set of results.
Set the page value in a variable and embed that variable in the
page parameter of the mgiSearchDatabase tag.
-
- In order for the "Previous" and "Next"
buttons to function, enter HTML <FORM> tags. The action
of the form tag should be the name of the search results page
(results.mgi) and the method should be "post".
-
- The following is an example search results page including
dynamic "Previous" and "Next" buttons. An
mgiSet tag is used before the mgiSearchDatabase tag to add wildcard
values to the beginning and end of the keyword search in the
example.
<form action="results.mgi" method="post">
<mgiComment>
Determine which page of results to display
</mgiComment>
<mgiSet name="PageToDisplay" defaultValue="1">
<mgiInlineIf lhs={mgiPostArgument name="ResultsSet"}
relationship="equals" rhs="Prev"
then={mgiPostArgument name="PrevPage"}>
<mgiInlineIf lhs={mgiPostArgument name="ResultsSet"}
relationship="equals" rhs="Next"
then={mgiPostArgument name="NextPage"}>
</mgiSet>
<mgiComment>
Perform the search and display the search results
</mgiComment>
<mgiSet name="SearchCriteria">
<mgiIf lhs={mgiPostArgument name="keyword"}
relationship="isNotEmpty">
*<mgiPostArgument name="keyword">*
</mgiIf>
</mgiSet>
<center>
<h2>Search Results</h2>
<p>
<table width="500" cellspacing="0"
cellpadding="3" border="1">
<tr bgcolor="#eeeeee">
<th>ISBN</th>
<th>Title/Description</th>
<th>Price</th>
</tr>
<mgiSearchDatabase databaseName="Products"
keyFieldName="ProductName"
pageVariableName="SearchCriteria"
orderByField="ProductID" resultsPerPage="5"
page={mgiGet name="PageToDisplay"}
resultVariableName="KeywordResults">
<tr>
<td>&mgiDBFieldProductID;</td>
<td><b>&mgiDBFieldProductName;</b>
<p>&mgiDBFieldProductDescription;</td>
<td align="right">$&mgiDBFieldProductPrice;</td>
</tr>
</mgiSearchDatabase>
</table>
</p>
<mgiComment>
Pass search criteria and display
dynamic Previous and Next Buttons
</mgiComment>
<p><input type="hidden" name="keyword"
value={mgiPostArgument name="keyword"}>
<input type="hidden" name="PrevPage"
value={mgiGet name="KeywordResults_PrevPage"}>
<input type="hidden" name="NextPage"
value={mgiGet name="KeywordResults_NextPage"}>
<mgiIf lhs={mgiGet name="KeywordResults_Page"}
relationship="greaterThan" rhs="1">
<input type="submit" name="ResultsSet" value="Prev">
</mgiIf>
<mgiIf lhs={mgiGet name="KeywordResults_NextPage"}
relationship="greaterThan" rhs="0">
<input type="submit" name="ResultsSet" value="Next">
</mgiIf>
</center>
</form>
- The first screen of results using the keyword "Cook"
would display as:
- The second set of search results using the keyword "Cook"
would display as:
Step 6: Save the search results page.
- Save the changes you have made to the search results page.
Step 7: FTP the search form and search results page to the
web server running MGI.
- Upload the search form and search results page to the web
server using an FTP program.
Step 8: View the search form in a browser and perform a search.
- View the search form in a web browser. Enter a keyword in
the field and click the "Search" button. The results
matching your criteria are displayed. If more than 5 results
are found, a "Next" button is also displayed. Clicking
the "Next" button will display the next set of results.
|