Hi Guys, I have a quick question regarding a program that I'm using to search a flat database. Here is the DB string that I'm using:
0-company_name|1-street|2-city_st_zip|3-phone|4-fax|5-website|6-email
So, here's the problem...some of the database entries do not use every field, in the above string. So, for example, if the first result,or match, contains every field entry and the second result contains every entry except for the "6-email" entry the second entry will display the "6-email" entry from the first match and so on until a different "match" contains a different "6-email" entry. Here's the code that I'm using...any help would be greatly appreciated!
$datafile = "entries.dat"; @searchterms = ("company_name","street","city_st_zip","phone","fax","w +ebsite","email"); @table_fields = ("company_name","street","city_st_zip","phone","fax"," +website","email"); $limit_of_search_results = 20; $bgcolor1 = "white"; $bgcolor2 = "white"; sub table_header { print qq! <CENTER><TABLE cellspacing="0" cellpadding="2" WIDTH="80%" border=0> <TR bgcolor=#4A6B63> <TD WIDTH="100%" bgcolor=#4A6B63 colspan=3><FONT face=Arial,Helvetica" + size="3" color="White"><B><I>$form{'searchtext'}</FONT></TD> </TR> !; # end of print statement } sub table_row { if ($field[0] ne "") {$company_name="$field[0]<BR>";} if ($field[1] ne "") {$street="$field[1]<BR>";} if ($field[2] ne "") {$city_st_zip="$field[2]<BR>";} if ($field[3] ne "") {$phone="$field[3]<BR>";} if ($field[4] ne "") {$fax="FAX: $field[4]<BR>";} if ($field[5] ne "") {$website="<a href=\"http://$field[5]\" target=blank>$field[5]</a> +<BR>";} if ($field[6] ne "") {$email="<a href=\"mailto:$field[6]\">$field[6]</a><BR>";} if ($field[9] ne "") {$logo="<img src=http://www.bonneylake.com/businesses/gfx/$field[9 +]>";} if ($field[8] ne "") {$storefront="<img src=http://www.bonneylake.com/businesses/gfx/$f +ield[8]>";} if ($field[11] ne "") {$first_name="$field[11]";} print qq! <TR bgcolor=$bgcolor> <TD WIDTH="40%" valign=top><font size=-1> <B>$company_name</B> $street $city_st_zip $phone $fax $website $email </TD></TR> !; } sub table_footer { print qq! <TR bgcolor=#4A6B63> <TD><font size="1">&nbsp;</font></TD><TD><font size="1">&nbsp;</font>< +/TD><TD><font size="1">&nbsp;</font></TD> </TR> </TABLE></CENTER> !; } (!$ENV{'REQUEST_METHOD'}) && (die __FILE__.' line '.__LINE__." No Requ +est method, $!\n"); if ($ENV{'REQUEST_METHOD'} =~ /^(GET|HEAD)$/ ) { $query_string = $ENV{'QUERY_STRING'}; if(!$query_string) { $form{1}=1; } } if ($ENV{'REQUEST_METHOD'} eq 'POST') { $length = $ENV{'CONTENT_LENGTH'} if $ENV{'CONTENT_LENGTH'}; (!$length) && (die __FILE__.' line '.__LINE__." No content length +with with POST request, $!\n"); ($length > 163840) and (die __FILE__.' line '.__LINE__." Content +Length exceeded Post hard limit of 16384, $!\n"); while($length){ $query_string .= getc(*STDIN); $length--; } !$query_string && (die __FILE__.' line '.__LINE__." No standard in +put with POST request, $!\n"); } my(@pairs) = split('&',$query_string); my($key,$value); my(@pairs) = split('&',$query_string); my($key,$value); foreach (@pairs) { ($key,$value) = split('='); $key = &unescape($key); !$key and next; !$value and next; $value = &unescape($value); $form{$key} = $value; } sub unescape { my($string) = @_; $string =~ tr/+/ / $string =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge; return $string; } print "Content-type: text/html\n\n"; open (DATABASE, "$datafile") || die print "$datafile"; @data=<DATABASE>; close (DATABASE); $counter = 1; foreach $data(@data) {$search_data{$counter} = $data; $counter++;} $counter = 0; foreach $table_fields(@table_fields) {$field_placement{$table_fields} = "$counter"; $counter++;} $results_counter = 1; if ($form{'search'}) { $search_terms_present = 0; # Split keywords ######################################### @searchText = split(/\x20/,$form{'searchtext'}); foreach $test (@searchText) {if ($test =~ /\S{2,}/){ push @searchKey, $test;}} # Prepare Link ######################################### $search_query = "search=1"; $form{'searchtext'} and $search_query .= "&searchtext=$form{'searc +htext'}" and $search_terms_present++; foreach $searchterms(@searchterms) {$form{$searchterms} ne "" and $search_query .= "&$searchterms=$fo +rm{$searchterms}" and $search_terms_present++;} # Loop Through Data ######################################### foreach $key(keys %search_data) { $found = 0; @line = split (/\|/, $search_data{$key}); # Custom Searching ######################################### foreach $searchterms(@searchterms) {$form{$searchterms} and $line[$field_placement{$searchterms}] =~ +/\b$form{$searchterms}\b/i and $found++;} # Text Searching ######################################### if ($form{'searchtext'}) { foreach $test(@searchKey) {$search_data{$key} =~ /\b$test\b/i and $found++;} } # end if we have search text # Check Results ######################################### # if we find something we make a new hash relation ship called %se +arch_results $found > 0 and $found == $search_terms_present and do{ $search_results{$key} = $search_data{$key}; $result_map{$results_counter} = $key; $results_counter++; }; #end found do } #end foreach search key # Begin the page of results ############################################################## $starting_point = $form{'starting_point'}; !$starting_point and $starting_point = 1; $current_page_number = int($form{'starting_point'}/$limit_of_search_re +sults) +1; $end_point = ($current_page_number * $limit_of_search_results); if (%search_results) { &table_header; $result = $starting_point; for $result($starting_point..$end_point) { !$search_results{$result_map{$result}} and next; $last_displayed = $result; @field = split (/\|/, $search_results{$result_map{$result}}); if ($bgcount > 1) {$bgcolor=$bgcolor2;$bgcount=1;} else {$bgcolor=$bgcolor1;$bgcount=2;} &table_row; } &table_footer; } else { print "<P><ul><ul>Sorry, nothing has matched what you are searching fo +r. Please try again.</ul></ul><P>"; } $results_counter--; $results_counter > $limit_of_search_results and do{ $page_count = int($results_counter/$limit_of_search_results) +1; ($results_counter/$limit_of_search_results) == int($results_counte +r/$limit_of_search_results) and $page_count--; $next_starting_point = ($last_displayed + 1); $count =1; $starting_point = 1; until ($count > $page_count) { $count == $current_page_number and $next_results_link .= qq~<b>$co +unt</b> | ~; $count != $current_page_number and $next_results_link .= qq~<a hre +f="display.cgi?$search_query&starting_point=$starting_point">$count</ +a> | ~; $starting_point += $limit_of_search_results; $count++; } $next_starting_point > $results_counter and $next_starting_point = + 1; $next_results_link .= qq~<a href="display.cgi?$search_query&starti +ng_point=$next_starting_point">Next Page</a>~; print "<P><center>$next_results_link</center><P>"; };#end do
Thanks, LisaW

In reply to Database Search Problem by lisaw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.