I am working on creating a filtering system for a mySQL database using dropdown menus. My system works when I search using one filter but I'm having a hard time getting them to stack. I am using GET to retrieve the filter they choose and I'm trying to save these and concatenate them to the next selection.
Right now my code gets all the variables possible:
$artistTag = $_GET['artistTag'];
$typeTag = $_GET['typeTag'];
$priceTag = $_GET['priceTag'];
$sizeTag = $_GET['sizeTag'];`
And here's where I'm having the issue:
$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);
}
I'm trying to append the links for additional filters with the previous filters using $filter.
<a href="index.php?artistTag='.$row[artistTag].''.$filter.'">'.$row[artist].'</a>
That way the next URL generated would be
index.php?artistTag=picasso&sizeTag=large
This works occasionally but if I select the same filter again it doesn't replace the old one. I am open to suggestions on how to do this but it's important that the filters are applied through the drop-down menu links and not a form. I've tried searching the forums and I can't find a solution thus far. Thank you for taking the time to help!
Edit: Kostas Mitsarakis was kind enough to help me get the code to get the tags previously searched and concatenate them into a prefix for the additional search tags. The problem is when new search tags are added it doesn't overwrite the tag of the same category. It also seems to not add the question mark and ampersand appropriately. This is my current code (simplified):
<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?