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, add 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