Gossamer Forum
Home : Products : Links 2.0 : Customization :

advance search

Quote Reply
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
Quote Reply
Re: advance search In reply to
lol...is anyone going to answer this post??

thanks
lucas

Quote Reply
Re: advance search In reply to
you have to be more patient on this board since people aren't obligated to answer you and have their own Links to mess with.

try here
http://www.gossamer-threads.com/...ew=&sb=&vc=1

I'm sure this is enough to do what you want.
Especially read what Alex wrote.

Quote Reply
Re: advance search In reply to
Yeah you could always find the answer yourself as similar things to this have been discussed in the forum.

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: advance search In reply to
well please point me in the right direction...
I've already searched the forums up and down 100 times...for hours actually. i've found similar posts, and none of them have any replies...

so if you can find them, I'd be more than happy to admit you're right

By the way, your mod site (if that's yours) was one of the best one I found when I was searching...I didn't think you'd be as unfriendly as you have been.

thanks
lucas


Quote Reply
Re: advance search In reply to
i don't know who you are talking about. I'm sure its not me since Glennu has a great mods site, but neither him nor me were "unfriendly". He said that its not hard to find the answer and i already posted a link to a thread which says how to do it. Is that unfriendly?

Quote Reply
Re: advance search In reply to
Here's one for searching fields. Don't know if there is a way to search Yes/No fields.

http://www.gossamer-threads.com/...ew=&sb=&vc=1

Smile

DT

Quote Reply
Re: advance search In reply to
I didn't mean any thing unfriendly by it, just trying to encourage you guys to have a go at figuring out solutions.

Anyway here's one quick way to do it:


In search.cgi find sub search, and above:

push (@{$link_results{$values[$db_category]}}, @values);
$numhits++; # We have a match!

add:

if ($in{'advance'}){
next unless ($values[$db_isNew] eq "yes");
}




Ie. in this example you could have a checkbox in your search form called "advance" and you could give it a value, can be any value so like value="yes". In this example you could use it to only display new links in results.

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: advance search In reply to
I'm really sorry for what i said before, I never use this forum, and being confused by the layout, only saw the post saying 'try to search yourself'. I'd just spent 2 hours tryingt to figure it out, and 1 hour searching the forum for similar things. So i wasn't really looking for that response, so I appologise.

Anyway, I'm not sure that the above posts really answer what I was looking for. I've got it working, but the method I'm using is a little odd...

With the traditional tickboxes in HTML, the value was still included in the 'search string' regardless of whether or not anything was assigned to it...Using a list rather than a tickbox (http://www.uproared.com/cgi-bin/search.cgi - see advanced search) it doesn't include a value in the string when you dont select anything.

I'd much prefer to use normal tick boxes though, so i want the three options: "yes, no, doesn't matter" (for most of them). I was previously assigning no value to "doesn't matter", but this simply made the script search for no value in that field (thus returning no results). Could anyone please help, or tell me if any of the above replies would be of any help.

Additionally, is it possible to also search for multiple entries in a field? (e.g. in the Cuisine - select both Mexican and Greek by ctrl-clicking?)

And finally, sorry for my uptightness before :)

thank-you
lucas
webmaster@uproared.com

Last edited by:

Andy: May 29, 2011, 6:42 AM
Quote Reply
Re: advance search In reply to
Yeah no problem. If you want to use radio buttons to do like you mentioned you could instead use:

In search.cgi find sub search, and above:

push (@{$link_results{$values[$db_category]}}, @values);
$numhits++; # We have a match!

add:

next unless ($values[$db_isNew] eq $in{'advance'});

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: advance search In reply to
ok I'll try that thread out and see what it does, thanks :)


Quote Reply
Re: advance search In reply to
I wanted to do it for the field BYO?
or did I just stuff something up here?

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

it prevented a search at all, and returned no results no matter what (even when I manually edited the search string from my browser)

thanks
lucas

Quote Reply
Re: advance search In reply to
Change this.....

next unless ($values[$db_isNew] eq $in{'BYO'});

to......

next unless ($values[$db_byo] =~ /yes/i);

...then in links.def (you'll see where)...add.....

$db_byo = field_number_goes_here;

The way you had it, it was matching if the isNew field matched the value of $in{'BYO'} - or is that what you wanted?

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: advance search In reply to
Ok, perhaps I should re-write what I'm trying to do...
I'm trying to make a directory for restaurants in my local suburb. the database has several fields such as BYO (allows yes,no), Licensed (allows yes,no), suburb (allows Prahran, South Yarra, Toorak), etc, etc.

I want people to be able to search the directory for their perference (using drop down boxes). There are to be several drop down boxes...

For instance:
BYO? (options = dont care, yes, no)
Licensed (options = dont care, yes, no)

Now when I search for say...BYO "yes", Licensed "dont care", it returns no results. I need the search script to skip searching a field (category) if the value of the drop down box (for example) is "donesn't matter".

I know this is possible, I just dont know how to do it :(
I'm currently using the mod on my search script that is shown in the first post. If you give my instructions on lines to add, could you please include whether this is on a new "search.cgi" script, or my existing one.

thanks again
I'll get it soon :)
lucas
webmaster@uproared.com
www.prahranEATS.com

Quote Reply
Re: advance search In reply to
Paul, just tried the last idea you posted.
It didn't work... :(

I'm willing to give a US10 deposit to whoever solves this for me (I'd give more, but I'm only 16, so I dont have a HUGE income as of yet)...

email: webmaster@uproared.com

I'm going to re-post this thread later, as it's getting a little long and confusing.

thanks to all of you that have helped out so far!

Quote Reply
Re: advance search In reply to
ok just thought of a way to sum up what I need:
when someone searches for a field value, I want them to be to choose three options. Two are defined field values, and the third is a value that shows the field's data regardless of what it is!....

Sounds simple, pity I'm no programmer :(
Thanks
lucas