简介:
我正在做一个非常简单的电子商务网站,我陷入了刻面导航问题,许多网站在网上使用这种技术,用户可以缩小选择,通过选择品牌,颜色,大小或价格范围等。我使用原始PHP,只是一个测试运行我的网站。
我在左边显示了我的过滤器,比如说(品牌/类型/颜色),我给了它们一个锚
品牌

 <a href="index.php?brand=nike">NIKE</a>

颜色
    <a href="index.php?color=blue">Blue</a>
    <a href="index.php?color=white">White</a>

类型
    <a href="index.php?type=shoe">Shoe</a>
    <a href="index.php?type=shirt">Shirt</a>

如您所见,我正在一个接一个地传递我的值,在我的PHP代码中,我使用下面的查询
$sql_query = "SELECT * FROM products ";


// add fileters e.g. from $_GET array
$i = 0;

foreach($filter_array as $filter -> $filter_value)
{
   if( ! $i)
   {
      $sql_query .= "WHERE ";
   }
   else
   {
      $sql_query .= " AND ";
   }

   $sql_query .= $fliter . " = '" . $filter_value . "'";

   $i++;
}

问题
如何在URL中获取多个值并读取它们并在select查询中使用它们。请建议:
 www.abc.com/index.php?brand=nike&color=blue&type=shoe

附加问题:)
在使用了以上过滤器之后,如何使用页面中的价格范围?

最佳答案

如果要使用get values创建sql查询,可以使用以下代码:
我用过“echo$query”。$condition;”检查查询

$cnt = count($_GET);
$query = "SELECT * FROM table";
$condition = "";
if($cnt > 0){
    $condition .= " WHERE ";
}
$i = 0;
foreach($_GET as $key => $value){
    $condition .= $key."=".$value;
    $i++;
    if($i < ($cnt)){
            $condition .= " AND ";
    }

}
echo $query . $condition;

关于php - 将值传递到URL并在搜索中使用它们,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22015151/

10-12 12:27
查看更多