簡體   English   中英

AJAX自動提示系統是否顯示重復項?

[英]AJAX auto-suggestion system shows duplicates?

我有一個搜索欄,用於搜索一些標題。 我為此做了一個簡單的自動完成系統。 我做了一個簡單的帶有自動完成功能的MySQL表。 就這個。

表

然后,我使用AJAX檢索數據。 但是,此自動完成系統顯示重復項。 圖片后給出了代碼。 (標題google有重復的條目,但表中只有一個)。

在此處輸入圖片說明

 function suggest(val){ if(val.length == 0){ document.getElementById("suggestions").innerHTML = ''; } else{ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if(this.readyState == 4 && this.status == 200){ var res = this.response; if(res.length != 0){ document.getElementById("suggestions").innerHTML = res; var spans = document.getElementById("suggestions").getElementsByTagName('span'); var span_values = []; for(span of spans){ if(span_values.includes(span.innerHTML)){ console.log(span.innerHTML); span.nextSibling.remove(); span.remove(); } else{ console.log(span.innerHTML); span_values.push(span.innerHTML); } } } else{ document.getElementById("suggestions").innerHTML = ''; } } } xhttp.open('GET', '../../search/apis/suggestions/?query=' + val, true); xhttp.send(); } } 
 #omnibox-container-main-div{ margin:40px 30px 0 30px; text-align: center; } #omnibox-container-div{ display: inline-flex; flex-flow: row wrap; align-items: center; } #omnibox-input{ display: inline-block; width: 80%; padding:8px; border:none; border-left:2px solid rgb(0, 97, 189); border-right:none; border-top:2px solid rgb(0, 97, 189); border-bottom:2px solid rgb(0, 97, 189); font-family: 'regular'; font-size:15px; border-top-left-radius: 50px; border-bottom-left-radius: 50px; } #omnibox-input::placeholder{ font-family: 'light'; color:rgb(134, 134, 134); } #omnibox-submit-btn{ display: inline-block; width:70px; color:rgb(126, 126, 126); font-family: 'light'; font-size: 15px; padding:8px; border: none; background-color: white; border-top:2px solid rgb(0, 97, 189); border-bottom:2px solid rgb(0, 97, 189); border-right:2px solid rgb(0, 97, 189); border-left:none; border-top-right-radius: 50px; border-bottom-right-radius: 50px; } #omnibox-submit-btn:hover{ background-color: rgb(1, 85, 163); color:white; cursor: pointer; } #close-notice-span{ text-decoration: none; color:rgb(78, 78, 78); font-family: 'bold'; position: absolute; right:10px;; cursor: pointer; } #suggestions{ margin-top:10px; font-family: 'medium'; z-index: 20; } .suggestions-a{ text-decoration: none; margin:5px; color:rgb(148, 148, 148); font-size: 15px; padding-left:8px; padding-right:8px; cursor: pointer; } .suggestions-a:hover{ background-color: rgba(21, 122, 255, 0.6); color:white; border-radius: 50px; } #sug-wrapper{ display:none; margin-left:calc((100vw - 470px) / 2); border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; margin-right:calc((100vw - 470px) / 2); padding-right:15px; padding-left: 15px; margin-top:-10px; border-left:2px solid rgb(0, 97, 189); border-right:2px solid rgb(0, 97, 189); border-bottom:2px solid rgb(0, 97, 189); box-shadow: 2px 2px 2px 2px rgb(167, 167, 167); height:200px; max-height: 200px; overflow: auto; } 
 <div id="omnibox-container-main-div"> <div id="omnibox-container-div"> <input id="omnibox-input" type="text" value = "" placeholder="Type here..." oninput="suggest(this.value)" autocomplete="off"/><button id="omnibox-submit-btn" onclick="search_query()">Search</button> </div> </div> <div id="sug-wrapper"> <div id="suggestions"> </div> </div> 

服務器端PHP代碼。

<?php
    if(isset($_GET['query'])){
        $q = strtolower($_GET['query']);
        if($q != '' or $q != null){
            $con = mysqli_connect('localhost', 'root', '', 'search');
            $sql = "SELECT `query` FROM `suggestions` WHERE `query` LIKE '%".$q."%'";
            $res = mysqli_query($con, $sql);
            if(mysqli_num_rows($res) > 0){
                $matched = [];
                $temp = [];
                while($row = mysqli_fetch_assoc($res)){
                    $query = strtolower($row['query']);
                    array_push($temp, $query);
                }
                array_unique($temp);
                $matched = preg_grep("/^".$q.".*?/", $temp);
                array_unique($matched);
                array_splice($matched, 6, count($matched) - 6);
                foreach($matched as $match){
                    $mes = "<span class='suggestions-a' onmouseleave='selected=false;' onmouseenter='selected=true;' onclick='change_omni(\"".$match."\")'>".$match."</span><br/>";
                    echo $mes;
                }
            }
            mysqli_close($con);
        }
    }
?>

我不為什么它顯示重復的值。 我不知道為什么。 我嘗試了很多事情

  • php代碼中的array_unique()
  • 檢查並刪除javascript中重復的span標簽

與該問題有關的所有代碼均已發布在上面。 請告訴我原因。

您無法調用array_unique的原因是array_unique 返回了一個唯一值數組。 它不會影響輸入數組。 這樣的事情會起作用:

$matched = array_unique($matched);

但是,在查詢中處理重復項比較容易,只需添加DISTINCT關鍵字:

$sql = "SELECT DISTINCT `query` FROM `suggestions` WHERE `query` LIKE '%".$q."%'";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM