私はデータベースをループしたいと思います。すべての値に対して、クエリのmy $ idarrayで返されたid値を使用して検索を絞りたいと思います。たとえば、ユーザーが「molly loves cats」を検索したいと思ったら、最初に「molly」のデータベースを検索し、すべてのIDを取得して、次の検索で使用します"愛する"など。そして最後に、すべての3つの単語で構成されるすべてのIDのリストを取得します。データベースをループし、返されたid値を使用して絞込み検索
search.php
$searchquery= (isset($_POST['searchq']) ? $_POST['searchq'] : NULL);
$q = explode(" ", $searchquery);
foreach($q as $key => $value){
$in = implode("','", $idarray);
if($stmt = mysqli_prepare($db_mysqli, "SELECT id FROM tbl_headlines WHERE (name LIKE ? OR header LIKE ?) AND id IN('$in')")){
$searchquerypre = "{$value}%";
mysqli_stmt_bind_param($stmt, "ss", $searchquerypre);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $id);
if(mysqli_stmt_num_rows($stmt) > 0){
while (mysqli_stmt_fetch($stmt)){
$idarray[] = array('id'=> $id, 'resultat'=> true);
}
}
if(mysqli_stmt_num_rows($stmt) == 0){
$idarray[] = array('resultat'=> false);
}
}
}
echo json_encode($idarray);
jqueryの
$('#searchquery').on('keyup', function(){
var searchquery = $("#searchquery").val();
if ($("#searchquery").val().length > 1) {
$.ajax({
type: "POST",
dataType: "json",
url: "search.php",
data: {searchq: searchquery},
success: function(result){
console.dir(result);
},
error: function(){
alert('Error');
}
});
}
});
3でクエリを実行すると、なぜそれが1になるのですか? – Naruto
これらの単語を1つのクエリで検索し、その検索の優先度を設定することもできます。 –
@Linda:コードで実際に何が間違っているのかを言わなかったのですか?あなたが非常に簡単なやり方でやっているよりも難しくなったようです。 –