如何阻止 URL 查询字符串重复


How to stop URL query string from repeating?

我正在使用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: &nbsp;</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: &nbsp;</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: &nbsp;</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 ";

使用您的代码,可以使用parse_url解构查询字符串,并使用每个链接的http_build_query重建它。

但是,就个人而言,我只会选择带有 3 个选择框的表单,其中预先选择了先前选择的值。

您可以将所有选择选项放在一个多维数组中,然后执行双循环。

例:

<?php
$options = array(
  "genre" => array("Fighting", "Role-Playing", ...),
  ...
);
foreach $options as $key => $value)
{
?>
<select name="<?php echo $key; ?>">
<?php 
  foreach ($value as $item)
  {
    // echo option for item and mark it selected if necessary
  }
?>
</select>
<?php
}
?>

修改每个<a>标签,如下所示: <a href="search.php?'.$query.'&genre=Fighting">Fighting</a><a href="search.php?&genre=Fighting">Fighting</a>

即删除'.$query.'部分。