【发布时间】: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í</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>
【问题讨论】:
-
为什么不直接使用
$_GETglobal? -
在哪里?我试图让连接以前的结果更容易。
标签: php html mysql database filter