Skip to main content.
home | support | download

Back to List Archive

Cgi Script needing to display "No Results Found"

From: Eric Haymaker <eric_haymaker(at)not-real.hotmail.com>
Date: Tue Nov 02 1999 - 17:33:16 GMT
Hello!

I am using both Swish-e and Swish.  The Swish came with a "toy cgi" that
works well, except that it doesn't display "No Results Found" if the query
words are not found.

Has anybody managed to edit that cgi script so that it will tell users that
nothing matched their query?  Or can someone give me an idea how to go about
editing it do that?

-- Eric



#! /usr/bin/perl
###############################################################################
#
# NAME
#
#	search.cgi
#
# SYNOPSIS
#
#	<FORM ACTION="search.cgi">
#	<INPUT TYPE=text NAME=search>
#	<INPUT TYPE=submit VALUE="Search">
#	</FORM>
#
# DESCRIPTION
#
#	This is a toy example CGI script written in Perl 5 to show how to
#	interface SWISH++ to a web-based search form.  It uses the supplied
#	WWW package to parse form data.
#
# SEE ALSO
#
#	search(1), WWW(3)
#
###############################################################################

use lib ( '/usr/home/swish++-3.0.3/lib' );
#		Put the path to where the WWW library is above.
require WWW;

$SWISH_BIN =	'/usr/home/swish++-3.0.3/bin';
#		The full path to the bin directory where you installed the
#		SWISH++ executables.

$DOC_ROOT =	'/usr/home/indexes';
#		The top-level directory for your document tree presumeably
#		where the index was generated from.

$INDEX_FILE =	'/usr/home/indexes/articles.index';
#		The full path to the index file to be searched through.

print "Content-Type: text/html\n\n";

# Header HTML
print <<END;
<HTML>
<HEAD>
<TITLE>Search Results</TITLE>
</HEAD>
<BODY>
<p style="margin-top: 0"><img border="0" src="/images/BannerSearchColor.jpg" 
width="400" height="64">
<font face="Agency FB"><BIG><B>Search Results</B></BIG></font>
<HR>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
END

##
# Zap dangerous characters before exposing to shell; escape rest.
##
$FORM{ search } =~ s/[^\s&'()*\-=\w]/ /g;
$FORM{ search } =~ s/([&'()*])/\\$1/g;

open( SEARCH, "$SWISH_BIN/search -i $INDEX_FILE $FORM{ search } |" ) or die;
while ( <SEARCH> ) {
	if ( /^# ignored: (.+)$/ ) {
		$ignored = $1;
		next;
	}
	##
	# Future releases of SWISH++ may emit other comments: ignore ones we
	# don't know about.
	##
	next if /^#/;

	my( $rank, $file, $size, $title ) = split( / /, $_, 4 );

	my $desc = WWW::extract_description( "$DOC_ROOT/$file" );
	WWW::hyperlink( $desc );
	$size /= 1024;
	$size = '&lt;1K' unless $size;

	print <<END;
	<TR VALIGN=top><TD ALIGN=right>$rank%&nbsp;&nbsp;</TD>
	<TD><DL><DT><B><A HREF="$file">$title</A></B> ($size)<DD>$desc</DL></TD>
END
}
close SEARCH;

print "</TABLE>\n";
if ( $? ) {
	print "<B>malformed query</B>\n";
} else {
	print "<P>ignored: $ignored\n" if $ignored;
}

# Footer HTML
print <<END;

<FORM METHOD=get ACTION="/cgi-bin/SearchArticles.cgi">
<B>Search:</B>
<INPUT TYPE=text NAME=search SIZE=30>
<INPUT TYPE=submit VALUE="New Search" ALIGN="top"
      style="background-color: #000000; color: #FFFFFF; font-family: 
sans-serif; font-size: 12pt; font-weight: regular; background-image: 
url('/images/ON_Button.jpg')">
</FORM>
</BODY>


</HTML>
END

______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com
Received on Tue Nov 2 09:32:42 1999