我正在使用 URL 查询字符串来过滤 MySQL 搜索结果。当用户单击其中一个链接时,查询连同另一个变量被传递给应用程序,然后应用程序构建并执行对数据库的 SQL 查询。
过滤有效 - 用户可以按流派、平台进行过滤,也可以同时进行排序,但问题是每次点击链接时,都会在$query
.
例如,如果用户在示例中输入并选择作为过滤器,流派操作,则查询如下所示:
keywords=example&genre=action
但是假设他们然后单击冒险类型,则查询如下所示:
keywords=example&genre=action&genre=adventure
如果已经设置了变量,有没有办法让查询替换它?
$query = mysql_real_escape_string(htmlentities(trim($_SERVER[QUERY_STRING])));
<div id="filter_nav">
<ul id="nav_form">
<li><h3 id="h3">Genre: </h3>
<li><a href="search.php?'.$query.'&genre=Fighting">Fighting</a></li>
<li><a href="search.php?'.$query.'&genre=Role-Playing">Role-Playing</a></li>
<li><a href="search.php?'.$query.'&genre=Action">Action</a></li>
</ul>
<ul id="nav_form">
<li><h3 id="h3">Platform: </h3>
<li><a href="search.php?'.$query.'&platform=Playstation 3">PS3</a></li>
<li><a href="search.php?'.$query.'&platform=xbox 360">Xbox 360</a></li>
<li><a href="search.php?'.$query.'&platform=Gamecube">Gamecube</a></li>
</ul>
</div>
';
echo '
<ul id="sorting_form">
<li><h3 id="h3">SORT BY: </h3>
<li><a href="search.php?'.$query.'&order=title">Title</a></li>
<li><a href="search.php?'.$query.'&order=release_date">Date</a></li>
<li><a href="search.php?'.$query.'&order=rating">Rating</a></li>
</ul>
';
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 ";
}
}
if (isset($_GET['platform']) && !empty($_GET['platform'])){
$platform = mysql_real_escape_string(htmlentities(trim($_GET['platform'])));
$where .= " AND platform='$platform'";
}
if (isset($_GET['genre']) && !empty($_GET['genre'])){
$genre = mysql_real_escape_string(htmlentities(trim($_GET['genre'])));
$where .= " AND genre='$genre'";
}
if (isset($_GET['order']) && !empty($_GET['order'])){
$order = mysql_real_escape_string(htmlentities(trim($_GET['order'])));
$where .= " ORDER BY $order DESC";
}
$results ="SELECT * FROM games WHERE $where ";