#!/usr/bin/perl -w
use DBI;

print "Content-Type:text/html\n\n";
print "<html>";
print "<head><title>Search</title>";
print "<base href=\"http://www.doctor-n.ru/\">";
print "<link rel=stylesheet type=text/css href=/css/cssnn.css></head>";
print "<body leftmargin=2 topmargin=2 marginwidth=2 marginheight=2>";
print "<table border=0 cellpadding=0 cellspacing=0 width=100%>";
print "<tr><td bgcolor=#E0EBE1 height=64 align=left valign=middle>";
print "<img src=/images/1x1.gif width=10 height=1><a href=index.shtml><img src=/images/logodn.gif width=126 height=39 border=0 alt=logo></a>";
print "</td><td bgcolor=#E0EBE1 align=right valign=middle>";
print "<img src=/images/1x1.gif width=468 height=1><img src=/images/1x1.gif width=15 height=1><img src=/images/1.gif width=69 height=62 border=0>";print "</td></tr></table>";
print "<table border=0  cellpadding=0 cellspacing=10>";
print "<tr><td align=left valign=top><div align=right><a class=menu href=\"javascript:history.back();void(0);\">Вернуться</a></div></td>";
print "<td align=left valign=top><div class=top align=right><a class=menu href=\"/search.shtml\">Поиск</a></div>";
print "</td></tr></table>";

$form{category}="";
$my_query_string = $ENV{'QUERY_STRING'};
@pairs = split(/&/,$my_query_string);
foreach $pair (@pairs) 
{
($name, $value) = split(/=/, $pair); 
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("c",hex($1))/eg;
if ($name eq "search") { $form{"search"} = $value;} 
if ($name eq "category") { $form{"category"} = $value; }
 } 
#print "SEARCH:$form{search}<br>";
#print "CATEGORY:$form{category}<br>";

if ($form{search} ne "")
{

@form{search}=~ tr/+/ /s;
@fin = split(/ /, $form{search});

$field="";
$dbh= DBI->connect("DBI:mysql:homeo-apte_dn:homeo-apte.mysql","homeo-apte_mysql","8lj3bhlx") || 
 die "Got error $DBI::errstr when connecting to $dsn\n";
$sth = $dbh->prepare("SET NAMES cp1251") or die $mysql::db_errstr;
$sth->execute or die "Unable_to_execute";

$flag=1;
foreach $fin (@fin)
{
if ($flag==1) {$flag=0;}
else {
$field.=" OR ";
}
$field.="(title like \"%$fin%\") OR (kind like \"%$fin%\") OR (structure like \"%$fin%\") OR (indication like \"%$fin%\")";
}
#print "$field<br>";
#print "$form{category}";
$sth = $dbh->prepare("SELECT DISTINCT * FROM $form{category} WHERE $field") or die $mysql::db_errstr;
$sth->execute or die "Unable_to_execute";
$rv = $sth->rows;
print "&nbsp;&nbsp;&nbsp;Всего записей: $rv\n";
if ($rv>0) 
{
print "<table align=center bordercolor=#B0CEB6 border=2 cellpadding=5 cellspacing=0 width=96%>";

while ( @row = $sth->fetchrow_array )
  {
 print "<tr><td>";
   foreach $row (@row)
     {
     print "$row<br>";
     }
 print "</td></tr>";
  }
print "</table>";
}
else
 {
print "К сожалению записей не найдено. Попробуйте сократить запрос.";
 }
down();
$sth->finish;	
$dbh->disconnect;
Apache::exit();

} else
{
print "Пустой поисковый запрос.";
down();
Apache::exit();
}

sub down
{
print "<table border=0  cellpadding=0 cellspacing=10>";
print "<tr><td align=left valign=top><div align=right><a class=menu href=\"javascript:history.back();void(0);\">Вернуться</a></div></td>";
print "<td align=left valign=top><div class=top align=right><a class=menu href=\"/search.shtml\">Поиск</a></div>";
print "</td></tr></table>";
print "</body></html>";
}

