高级搜索代码

时间:2012-01-23 05:18:08

标签: mysql search

我正在转换分类广告/平面文件perl脚本以使用mysql / dbi; 我有这个搜索代码,但它工作得不好。它会从item_category中找到Vintage_Trailers,但是如果我包含item_state它将找不到任何内容。我需要它只能在内华达州找到Vintage_Trailers。但是用户可能不会选择状态,因此只能找到Vintage_trailers。或者也许只有关键词'Shasta'......我可以修改的“开箱即用”的东西非常棒。任何帮助表示赞赏。

$searchfor="$multi_input $keywords $user $item_category $item_city $item_state";
my $dsn = "DBI:mysql:$database";
my $dbh = DBI->connect($dsn, $userid, $password ) 
          or die $DBI::errstr;
my @searchthings = split(/ /,$searchfor);
foreach $thing(@searchthings)
     {
    if ($thing){
        $statement .= "(item_name like '%$thing%' or 
        item_desc like '%$thing%' or 
        item_desc2 like '%$thing%' or 
        item_category like '%$thing%' or 
        item_city like '%$thing%' or 
        item_state like '%$thing%' or 
        user like '%$thing%' ) ";
    }
}

$sth = $dbh->prepare(qq(select * from ads where $statement)) or  die $DBI::errstr;
$sth->execute();
    while ((my (@rows)) = $sth->fetchrow_array)
    {
    $total_row_count= $sth->rows;
    $database_rows = join ("\|", @rows);
    push (@database_rows,$database_rows);
    }
$sth->finish() or  die $DBI::errstr;
 $dbh->disconnect() or  die $DBI::errstr;

1 个答案:

答案 0 :(得分:0)

$ANDOR = param('andor');#creates an AND OR search
if($ANDOR eq ""){ $ANDOR="AND";}

if($item_category){
$item_category = "$item_category";
} else{  $item_category=" "; 
}
$statement .= "item_category LIKE  ? ";
push(@binds,$item_category);


if($item_state){
$statement .= " $ANDOR item_state LIKE ? ";
push(@binds,$item_state);
}
if($item_city){
$statement .= " $ANDOR item_city LIKE ? ";
push(@binds,$item_city);
}
if($db_id){
$statement .= " $ANDOR db_id = ? ";
push(@binds,$db_id);
}

$keywords=param('keywords');
if(param('as_a_phrase')){
$keywords =~ s/ /\+/g;
}
if($keywords)
{
if(param('searchmatch') eq "exact")
{
$statement .= " $ANDOR (item_name = ? OR item_desc = ? OR item_desc2 = ? )";#
push(@binds,$keywords,$keywords,$keywords);

}
else
{   $statement .= " $ANDOR  ";

my @keywords = split(/ /,$keywords);
my $keywordcount=@keywords;

foreach my $keyword(@keywords)
{ $keywordcount2++;
$statement .= " (item_name LIKE ? 
OR item_desc LIKE ? 
OR item_desc2 LIKE ? )";
if ($keywordcount2 == $keywordcount) {$statement .= "";} else{ $statement .= " OR "; }
push(@binds, "%$keyword%","%$keyword%","%$keyword%");
}
}
}

$date_begin=param('date_begin');
if($date_begin){
$statement .= " $ANDOR modification_time > ? "; 
push(@binds,$date_begin);
}

if($user){
$statement .= " $ANDOR user LIKE ? ";
push(@binds,$user);
}


$price_low=param('price_low');
$price_high=param('price_high');
if (($price_low) && ($price_high)){
$statement .= " $ANDOR item_price BETWEEN ? AND ? ";
push(@binds,$price_low,$price_high);
}
elsif (($price_low) && ($price_high eq "")){
$statement .= " $ANDOR item_price > ? ";
push(@binds,$price_low);
}
elsif (($price_high) && ($price_low eq "")){
$statement .= " $ANDOR item_price BETWEEN ? AND ? ";
push(@binds,1,$price_high);
}
else
{  }


my $sth = $dbh->prepare(qq(SELECT * FROM ads WHERE $statement )) or  die $DBI::errstr;
$sth->execute(@binds);