【问题标题】:How to make an autocomplete search with flexsearch如何使用 flexsearch 进行自动完成搜索
【发布时间】:2019-06-28 13:22:13
【问题描述】:

在我的研究案例中,我必须使用此脚本创建一个自动完成搜索。我的问题是如何调用数据库。我不知道。 我想我必须创建另一个文件调用 search.php

我的代码

<head>
     <script src="https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@master/dist/flexsearch.min.js"></script>


<script src="search.php"></script>

<style>
        table{
            width: 300px;
            table-layout: fixed;
        }
        td, tr{
            border: none;
        }
        input{
            border: 1px solid #ddd;
            border-radius: 3px;
            outline: none;
            background-color: #f5f5f5;
        }
        input, div{
            padding:5px 5px;
            width: 100%;
            box-sizing: border-box;
        }
        #suggestions div{
            padding: 10px 0;
            border-bottom: 1px solid #ddd;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
    </style>

</head>

  <div><input type="text" placeholder="Search ..." onkeyup="show_results.call(this);"></div>
  <div id="suggestions"></div>

</div>


<script>
  (function(){

    var index = new FlexSearch({

      encode: "advanced",
      tokenize: "reverse",
      suggest: true
    });

    var container = document.getElementById("suggestions");

    for(var i = 0; i < data.length; i++){

      index.add(i, data[i]);
    }

    window.show_results = function(){

      var results = index.search(this.value, 10);
      var fragment = document.createDocumentFragment();
      var entry, tmp;

      for(var i = 0; i < results.length; i++){

        entry = document.createElement("div");
        entry.textContent = data[results[i]];
        fragment.appendChild(entry);
      }

      while((tmp = container.firstChild)){

        container.removeChild(tmp)
      }

      container.appendChild(fragment);
    };

  }());
</script>

我的search.php,我尝试了这段代码,但是关于如何获取某人制作的搜索关键字。

<?php
        $terms = strtolower($_GET["q"]);

        $Qcheck = $Db->prepare('select distinct products_id as id,
    products_description as description
                                            from :table_products_description
                                            where products_description LIKE :terms
                                            limit 10
                                          ');
        $Qcheck->bindValue(':terms', '%' . $terms . '%');
        $Qcheck->execute();

        $list = $Qcheck->rowCount() ;

        if ($list > 0) {
          $array = [];

          while ($value = $Qcheck->fetch() ) {
            $array[] = $value;
          }

          $json_response = json_encode($array); 

          echo $json_response;
?>

我希望在输入字段中显示整个数据库的搜索结果

【问题讨论】:

  • 看起来您可能遗漏了一些代码。当您致电onkeyup="show_results.call(this);" 时,您似乎走在了正确的轨道上。这会尝试调用页面上可能不存在的函数,因此没有任何反应。
  • @khuderm 我基于此链接演示 github.com/nextapps-de/flexsearch/tree/master/demo 。在我不知道如何进行 php / mysql 搜索之后

标签: php mysql autocomplete


【解决方案1】:

我想帮助你。首先我无法检查你的 php 代码,所以请检查 php 返回一个 json 编码的字符串。那么您的意图的解决方案如下。

用这两行替换search.php中的最后一行:

header('Content-Type: text/javascript');
echo 'var data = ' . $json_response . ';';

就是这样:)

【讨论】:

    猜你喜欢
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 2012-06-03
    • 1970-01-01
    • 2012-07-29
    • 2017-09-22
    • 1970-01-01
    相关资源
    最近更新 更多