Mój problem jest następujący: mam wyszukiwarkę na stronie, jednak gdy wyszukuję słowo "kania", w wynikach pojawiają się również "mieszkania", mimo że wyszukuję z opcją "exact", czyli wyszukiwanie tylko dokładnie tych słów, które podałem.
Formularz wygląda tak:
Kod
<form name="search" action="search.php" method="link" onSubmit="return validateForm(search);" style="margin: 0px; padding: 0px;">
<td class="search_bar">
<table width="100%">
<tr>
<td width="400" nowrap>
<div style="background-image: url(../images/search_back.jpg); border: 1px solid #AFAFAF; padding: 3px;">
<? echo $search_search; ?> <input type="textbox" name="search" class="search_box"> <input type="submit" value="<? echo $search_button; ?>" class="go_button">
<br />
<input type="radio" name="match_type" value="all" <? if($_SESSION['search_match_type'] == "all"){ echo "checked"; } ?>> <? echo $search_match_all; ?> | <input type="radio" name="match_type" value="any" <? if($_SESSION['search_match_type'] == "any"){ echo "checked"; } ?>> <? echo $search_match_any; ?><br><? if($setting->hide_id != 1){ ?><input type="radio" name="match_type" value="id" <? if($_SESSION['search_match_type'] == "id"){ echo "checked"; } ?>> <? echo $search_match_id; ?> | <? } ?><input type="radio" name="match_type" value="exact" <? if($_SESSION['search_match_type'] == "exact"){ echo "checked"; } ?>> <? echo $search_match_exact; ?>
</form>
<td class="search_bar">
<table width="100%">
<tr>
<td width="400" nowrap>
<div style="background-image: url(../images/search_back.jpg); border: 1px solid #AFAFAF; padding: 3px;">
<? echo $search_search; ?> <input type="textbox" name="search" class="search_box"> <input type="submit" value="<? echo $search_button; ?>" class="go_button">
<br />
<input type="radio" name="match_type" value="all" <? if($_SESSION['search_match_type'] == "all"){ echo "checked"; } ?>> <? echo $search_match_all; ?> | <input type="radio" name="match_type" value="any" <? if($_SESSION['search_match_type'] == "any"){ echo "checked"; } ?>> <? echo $search_match_any; ?><br><? if($setting->hide_id != 1){ ?><input type="radio" name="match_type" value="id" <? if($_SESSION['search_match_type'] == "id"){ echo "checked"; } ?>> <? echo $search_match_id; ?> | <? } ?><input type="radio" name="match_type" value="exact" <? if($_SESSION['search_match_type'] == "exact"){ echo "checked"; } ?>> <? echo $search_match_exact; ?>
</form>
Skrypt odpowiadający za wyszukiwanie wygląda tak:
Kod
$my_search = strtolower($my_search);
$my_search_words = split(" ",$my_search);
$words = count($my_search_words);
for($z = 0; $z < $words; $z++){
if(strlen($my_search_words[$z]) >= 1){
$my_search_words2 = $my_search_words2 . "," . strtolower($my_search_words[$z]);
//$my_search_words2 = "," . $my_search_words2;
}
}
$my_search_words2 = split(",",$my_search_words2);
$words2 = count($my_search_words2);
//echo $words2;
//exit;
//$my_search = "t";
if($match_type == "id"){
$searcher = "SELECT * FROM photo_package where active = '1' and gallery_id IN ($approved_cats) and id = '" . $_GET['search'] . "'";
}
if($match_type != "id" && $match_type != "exact"){
if($words2 < 2){
$searcher = "SELECT * FROM photo_package where active = '1' and gallery_id IN ($approved_cats) and keywords like '%$my_search%'";
} else {
$searcher = "SELECT * FROM photo_package where active = '1' and gallery_id IN ($approved_cats) and (";
for($z2 = 1; $z2 < $words2; $z2++){
$searcher.= " keywords like '%" . $my_search_words2[$z2] . "%'";
if($z2 < ($words2 - 1)){
if($match_type == "any"){
$searcher.= " or ";
}
if($match_type == "all"){
$searcher.= " and ";
}
}
}
if($words2 > 0){
$searcher.= " or";
}
for($z3 = 1; $z3 < $words2; $z3++){
$searcher.= " title like '%" . $my_search_words2[$z3] . "%'";
if($z3 < ($words2 - 1)){
if($match_type == "any"){
$searcher.= " or ";
}
if($match_type == "all"){
$searcher.= " and ";
}
}
}
if($words2 > 0){
$searcher.= " or";
}
for($z3 = 1; $z3 < $words2; $z3++){
$searcher.= " description like '%" . $my_search_words2[$z3] . "%'";
if($z3 < ($words2 - 1)){
if($match_type == "any"){
$searcher.= " or ";
}
if($match_type == "all"){
$searcher.= " and ";
}
}
}
}
$search_display_limit = $setting->search;
$searcher.= ") order by cart_count desc LIMIT $search_display_limit";
}
if($match_type == "exact"){
$searcher = "SELECT * FROM photo_package where active = '1' and gallery_id IN ($approved_cats) and keywords like '%$my_search%' or description like '%$my_search%' or title like '%$my_search%'";
$search_display_limit = $setting->search;
$searcher.= " order by cart_count desc LIMIT $search_display_limit";
}
$package_result = mysql_query($searcher, $db);
$package_rows = mysql_num_rows($package_result);
if($match_type == "exact"){
while($package = mysql_fetch_object($package_result)){
$search_array = explode(",",$package->keywords);
$seek = $_GET['search'];
foreach($search_array as $key => $value){
$seeker = trim($search_array[$key]);
if($seeker == $seek){
$match++;
} else {
// Do not match anything
}
}
$search_array_title = $package->title;
if($seek == $_GET['search']){
$match = 1;
}
if($match > 0){
$id = $package->id;
$photo_result = mysql_query("SELECT * FROM uploaded_images where reference = 'photo_package' and reference_id = '$package->id' order by original", $db);
$photo_rows = mysql_num_rows($photo_result);
$photo = mysql_fetch_object($photo_result);
$my_search_words = split(" ",$my_search);
$words = count($my_search_words);
for($z = 0; $z < $words; $z++){
if(strlen($my_search_words[$z]) >= 1){
$my_search_words2 = $my_search_words2 . "," . strtolower($my_search_words[$z]);
//$my_search_words2 = "," . $my_search_words2;
}
}
$my_search_words2 = split(",",$my_search_words2);
$words2 = count($my_search_words2);
//echo $words2;
//exit;
//$my_search = "t";
if($match_type == "id"){
$searcher = "SELECT * FROM photo_package where active = '1' and gallery_id IN ($approved_cats) and id = '" . $_GET['search'] . "'";
}
if($match_type != "id" && $match_type != "exact"){
if($words2 < 2){
$searcher = "SELECT * FROM photo_package where active = '1' and gallery_id IN ($approved_cats) and keywords like '%$my_search%'";
} else {
$searcher = "SELECT * FROM photo_package where active = '1' and gallery_id IN ($approved_cats) and (";
for($z2 = 1; $z2 < $words2; $z2++){
$searcher.= " keywords like '%" . $my_search_words2[$z2] . "%'";
if($z2 < ($words2 - 1)){
if($match_type == "any"){
$searcher.= " or ";
}
if($match_type == "all"){
$searcher.= " and ";
}
}
}
if($words2 > 0){
$searcher.= " or";
}
for($z3 = 1; $z3 < $words2; $z3++){
$searcher.= " title like '%" . $my_search_words2[$z3] . "%'";
if($z3 < ($words2 - 1)){
if($match_type == "any"){
$searcher.= " or ";
}
if($match_type == "all"){
$searcher.= " and ";
}
}
}
if($words2 > 0){
$searcher.= " or";
}
for($z3 = 1; $z3 < $words2; $z3++){
$searcher.= " description like '%" . $my_search_words2[$z3] . "%'";
if($z3 < ($words2 - 1)){
if($match_type == "any"){
$searcher.= " or ";
}
if($match_type == "all"){
$searcher.= " and ";
}
}
}
}
$search_display_limit = $setting->search;
$searcher.= ") order by cart_count desc LIMIT $search_display_limit";
}
if($match_type == "exact"){
$searcher = "SELECT * FROM photo_package where active = '1' and gallery_id IN ($approved_cats) and keywords like '%$my_search%' or description like '%$my_search%' or title like '%$my_search%'";
$search_display_limit = $setting->search;
$searcher.= " order by cart_count desc LIMIT $search_display_limit";
}
$package_result = mysql_query($searcher, $db);
$package_rows = mysql_num_rows($package_result);
if($match_type == "exact"){
while($package = mysql_fetch_object($package_result)){
$search_array = explode(",",$package->keywords);
$seek = $_GET['search'];
foreach($search_array as $key => $value){
$seeker = trim($search_array[$key]);
if($seeker == $seek){
$match++;
} else {
// Do not match anything
}
}
$search_array_title = $package->title;
if($seek == $_GET['search']){
$match = 1;
}
if($match > 0){
$id = $package->id;
$photo_result = mysql_query("SELECT * FROM uploaded_images where reference = 'photo_package' and reference_id = '$package->id' order by original", $db);
$photo_rows = mysql_num_rows($photo_result);
$photo = mysql_fetch_object($photo_result);
Napiszcie chociaż gdzie mam szukać błedu.
Thanks from the mountain!