简体   繁体   English

如何在搜索引擎中使用关键字

[英]how to use keywords in searchengine

hello i`m sitting here and thinking about the following issue: 你好,我坐在这里,考虑以下问题:

i have a autosuggestion search engine that runs through a table like: 我有一个自动建议搜索引擎,该引擎通过一个表运行:

zipcode    city       sum

12345      town       5

i do realize the search by using javascript that runs through a loop on each letter that will be typed in. up to that point i only can look for one keyword that can be city name or zipcode for example. 我确实通过使用JavaScript来实现搜索,该JavaScript在要键入的每个字母上循环运行。到那时为止,我只能寻找一个关键字,例如城市名称或邮政编码。

now i would like to combine these both so that i do not limit an user anymore to one of these both and even when entering city and zipcode it will be found. 现在,我想将两者结合起来,这样我就不再将用户限制在这两者之中,即使在输入城市和邮政编码时也可以找到它。

therefor i would like to seperate each keyword by using: 因此,我想使用以下命令将每个关键字分开:

preg_split('/[\s]+/', $keywords);

this is the problem at the moment. 这是目前的问题。 using this means, every keyword will be inserted in an array. 使用这种方式,每个关键字都会插入一个数组中。 how it works: 这个怎么运作:

in the html i have an input field. 在html中,我有一个输入字段。 below that i have an unordered list div. 下面,我有一个无序列表的股利。 in that div javascript is automatically adding the results from my search.php file. 在该div中,javascript将自动从我的search.php文件中添加结果。 that looks like: 看起来像:

include('../scripts/db_connect.php');

if (isset($_POST['search_term']) == true && empty($_POST['search_term']) == false) {

$search_term = $db->real_escape_string(htmlentities(trim($_POST['search_term'])));

$search_term_query = "SELECT * FROM `a` WHERE `b` LIKE '$search_term%'";

if ($result_query = $db->query($search_term_query)) {

    while ($row = $result_query->fetch_assoc()) {

    echo '<li>', 
          $row["a"], 
          ' ', 
          $row["b"],  
          ' ',
          '( ',
          $row["c"],
          ' )', 
          ' </li>';
    }

}

}

and my javascript is only handling one keyword: 而我的JavaScript只处理一个关键字:

$(document).ready(function() {
    $('.searchfield').keyup(function() {
        var search_term = $(this).attr('value');
        $.post('ajax/search.php', {search_term:search_term}, function(data) {
            $('.result').html(data);

            $('.result li').click(function() {
                var result_value = $(this).text();
                $('.searchfield').attr('value', result_value);
                $('.result').html('');
            });
        });
    });
});

so at the moment i dont know how to solve that. 所以目前我不知道该怎么解决。 if there is someone who could help me out i really would appreciate. 如果有人可以帮助我,我将不胜感激。 thanks. 谢谢。

Just keep the value as it is and send the whole string to your php file through a post call. 只需保持该值不变,然后通过邮寄将整个字符串发送到您的php文件即可。 In your search.php you can try something like this: 在您的search.php中,您可以尝试执行以下操作:

function split_search_term($val) {
    $val = explode(" ", $val);
    $search_term_query = "SELECT * FROM `a` WHERE ";
    foreach($val as $val_row) {
        $search_term_query .= "(`zipcode` LIKE '" . $search_term . "%' OR `city`  LIKE '" . $search_term . "%') OR ";
    }
    if(count($val) > 0) {
        // The IF statement is used to make sure that the foreach loop has been fired at least once,
        // or else it would just chop off the 'WHERE ' part and you'll get something like:
        // "SELECT * FROM `a` WH" and this will give you an error
        $search_term_query = substr($search_term_query, 0, -4); // To cut off the last remaining ' OR ' in the query
    } else {
        $search_term_query .= "0"; // To finish the query so it doesn't return anything we don't want
    }

    return $search_term_query;
}

From here you can go ahead and fetch the result. 从这里您可以继续并获取结果。 I hope it works. 我希望它能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM