gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:43:19 25 4
gpt4 key购买 nike

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

我的数据库表是这样的:

game_id title   developer   publisher   genre   release_date    platform          
rating image_location description

我想让用户在进行搜索后过滤结果,最好不要刷新页面(只编写了一个月的代码,但对 jquery 和 ajax 有一般的了解)。

这是我的代码,我使用了 include,所以搜索栏、搜索结果和查询页面是分开的。

第 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>

第三页查询数据库

<?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;
}
}

?>

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

就像我说的,我只做了一个月,所以如果我做错了什么,请毫不犹豫地称我为白痴,但请帮忙。任何帮助都将不胜感激,甚至链接到教程或书籍推荐。谢谢。

最佳答案

你的做法是对的。根据出版商名称或开发者、类型、发布日期过滤搜索结果,

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

如何选择接口(interface)将由您的需求规范决定。请注意,在所有情况下,只有您的 WHERE 子句会发生变化。

关于php - 使用 php 和/或 jquery 过滤 mysql 搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10373863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com