【问题标题】:How to stop URL query string from repeating?如何阻止 URL 查询字符串重复?
【发布时间】:2012-05-14 21:09:38
【问题描述】:

我正在使用 URL 查询字符串来过滤 MySQL 搜索结果。当用户单击其中一个链接时,查询连同另一个变量将传递给应用程序,然后应用程序构建并执行对数据库的 SQL 查询。

过滤有效 - 用户可以按流派、平台进行过滤,也可以同时进行排序,但问题是每次点击链接时都会在$query 末尾添加另一个变量。

例如,如果用户输入 example 并选择作为过滤器,流派 action,则查询如下所示:

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 ";

【问题讨论】:

标签: php mysql url parameter-passing


【解决方案1】:

使用您的代码,可以使用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
}
?>

【讨论】:

  • 很有趣,我会尝试并回复您。谢谢。
【解决方案2】:

修改每个&lt;a&gt; 标签,如: &lt;a href="search.php?'.$query.'&amp;genre=Fighting"&gt;Fighting&lt;/a&gt;&lt;a href="search.php?&amp;genre=Fighting"&gt;Fighting&lt;/a&gt;

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

【讨论】:

  • 这不会让你失去所有其他参数吗?
  • is there a way to get the query to replace the variable if its already set?
猜你喜欢
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多