Searching Part 1
Senior Editor, TheScripts.com
In that sea of if statements, we come upon the part that states,
elsif ($form{'action'} eq "search") {
&header("Search For Something");
&search_form("search_two");
&footer;
}
elsif ($form{'action'} eq "search_two") {
&search_two;
}
Two actions for two different parts of the search functions for the script. We will check out the first elsif block here;
&header("Search For Something");
You should know this one by now... just the page header and the page title.
&search_form("search_two");
Here should be something new to you. The search_form sub routine creates the search form you search with. The search_two part specifies the value for a hidden input field, which will be explained below. The sub actually looks like this......
--
sub search_form {
$action_val = shift;
This specifies what the value of the hidden field, 'action', will be. This field controls the action of the script, and is the information fed into the sea of if statements. at the top.
$text = shift;
If there is specific text going to be printed out in the headline part of this search form, it is sent into this sub when it is called. Otherwise, nothing will be printed.
my ($check) = "";
The variable $check is now made local to this sub routine, and has no value outside of it. It is also set to "".
print qq~
<FORM METHOD=post>
<INPUT TYPE="hidden" NAME="action" VALUE="$action_val">
Here is where that $action_val variable is put to use.
<TABLE border=1 bgcolor="#FFFFFF" cellspacing=0 cellpadding=4>
<TR bgcolor="#C0C0C0">
<TD colspan=2><CENTER><font size=-1> Search For a
Record $text </CENTER></TD>
</TR>
.... and here is the $text variable, if it even has a value.....
<TR bgcolor="#DDDDDD">
<TD><font size=-1>Search Term </TD>
<TD><font size=-1>
zz<INPUT TYPE="text" NAME="search_term" size=20>
Type <INPUT TYPE="radio" NAME="type" value="phrase">
Phrase <INPUT TYPE="radio" NAME="type" value="keywords" CHECKED>
Keywords</TD>
</TR>
This is where your search term is specified in the input field 'search_term', and also where you decide wheter your search term is a single phrase or a group of keywords. The actual work for this is done in the sub procedure &search.
<TR bgcolor="#DDDDDD">
<TD><font size=-1>
Boolean Connector</TD>
<TD><font size=-1> <INPUT TYPE="radio" NAME="boolean"
value="and" CHECKED>AND
<INPUT TYPE="radio" NAME="boolean" value="or">OR
</TD></TR>
Just like above, the work for this is done in &search, but this is whwere you specify whether you are connecting your search keywords with the AND connector, or the OR connector.
<TR bgcolor="#DDDDDD">
<TD><font size=-1>Search Fields</TD>
<TD>
<font size=-1>All: <INPUT TYPE="radio" NAME="field"
value="everything" CHECKED>~;
foreach $field (@db_fields) {
if ($field eq "ID") { next; }
print qq~ $db_name{$field}<INPUT TYPE="radio"
NAME="field" value="$field">
~;
}
Here all of the search fields are displayed as checkboxes, and you can specify what fields you want to search in. The default one is 'everything', which searches all fields. Let me explain the foreach $field (@db_fields) { line.... as it is used again below.
Remember at the top of this script in the section with foreach $field (sort { $fields{$a}[0] <=> $fields{$b}[0] }, we pushed the names of each of the %fields hash into this array. This re-iterates over this array, spitting out the name and value into each checkbox field.
print qq~</TD></TR>
<TR bgcolor="#DDDDDD">
<TD><font size=-1>Sort By</TD>
<TD>~;
foreach $field (@db_fields) {
if ($field eq $db_key) { $val = " CHECKED"; } else { $val = "" };
print qq~<font size=-1> $db_name{$field}<INPUT TYPE="radio"
NAME="sort_field"
value="$field"$val>
~;
}
Just like above, the foreach loop here re-iterates over this array, except this time it is used for sorting. The $val jargon right above print qq~ ~; just defaults that the $db_key checkbox is checked from the start.
print qq~</TD></TR></TABLE>~;
print qq~<CENTER><INPUT TYPE="submit" value="Search"></CENTER></FORM>~;
}
# end of sub
--.
&footer;
The page footer sub is called again to finish off the search_form sub.
Makes sense? Good, as that part is just really html, and we haven't even gotten to the really hard stuff yet.
On to the next part....
