微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AJAX 实时搜索正在复制结果

如何解决AJAX 实时搜索正在复制结果

我有这个脚本,它从输入字段读取文本,然后将值发送到 PHP 文件搜索数据库中的所有表(“pharmacie”)。我在不同的表中有相同的数据(例如:“ABILIFY 10MG COMP. B/28”存在于两个表中)。我如何只显示一次。 我试过 SELECT disTINCT。

Results output

<script>
    $(document).ready(function() {
      $('.search input[type="text"]').on("keyup input",function() {
        /* Get input value on change */
        var inputVal = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if (inputVal.length) {
          $.get("livesearch.PHP",{
            term: inputVal
          }).done(function(data) {
            // display the returned data in browser
            resultDropdown.html(data);
          });
        } else {
          resultDropdown.empty();
        }
      });

      // Set search input value on click of result item
      $(document).on("click",".result p",function() {
        $(this).parents(".search").find('input[type="text"]').val($(this).text());
        $(this).parent(".result").empty();
      });
    });
  </script>

livesearch.PHP

<?PHP
/* Attempt MysqL server connection. Assuming you are running MysqL
server with default setting (user 'root' with no password) */
$link = MysqLi_connect("localhost","root","","pharmacie");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . MysqLi_connect_error());
}

$tables = MysqLi_query($link,"SHOW TABLES");
while ($table = MysqLi_fetch_object($tables))
{
    $table_name = $table->{"Tables_in_pharmacie"};
 
if(isset($_REQUEST["term"])){
    // Prepare a select statement
    $sql = "SELECT * FROM " . $table_name . " WHERE Nom_medicine LIKE ?";
    
    if($stmt = MysqLi_prepare($link,$sql)){
        // Bind variables to the prepared statement as parameters
        MysqLi_stmt_bind_param($stmt,"s",$param_term);
        
        // Set parameters
        $param_term = $_REQUEST["term"] . '%';
        
        // Attempt to execute the prepared statement
        if(MysqLi_stmt_execute($stmt)){
            $result = MysqLi_stmt_get_result($stmt);
            
            // Check number of rows in the result set
            if(MysqLi_num_rows($result) > 0){
                // Fetch result rows as an associative array
                while($row = MysqLi_fetch_array($result,MysqLI_ASSOC)){
                    echo "<p>" . $row["Nom_medicine"] . "</p>";
                }
            } 
        } else{
            echo "ERROR: Could not able to execute $sql. " . MysqLi_error($link);
        }
    }
     
    // Close statement
    MysqLi_stmt_close($stmt);
}
 
}
// close connection
MysqLi_close($link);
?>

解决方法

您遍历所有表,并回显每个表的结果,这就是 select distinct 不起作用的原因。你可以写回,你已经返回的结果并跳过它们。

我可能不会那样解决它,您可以更多地重写脚本以在单个选择中存档所有这些,或者您甚至可以直接在 sql 查询中排除已经打印的那些以获得更快的性能,但作为快速修复这应该对您有用:

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost","root","","pharmacie");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
$alreadyReturned = [];
$tables = mysqli_query($link,"SHOW TABLES");
while ($table = mysqli_fetch_object($tables))
{
    $table_name = $table->{"Tables_in_pharmacie"};
 
if(isset($_REQUEST["term"])){
    // Prepare a select statement
    $sql = "SELECT * FROM " . $table_name . " WHERE Nom_medicine LIKE ?";
    
    if($stmt = mysqli_prepare($link,$sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt,"s",$param_term);
        
        // Set parameters
        $param_term = $_REQUEST["term"] . '%';
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);
            
            // Check number of rows in the result set
            if(mysqli_num_rows($result) > 0){
                // Fetch result rows as an associative array
                while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
                    if (in_array($row["Nom_medicine"],$alreadyReturned,true))
                    {
                        continue;
                    }
                    echo "<p>" . $row["Nom_medicine"] . "</p>";
                    $alreadyReturned[] = $row["Nom_medicine"];
                }
            } 
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
}
 
}
// close connection
mysqli_close($link);
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。