【问题标题】:PHP Multiple Filtering LinksPHP 多重过滤链接
【发布时间】:2016-02-26 04:31:08
【问题描述】:

我正在使用下拉菜单为 mySQL 数据库创建过滤系统。当我使用一个过滤器进行搜索时,我的系统可以正常工作,但我很难让它们堆叠起来。我正在使用 GET 来检索他们选择的过滤器,我正在尝试保存这些并将它们连接到下一个选择。

现在我的代码可以获得所有可能的变量:

$artistTag = $_GET['artistTag'];
$typeTag = $_GET['typeTag'];
$priceTag = $_GET['priceTag'];
$sizeTag = $_GET['sizeTag'];`

这就是我遇到问题的地方:

$query = "SELECT * FROM artData";
$conditions = array();

$filter = '';

if ($artistTag != "") {
    $conditions[] = "artistTag='$artistTag'";
    $artistFilter = "artistTag=".$artistTag."";
    $filter = $artistFilter;
}
if ($typeTag != "") {
    $conditions[] = "typeTag='$typeTag'";
    $typeFilter = "typeTag=".$typeTag."";
    $filter .= "&".$typeFilter."";
}
if ($priceTag != "") {
    $conditions[] = "priceTag='$priceTag'";
    $priceFilter = "priceTag=".$priceTag."";
    $filter .= "&".$priceFilter."";
}
if ($sizeTag != "") {
    $conditions[] = "sizeTag='$sizeTag'";
    $sizeFilter = "sizeTag=".$sizeTag."";
    $filter .= "&".$sizeFilter."";
}

$sql = $query;
if (count($conditions) > 0) {
    $sql .= " WHERE " . implode(' AND ', $conditions);
}

我正在尝试使用 $filter 将其他过滤器的链接附加到以前的过滤器中。

<a href="index.php?artistTag='.$row[artistTag].''.$filter.'">'.$row[artist].'</a>

这样生成的下一个 URL 将是

index.php?artistTag=picasso&sizeTag=large

这偶尔会起作用,但如果我再次选择相同的过滤器,它不会替换旧的过滤器。我愿意接受有关如何执行此操作的建议,但重要的是通过下拉菜单链接而不是表单应用过滤器。我已经尝试搜索论坛,但到目前为止我找不到解决方案。感谢您抽出宝贵时间提供帮助!

编辑: Kostas Mitsarakis 非常友好地帮助我获取代码以获取先前搜索的标签并将它们连接成附加搜索标签的前缀。问题是当添加新的搜索标签时,它不会覆盖同一类别的标签。它似乎也没有适当地添加问号和&符号。这是我当前的代码(简化):

<DOCTYPE html>
<?php
define('DB_HOST', 'host');
define('DB_NAME', 'name');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');

$vars = array('artistTag', 'typeTag', 'priceTag', 'sizeTag');
$query = "SELECT * FROM artData ";
$filter = array();
$binds = array();
$i = 0;

$href = '<a href="index.php';

foreach($vars as $key => $value) { //For every attribute you want to take from $_GET
    if ($_GET[$value] != '') { //If the value is not empty
        $filter[] = $value." = :".$value; //Add it for WHERE clause
        $binds[':'.$value] = $_GET[$value]; //And also prepare the value
        if ($i == 0) { //This is for link creation
            $href .= '?'.$value.'='.$_GET[$value];
        } else {
            $href .= '&'.$value.'='.$_GET[$value];
        }
        $i++;
    }
}

try {
    //Make your connection handler to your database
    $conn = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));

    if (!empty($filter)) { //If one or more $_GET variables are not empty
        $query .= " WHERE ".implode(' AND ', $filter); //Concatenate at Where clause
    }

    $stmt = $conn->prepare($query); //Prepare the statement
    $stmt->execute($binds); //And bind the values
    $result = $stmt->fetchAll();

    foreach($result as $row) {
        print ''.$row[artist].' - '.$row[id].' - '.$row[priceTag].'</br>';
    }

    $stmt = null; //Close the connection
    $conn = null;
} catch(PDOException $e) {
    echo $e->getMessage();
    die();
}

?>
<html>
    <head>
        <title>debug mode</title>
    </head>
    <body>
        <?php
        print ''.$href.'artistTag=ricker">Bruce Ricker</a></br>
        '.$href.'artistTag=picasso">Pablo Picasso</a></br>
        '.$href.'artistTag=dali">Salvador Dal&iacute</a></br></br>

        '.$href.'typeTag=drawing">Drawing</a></br>
        '.$href.'typeTag=painting">Painting</a></br></br>

        '.$href.'sizeTag=large">Large</a></br>
        '.$href.'sizeTag=medium">Medium</a></br>
        '.$href.'sizeTag=small">Small</a></br></br>';


        ?>
    </body>
</html>

【问题讨论】:

  • 为什么不直接使用$_GET global?
  • 在哪里?我试图让连接以前的结果更容易。

标签: php html mysql database filter


【解决方案1】:

您可以将 PDO 与准备好的语句一起用于您的 SELECT 查询,并使用一个帮助器数组来创建链接。

define('DB_HOST', 'localhost');
define('DB_NAME', 'your_database');
define('DB_USER', 'your_username');
define('DB_PASSWORD', 'your_password');

$vars = array('artistTag', 'typeTag', 'priceTag', 'sizeTag');
$query = "SELECT * FROM artData ";
$filter = array();
$binds = array();
$i = 0;

$href = '<a href="index.php';

foreach($vars as $key => $value) { //For every attribute you want to take from $_GET
    if ($_GET[$value] != '') { //If the value is not empty
        $filter[] = $value." = :".$value; //Add it for WHERE clause
        $binds[':'.$value] = $_GET[$value]; //And also prepare the value
        if ($i == 0) { //This is for link creation
            $href .= '?'.$value.'='.$_GET[$value];
        } else {
            $href .= '&'.$value.'='.$_GET[$value];
        }
        $i++;
    }
}

try {
    //Make your connection handler to your database
    $conn = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));

    if (!empty($filter)) { //If one or more $_GET variables are not empty
        $query .= " WHERE ".implode(' AND ', $filter); //Concatenate at Where clause
    }

    $stmt = $conn->prepare($query); //Prepare the statement
    $stmt->execute($binds); //And bind the values
    $result = $stmt->fetchAll();

    foreach($result as $row) {
        echo $row['id'].'<br />';
    }

    $stmt = null; //Close the connection
    $conn = null;
} catch(PDOException $e) {
    echo $e->getMessage();
    die();
}

编辑:

要检查 get 变量是否正在使用,您可以使用以下内容(需要修改)。

$values = array(
    'artistTag'=>array(
        array(
            'url'=>'ricker',
            'text'=>'Bruce Ricker',
        ),
        array(
            'url'=>'picasso',
            'text'=>'Pablo Picasso',
        ),
        array(
            'url'=>'dali',
            'text'=>'Salvador Dal&iacute',
        ),
    ),
    'typeTag'=>array(
        array(
            'url'=>'drawing',
            'text'=>'Drawing',
        ),
        array(
            'url'=>'painting',
            'text'=>'Painting',
        ),
    ),
    'sizeTag'=>array(
        array(
            'url'=>'large',
            'text'=>'Large',
        ),
        array(
            'url'=>'medium',
            'text'=>'Medium',
        ),
        array(
            'url'=>'small',
            'text'=>'Small',
        ),
    ),
);

foreach($values as $key => $value) {
    if (empty($_GET[$key])) {
        foreach($value as $s_key => $s_value) {
            echo $href.'?'.$key.'='.$s_value['url'].'>'.$s_value['text'].'</a>';
        }
    }
}

【讨论】:

  • 感谢您的快速回复!这看起来正是我想要做的。我正在努力实施它,我会告诉你进展如何。
  • 是的,我认为这正是您所需要的。如果我遗漏了任何内容,请对其进行修改以获得您想要的结果。
  • 检查更新的答案:foreach($result as $row)
  • 完美!这适用于打印结果。我现在正在正确使用 $href 变量。
  • 一旦我让它正常工作,我会将答案标记为已解决!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 2019-07-26
  • 2018-07-08
  • 2018-08-30
  • 1970-01-01
相关资源
最近更新 更多