2012-01-23 53 views
0

我正在將分類的廣告/平面文件perl腳本轉換爲使用mysql/dbi; 我有這個搜索代碼,它不工作得很好。它會從item_category中找到Vintage_Trailers,但是如果我包含item_state,它將找不到任何東西。我需要它僅在內華達州找到Vintage_Tilers。但用戶不能選擇一個狀態,所以只能找到Vintage_trailers。或者,也許只有關鍵字'沙斯塔'......我可以修改的某些「開箱即用」功能非常出色。任何幫助表示讚賞。高級搜索代碼

$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; 

回答

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 [email protected]; 

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);