PHP多个过滤链接
我正在使用下拉菜单为mySQL数据库创建一个过滤系统。当我使用一个过滤器进行搜索时,我的系统可以工作,但我很难让它们堆叠起来。我使用GET检索他们选择的过滤器,并尝试保存这些过滤器并将它们连接到下一个选择 现在,我的代码获得了所有可能的变量:PHP多个过滤链接,php,html,mysql,database,filter,Php,Html,Mysql,Database,Filter,我正在使用下拉菜单为mySQL数据库创建一个过滤系统。当我使用一个过滤器进行搜索时,我的系统可以工作,但我很难让它们堆叠起来。我使用GET检索他们选择的过滤器,并尝试保存这些过滤器并将它们连接到下一个选择 现在,我的代码获得了所有可能的变量: $artistTag = $_GET['artistTag']; $typeTag = $_GET['typeTag']; $priceTag = $_GET['priceTag']; $sizeTag = $_GET['sizeTag'];` 这就是我
$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 Mitsrakis非常好心,帮助我获取代码,以获取先前搜索的标记,并将它们连接到附加搜索标记的前缀中。问题是,当添加新的搜索标记时,它不会覆盖同一类别的标记。它似乎也没有适当地添加问号和符号。这是我当前的代码(简化):
您可以将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();
}
define('DB_HOST','localhost');
定义('DB_NAME'、'your_database');
定义('DB_USER','your_username');
定义(‘DB_密码’、‘您的_密码’);
$vars=数组('artistTag'、'typeTag'、'priceTag'、'sizeTag');
$query=“从artData中选择*”;
$filter=array();
$binds=array();
$i=0;
$href='';
}
}
}
您可以将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();
}
define('DB_HOST','localhost');
定义('DB_NAME'、'your_database');
定义('DB_USER','your_username');
定义(‘DB_密码’、‘您的_密码’);
$vars=数组('artistTag'、'typeTag'、'priceTag'、'sizeTag');
$query=“从artData中选择*”;
$filter=array();
$binds=array();
$i=0;
$href='';
}
}
}
为什么不直接使用$\u GET
global?在哪里?我试图使连接以前的结果更容易。为什么不直接使用$\u GET
全局?在哪里?我试图使连接以前的结果更容易。感谢您的快速响应!这看起来正是我想要做的。我现在正在努力实现它,我会让你知道它的进展。是的,我想这正是你需要的。如果我遗漏了任何内容,请修改它以获得您想要的结果。检查更新的答案:foreach($result as$row)完美!这对打印结果起了作用。我现在正在正确使用$href变量。一旦它正常工作,我会将答案标记为已解决!感谢您的快速回复!这看起来正是我想要做的。我现在正在努力实现它,我会让你知道它的进展。是的,我想这正是你需要的。如果我遗漏了任何内容,请修改它以获得您想要的结果。检查更新的答案:foreach($result as$row)完美!这对打印结果起了作用。我现在正在正确使用$href变量。一旦它正常工作,我会将答案标记为已解决!
$values = array(
'artistTag'=>array(
array(
'url'=>'ricker',
'text'=>'Bruce Ricker',
),
array(
'url'=>'picasso',
'text'=>'Pablo Picasso',
),
array(
'url'=>'dali',
'text'=>'Salvador Dalí',
),
),
'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>';
}
}
}