使用php和/或jquery过滤mysql搜索结果

时间:2012-04-29 16:37:13

标签: php jquery mysql filter search-engine

我为我的网站构建了一个简单的搜索引擎,它的工作方式是当用户输入一个关键字时,它会查询数据库并显示结果。

我的数据库表如下所示:

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\"> &nbsp;
            <select id=\category\" name=\"category\" class=\"searchbox\"> 
";
createCategoryList();
echo '
            </select> &nbsp;
            <input type="submit" value="Search" class="button" /> &nbsp;
            </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'].'&nbsp; &nbsp;</h2></a>
                    <h3 id="platform">for  &nbsp;'.$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;
}   
}

?>

因此,如果我不清楚我想允许用户搜索关键词,那么它将从数据库中显示标题中包含关键词的任何游戏(我已经使这部分工作)。现在无法弄清楚如何让用户过滤他们的结果,让我们说只有动作游戏,以及让我们说某个平台等等。

就像我说我只做了一个月这样做,所以如果我做错了不要犹豫,叫我白痴,但请帮忙。任何帮助将不胜感激,甚至链接到教程或书籍推荐。谢谢。

1 个答案:

答案 0 :(得分:0)

你的方法是正确的。根据发布商名称或开发者,流派,发布日期

过滤搜索结果

1)您可以拥有另一个“高级搜索”页面,其中您可以为这些属性插入HTML,并只需调整查询的WHERE子句。因此,将有两个用于搜索的页面 - 一个简单的搜索,允许用户仅根据标题名称进行过滤,并说出类别和高级搜索页面,以根据其他属性进行过滤。
2)您甚至可以选择在基本搜索页面上提供选项,以便根据其他参数进行过滤。

如何选择界面将是您的需求规范所要求的决定。请注意,在所有情况下,只有WHERE子句会发生变化。