Gossamer Forum
Home : Products : Links 2.0 : Customization :

Search Category mod

Quote Reply
Search Category mod
Hello,
I would like to be able to search all the records in a specified category with search.cgi. How to do this ?
If Possible with radio buttons or select menu.

Something like that:
search.cgi?query=models&Category=Photo/Fashion

or like that:
search.cgi?query=models&id_Category=121


Example of what I wanna do:
http://www.boncap.com/...emplate2_im_lili.htm (take a look at the bottom of the page)

Thanx

Teo
Quote Reply
Re: [teo] Search Category mod In reply to
I have cobbled together a mod that will put a drop-down list of categories on the search page; when one is selected, only that category is searched. A frequent problem with this type of mod has been that it only works on category pages... I have overcome that very simply, by adding a routine to search.cgi that I copied (and renamed) from nph-build.cgi. The category list is generated dynamically, not hard-coded.


1. In search.cgi, sub search, replace this:

# 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!
}


with this:

# Add hits from the prefered category.
#From this specific category:
if ($in{'thiscat'})
{
if ($values[$db_category] =~ /^$in{'thiscat'}/io)
{
# 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!
}
}
}
#From the entire directory:
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!
}
}


----

2. Add this to the bottom of the file:

sub cat_search_list {
# --------------------------------------------------------
# This routine loads the category information into memory and
# does some grouping.

my ($name, @values, @base, $base,);

open (DB, "<$db_category_name") or &cgierr("unable to open database: $db_category_name. Reason: $!");
LINE: while (<DB>) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp;
@values = &split_decode ($_);
$name = $values[$db_main_category];

# We store the category information in a globally accessable hash %category, indexed by category name.
@{$category{$name}} = @values;

# We also initilize the days_old variable to Jan 1. 1990 so that if
# this category doesn't have any links, we still have a valid date
# for it.
$stats{$name}[1] = "1-Jan-1990";
$stats{$name}[0] = 0;

# We now figure out if this category is a subcategory of something,
# and if so, store it in the hash of arrays %subcategories.
(@base) = split (/\//, $name);
pop @base; $base = join ("/", @base);
if ($base) {
push (@{$subcategories{$base}}, $name);
}
}
close DB;
}


----

3. At the bottom of site_html_templates.pl, above the 1;, add this:

sub cat_search {
#################################
# Builds a dropdown list of categories so user can select a specific category to search.


$output = qq(Search only in: <select name="thiscat">
<option value="" selected="selected">All Categories</option> );


$cat = &cat_search_list;
CATEGORY: foreach $cat (sort keys %category) {
next CATEGORY if ($cat =~ /^\s*$/); # How'd that get in here? =)

$title = &build_unlinked_title ($cat);
@categorylist = split (/\//, $cat);

$output .= qq~<option value="$cat">$title</option>~;
}

$output .= qq~</select> ~;

return $output;
}


This is meant to go within your existing search form, between the <form> tags.
--
3b. An option to list only top-level categories is available, and will create a shorter dropdown. Just change this:

next CATEGORY if ($cat =~ /^\s*$/);

to this:

next CATEGORY if ($cat =~ /\//);

----

4. In the same file, a
dd this to sub site_html_search_form, sub site_html_search_results, and sub site_html_search_error:

cat_search => &cat_search,

Or add it to the proper sub-routine for the page you want to use the drop-down on.

----

5. Then put this in your templates where you want the dropdown to appear:

<%cat_search%>

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

6. To do what you want, with checkboxes for just a few categories, do step 1 above, then put this code in your template wherever you want it:

<form action="$db_cgi_url/search.cgi" method="get">
<input type="text" size=15 name="query"><input type=submit value="Search!">
All: <input type="radio" name="thiscat" value="" checked="checked">
Photo: <input type="radio" name="thiscat" value="Photo">
Video: <input type="radio" name="thiscat" value="Video">
Illustration: <input type="radio" name="thiscat" value="Illustration">
</form>


Of course, you need to change the labels and values to suit your site. The value should be the category name as entered in the admin. So if you have "Digital Cameras" as a category, it's value would be "Digital_Cameras".

Also, adjust the HTML as needed...

This should work...

The code here is from these posts:

- http://www.gossamer-threads.com/...i?post=102744#102744
- http://www.gossamer-threads.com/...ch%20category;#42756



Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Search Category mod In reply to
there was something funky with the code in step 3, Perlflunkie the problems are in red if you wanna trouble shoot (kept saying build_unlinked_title was unidentified, or something)... not sure why but here is a very clunky fix for anyone pounding their heads against this mod

in step 3 instead of:

Code:
sub cat_search {
#################################
# Builds a dropdown list of categories so user can select a specific category to search. $output = qq(Search only in: <select name="thiscat">
<option value="" selected="selected">All Categories</option> ); $cat = &cat_search_list;
CATEGORY: foreach $cat (sort keys %category) {
next CATEGORY if ($cat =~ /^\s*$/); # How'd that get in here? =) $title = &build_unlinked_title ($cat);
@categorylist = split (/\//, $cat); $output .= qq~<option value="$cat">$title</option>~;
} $output .= qq~</select> ~; return $output;
}


try
Code:
sub cat_search {
#################################
# Builds a dropdown list of categories so user can select a specific category to search. $output = qq(Search only in: <select name="thiscat">
<option value="" selected="selected">All Categories</option> ); $cat = &cat_search_list;
CATEGORY: foreach $cat (sort keys %category) {
next CATEGORY if ($cat =~ /^\s*$/); # How'd that get in here? =) @categorylist = split (/\//, $cat); $output .= qq~<option value="$cat">$cat</option>~;
} $output .= qq~</select> ~; return $output;
}


great mod btw... surprised more folks dont use it Smile




Vote Stinky

Last edited by:

security_man: Dec 3, 2005, 8:05 PM
Quote Reply
Re: [security_man] Search Category mod In reply to
Glad you like it! I suspect it can be easily fixed by copying the sub build_unlinked_title from nph-build.cgi and pasting it into the bottom of add.cgi. I do not currently use this mod, and I don't have a copy of the code I used to test it; I guess I just forgot to include that step in the instructions.

Thanks for posting a fix that works!


Leonard
aka PerlFlunkie