Display Search Results
Senior Editor, TheScripts.com
This entire sub procedure was probably not even needed, as I could have simply merged it with the next sub procedure. Since this is just a tutorial though, I figured it might be best to keep this for readability.
sub multi_match_view {
my (@results) = @_;
Here we have the contents of the array sent to mulit_match_view stored in the array @results, which is set as a my variable.
my (%rec);
The hash %rec is set as a my variable
&header($search_num." Matches Found For ". $form{'search_term'});
Since the $search_num variable in sub search_two was not set as a my variable, it is global and can be used here. So, when we call the header sub procedure, the number of results is added into the title, as well as the search term.
print qq~
<CENTER><TABLE border=1 bgcolor="#FFFFFF" cellpadding=3 cellspacing=0>
<TR bgcolor="#C0C0C0">
~;
foreach $field (@db_fields) {
print qq~<TD><CENTER><FONT size=-1>$field</CENTER></TD>~;
}
Just like sub &nomatches. Each database field is displayed here in its own <TD>.
print qq~</TR>~;
foreach $result (@results) {
%rec = &process_record(&grab_data($result));
print qq~<TR bgcolor="#DDDDDD">~;
foreach $field (@db_fields) {
print qq~<TD>
<FONT size=-1>~.&nl2br($rec{$field}).qq~</TD>~;
}
print qq~</TR>~;
}
Now, for each value in @results, we re-iterate over it and display the contents. The line %rec = &process_record(&grab_data($result)); is more of a compacted line. $result is the current value in our foreach loop, and it is being sent to &grab_data. &grab_data returns an array of values, which is then sent off to &processs_record right away, returning a hash of data into %rec. Now that %rec is set, we print a <TR>, and move to yet another foreach loop. Here we re-iterate over each value in @db_fields, which contains the names of our database fields. Each field is regarded now as $field in the loop.
So, now we starting printing again. The appropriate <TD> is made, and then we come upon this part.... &nl2br($rec{$field}). Here we stopped the normal print qq statement right before it with the ~, added the string concatenation operator '.' (adds strings to each other), and suddenly call a function called &nl2br. Those of you that use PHP know what this function probably does. In perl though, I had to make it myself. It looks like this.
sub nl2br {
#changes newlines to <br>'s
my ($tmp) = shift;
$tmp =~ s/\n/\n<br>/g;
return ($tmp);
}
No real explanation needed. It takes the string sent to it, refers to it as $tmp, does a regular expression search and replace on it, replacing all newlines with a newline then a <BR> tag. The string is then returned. Anyways, &nl2br($rec{$field}) means that $rec{$field} is being sent to it. As we just saved the record's contents in the hash %rec, and in this foreach loop all database fields are referenced to as $field, this should make total sense. After this bit, we have the '.' operator again, qq~ again, then just a closing </TD> tag.
After the foreach loop, the <TR> tag is closed.
print qq~</TABLE></CENTER>~;
&footer;
Here we just close the table and center tags, then print the footer
}
End of the sub procedure multi_match_view
Done searching!! Everything is just downhill from here now, so don't worry about it.
