我爲我的網站構建了一個簡單的搜索引擎,它的工作方式是當用戶輸入關鍵字時,它會查詢數據庫並顯示結果。使用php和/或jquery過濾mysql搜索結果
我的數據庫表看起來像這樣:
game_id title developer publisher genre release_date platform
rating image_location description
我想允許用戶通過結果中過濾,一旦他們做了一個搜索,最好不刷新頁面(僅被編碼了一個月,但有對jquery和ajax的一般理解)。
這是我的代碼,即時通訊使用包含搜索欄,搜索結果和查詢頁面的分隔。
第1頁搜索欄
<div id=\"top_search\">
<form name=\"input\" action=\"search.php\" method=\"get\" id=\"search_form\">
<input type=\"text\" id=\"keywords\" name=\"keywords\" size=\"128\" class=\"searchbox\" value=\"$defaultText\">
<select id=\category\" name=\"category\" class=\"searchbox\">
";
createCategoryList();
echo '
</select>
<input type="submit" value="Search" class="button" />
</form>
</div>
</header>
';
2頁顯示結果
<?php
session_start();
include ("includes/search_func.php");
if (isset($_GET['keywords'])){
$keywords = mysql_real_escape_string(htmlentities(trim($_GET['keywords'])));
$errors = array();
if(empty($keywords)){
$errors[] = 'Please enter a search term';
}else if(strlen($keywords)<3){
$errors[] = 'Your search term must be at least 3 characters long';
}else if(search_results($keywords) == false){
$errors[] = 'Your search for'.$keywords.' returned no results';
}
if(empty($errors)){
function diplay_results($keywords){
$results = search_results($keywords);
$results_num = count($results);
foreach($results as $result){
echo '
<div id="game_result">
<a href= game_page.php?game_id='.$result['game_id'].'><img src= '.$result['image_location'].' id="image" /></a>
<div id="main_title">
<a href= game_page.php?game_id='.$result['game_id'].'><h2 id="game_title">'.$result['title'].' </h2></a>
<h3 id="platform">for '.$result['platform'].'</h3>
</div>
<p id=game_description>'.$result['description'].'</p>
<div id="right_side">
<h4 id="rating">'.$result['rating'].'</h4>
</div>
<hr id="hr"/>
</div>
';
}
}
}else{
foreach($errors as $error){
echo $error, '<br />';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Search</title>
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/search.css">
</head>
<body>
<div id="wrapper">
<div id="main_aside">
//filter navigation will go here
</div>
<div id="main_section" class="header">
<?php diplay_results($keywords)?>
</div>
</div>
</body>
</html>
第3頁查詢數據庫
<?php
include 'includes/connect.php';
function search_results($keywords){
$returned_results = array();
$where = "";
$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);
foreach($keywords as $key=>$keyword){
$where .= "title LIKE '%$keyword%'";
if($key != ($total_keywords - 1)){
$where .= " AND ";
}
}
$results ="SELECT * FROM games WHERE $where ";
echo $results;
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;
if($results_num === 0){
return false;
}else{
while($row = mysql_fetch_assoc($results)){
$returned_results[] = array(
'game_id' => $row['game_id'],
'title' => $row['title'],
'platform' => $row['platform'],
'rating' => $row['rating'],
'image_location' => $row['image_location'],
'description' => $row['description'],
);
}
return $returned_results;
}
}
?>
所以,如果它不是清楚,我想允許用戶搜索一個關鍵詞,然後它會從數據庫中顯示任何包含標題中關鍵字的遊戲(我已經有了這部分工作)。現在無法弄清楚的是如何讓用戶過濾他們的結果,讓他們說只有動作遊戲,以及讓我們說一個特定的平臺等等。
就像我說的,只有這樣做了一個月,所以如果我做錯了什麼,不要猶豫,叫我一個白癡,但請幫助。任何幫助將不勝感激即使鏈接教程或書建議。謝謝。
謝謝,你的迴應真的讓我走上正軌。截至目前,我只能排序,但我已經知道如何讓我的過濾工作。我將在稍後應用JavaScript,因此我不必每次都訪問數據庫。 – hugoismyname