Article
Get Started with Dynamic Data in GoLive
Step 5. Search and You Shall Find
You've created your database, made your site dynamic, and linked the two together. You're finished with the groundwork, so it's time for some fun!
You'll now create what is essentially a dynamic Search form that can pull up your data and display it based on keyword searches. Doing this even once will open up a new realm of ideas for your site - it means you know the basics of some cool tricks with site forms, and can display user data in interesting new ways.
A Dynamic Search Page
Make your Search page dynamic by opening the static search page you mocked up in Step 1. Open the Dynamic Bindings palette and choose the server scripting language you want (ASP, PHP, JSP) and click Convert Page. GoLive automatically renames the page's extension to match what you selected.
From here you can add one or more Content Sources (the queries, remember?) to the head section. To get content, drag a Content Source object from the Dynamic Content palette to the head section of your Search page. Fill in the Inspector fields as follows:
- Description: "Search_Results" (name it what you want, but this Content Source will hold our search results)
- Type: ADO Database Query (or MySQL Database Query if using PHP/MySQL)
- Choose the database name from the pop up menu (adobe, if using the example database)
- Choose the table name from the pop up menu (golive_test, if using the example database)
Filter the Searches
At this point, the Search_Results Content Source will return ALL records from the database table we specified. But... what if we don't want all records? You can now filter the Search_Results Content Source so it will only return records based on a user's search request. GoLive provides a very nice visual way to do this via the Content Source Inspector. To get started:
- Link the search Form Action on your Search page to the file itself (index.asp or the name you gave it)
- Make sure the Form Method is "Get", not "Post"
- Rename your text field on your search form to "lname" (or, the same name as the last name field of the sample database)
- Select "lname" from the Field Name list in the Content Source Inspector
- Choose Match request parameter 'lname' from the Match Pattern popup menu to the right in the Content Source Inspector
- Bind the table you created to display search results to the Search_Results Content Source using the Dynamic Bindings palette by:
- Selecting the table
- Clicking Replace Rows in the Dynamic Bindings palette
- Choosing Search_Results as the Content Source
- Setting it to display 5 records per page (this is for Block Navigation; more on this later)
- Bind the fields in the first row of the table to their appropriate fields in the database using the Dynamic Bindings palette. You only need to worry about the first row! The rest of the table will fill in automatically, because you checked "Replace Rows" in the Dynamic Bindings palette. You will bind the fields by selecting the mock content first name text and binding it to the first name field in the Dynamic Bindings palette. Do the same for the last name and phone number text in the first row of the table, binding them to their respective fields in the database.
What we've just done is tell our Content Source to kick back all the records whose last name field exactly matches what a user typed into your search form text field.
Step 6. Fine Tuning the Searches
Your dynamic Search form is working, but now you need to plan for all the ways a user might try to use it. For instance, what if the user calls the page directly, or enters nothing for search criteria? At this point your Search page is looking for a "request parameter", and it has to match the "lname" exactly. GoLive has given us a great starting place, and from here a few additional steps will cover the contingenes:
- Rename your text field on your search form to "keyword".
- Change the Table pop up menu to Custom SQL in the Content Source Inspector
- Select the text in the SQL edit box and delete it.
- Type or paste the text below into the SQL box:
For ASP:
select * from golive_test where lname like
'%{Request.QueryString("keyword")}%' or fname like
'%{Request.QueryString("keyword")}%' or phone like
'%{Request.QueryString("keyword")}%'For PHP:
select * from golive_test where lname like
'%{@$GLOBALS["keyword"]}%' or fname like '%{@$GLOBALS["keyword"]}%'
or phone like '%{@$GLOBALS["keyword"]}%'This snippet of source code transforms the Search_Results Content Source into a practical search engine. The above code basically says: "look at what the user typed into the search field and see if it matches anything in any of the fields of all the records. And while you're at it, return the matching records and make them available as content for this page." This is what we want! If nothing is typed into the search box, or if the page is called directly with no previous page, ALL records are returned. Anything typed into the search box will be matched to the database fields.
Note that the "select * from golive_test" statement means to select everything in the database named 'golive_test'. The 'where' statement sets you up to make some qualifications. The 'like' word specifies a partial match, so it doesn't have to be exact. The '%' symbol is a wildcard that tells it to match any number of characters. Because there are '%' symbols on both ends of the expression, the search will match every record if nothing is typed into the search box or if the page is called first with no previous page. It's kind of like asking "show me records that contain any number of characters + what the user typed + any number of characters". That sounds like word math to me! If the user typed nothing, or if the page is called first, the word math for that would be: Records = 'any number of characters' + 0 + 'any number of characters'. Hey... every record qualifies for that!
Block Navigation
Now you want to add some Block Navigation, which is a cool way to deliver users their search results. Block Navigation lets you specify how many records per page you want to display, and includes links that allow the user to browse easily through multiple pages of results.
Start by adding another blank Content Source to the header of your page. Fill out the fields as follows:
- Description: Block (again, name this anything you want, but this Content Source will provide our Block Navigation)
- Type: Navigation Block View
- Content Source: "Search_Results" (or whatever you named that first Content Source.)
- Create a regular 1 row x 2 cell table underneath the search results table - in the first cell type "Records:"
- Drag a Repeat Cells object from the Dynamic Content palette into the second cell of the 1x2 table
- Delete all but one cell of the Repeat Cells table and type "x-y" in that cell
- Select the 'x' and bind it to the "Block" Content Source with the filed set to "First"
- Select the 'y' and bind it to the "Block" Content Source with the filed set to "Last"
- Select the whole 'x-y' text and link it to your Search page
- In the Dynamic Bindings palette Click "Bind Link Destination To" and set the Field to "Link_URL"
What you now have is a page that is looking for searches and doing block navigation. When someone types in a keyword like "John," all the records that contain "John" in the fields you indicated will be displayed. It also will use the "Block" Content Source to provide those handy links that allow a user to bounce between multiple pages of results. This is Block Navigation in the form "Records: 1-5 6-10 11-13, etc." Very cool! And you thought you couldn't program!
Tip: Remember to set your posting method for your search form to "Get" as "Post" isn't compatible with Blockavigation.
At this point you can use the GoLive Preview Tab to see results, but note that your page is not live, so you really are only previewing. If you get an error message when trying to display a custom search, try previewing it in a browser. Also, GoLive 6.0.1 displays this custom code correctly in Preview mode, so ensure you've done the upgrade.