Home : Products : Links 2.0 : Customization :

Products: Links 2.0: Customization: advance search: Edit Log

Here is the list of edits for this post
advance search
hey everyone...
Im trying to setup an advanced search so that I can search particular fields. E.g. BYO? yes/no?

I'm using this in my search.cgi file:

sub search {
# --------------------------------------------------------
# This routine does the actual search of the database.
#
my ($search_terms, $bool) = @_;
my ($regexp, @values, $grand_total, $match, $andmatch, $field, $or_match, %seen, $link, $tmp);

# Save the reg expressions to avoid rebuilding.
$or_match = $bool ne 'and';
if ($or_match) {
for (0 .. $#{$search_terms}) {
next if (length ${$search_terms}[$_] < 2); # Skip single letter words.
$tmp .= "m/\Q${$search_terms}[$_]\E/io ||";
}
}
else {
for (0 .. $#{$search_terms}) {
next if (length ${$search_terms}[$_] < 2); # Skip single letter words.
$tmp .= "m/\Q${$search_terms}[$_]\E/io &&";
}
}
chop ($tmp); chop ($tmp);

# We can also search by field names.
my @field_search;
for (0 .. $#db_cols) {
exists $in{$db_cols[$_]} and (push (@field_search, $_));
}
if (!$tmp and !@field_search) { return ("Please enter one or more keywords."); }
if ($tmp) { $regexp = eval "sub { $tmp }"; $@ and &cgierr ("Can't compile reg exp: $tmp! Reason: $@");}

# Go through the database.
open (DB, "<$db_file_name") or &cgierr("error in search. unable to open database: $db_file_name. Reason: $!");
flock (DB, 1) if ($db_use_flock);
LINE: while (<DB>) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp; # Remove trailing new line.
@values = &split_decode($_);
$grand_total ;

# Check to see if the link matches.
$match = 0; $andmatch = 1;
if ($regexp) {
FIELD: foreach $field (@search_fields) {
$_ = $values[$field];
$or_match ?
($match = $match || &{$regexp}) :
($match = &{$regexp});
last FIELD if ($match);
}
}

# Check to see if the link matches any database fields. Only exact matches
# here.
if ($or_match || $match || !$regexp) {
FIELD: foreach $field (@field_search) {
if ($or_match) {
$match = $match || ($in{$db_cols[$field]} eq $values[$field]);
$match and last FIELD;
}
else {
$match = ($in{$db_cols[$field]} eq $values[$field]);
$match or last FIELD;
}
}
}
$andmatch = $andmatch && $match;

# Split Categories and sub Categories into two different variables
($subcat1,$subcat2) = split(/\//, $values[$db_category]);

# do the same for the form's hidden value (category or subcateg name)
($subcat3,$subcat4) = split(/\//, $in{'thiscat'});

# check what type of category is and define what the search criteria will be
if ($subcat4) {$subcat1 = $values[$db_category];}

if ($in{'thiscat'})
{
if ($subcat1 eq $in{'thiscat'})
{
# If we have a hit, add it in!
if (($or_match && $match) or $andmatch) {
push (@{$link_results{$values[$db_category]}}, @values);
$numhits ; # We have a match!
}
}
}

else
{
# If we have a hit, add it in!
if (($or_match && $match) or $andmatch) {
push (@{$link_results{$values[$db_category]}}, @values);
$numhits ; # We have a match!
}
}


# Check to see if the category matches.
if ($regexp and !$seen{$values[$db_category]} ) {
$match=0; $andmatch = 1;
$_ = $values[$db_category];
$or_match ?
($match = $match || &{$regexp}) :
($match = &{$regexp});
$andmatch = $andmatch && $match;

if (($or_match && $match) or $andmatch) {
$numcat ;
push (@category_results, $values[$db_category]);
}
}
}
close DB;

# Word is too common, don't try and sort it, can cause problems.
if (($numhits > 50) and (($grand_total * 0.75) < $numhits)) {
return "Search term is too common.";
}

# Sort the results using build_sorthit found in db.pl.
foreach $link ( keys %link_results ) {
@{$link_results{$link}} = &build_sorthit (@{$link_results{$link}});
}
@category_results = sort @category_results;
my $elapsed = get_time();
return "ok";
}

----------------------

then, in the HTML for the search I'm using this:

<select name="BYO">
<option name="" selected>Doesn't Matter
<option name="Yes">Yes
<option name="No">No

</select>

Im using other fields similar to that one above. It works when I select a yes/no option, however as soon as I dont select an option ('it doesn't matter')("")...it returns no results, rather than skipping that criteria...

Could somebody PLEEAAASSSEEE help!
I need it so that if someone selects "it doesn't matter" then it skips that field rather than returning no results AT ALL (e.g. 0 results found)

here is the url I'm trying it for
http://www.uproared.com/directory/Eating/

thanks in advance!
lucas
email: webmaster@uproared.com

Last edited by:

Andy: May 29, 2011, 6:42 AM

Edit Log: