Enhance attribute fetching to include data type and improve value sorting

This commit is contained in:
Fabian Schieder 2026-03-30 23:05:26 +02:00
parent 1ef0f7c8bf
commit 876d9ca9e3

View File

@ -20,13 +20,12 @@ if (isset($categoriesConfig[$currentCategory])) {
// Fetch available attributes for the category if selected
$attributes = [];
if ($catId) {
// Only show attributes related to the products in the active category
// Show attributes related to the active category
$stmtAttr = $conn->prepare("
SELECT DISTINCT a.attributeID, a.name, a.unit
SELECT a.attributeID, a.name, a.unit, a.dataType
FROM attributes a
JOIN productAttributes pa ON a.attributeID = pa.attributeID
JOIN products p ON pa.productID = p.productID
WHERE p.categoryID = ?
JOIN categoryAttributes ca ON a.attributeID = ca.attributeID
WHERE ca.categoryID = ?
ORDER BY a.name
");
$stmtAttr->bind_param("i", $catId);
@ -37,12 +36,10 @@ if ($catId) {
}
$stmtAttr->close();
} else {
// Or all active across any product?
// User typically filters by category first. But if 'all', maybe just get top attributes.
// If no category selected, just fall back
$resAttr = $conn->query("
SELECT DISTINCT a.attributeID, a.name, a.unit
SELECT a.attributeID, a.name, a.unit, a.dataType
FROM attributes a
JOIN productAttributes pa ON a.attributeID = pa.attributeID
ORDER BY a.name LIMIT 5
");
if ($resAttr) {
@ -75,7 +72,6 @@ if ($catId) {
FROM productAttributes pa
JOIN products p ON pa.productID = p.productID
WHERE p.categoryID = ? AND pa.attributeID = ?
ORDER BY pa.valueString, pa.valueNumber
");
$vStmt->bind_param("ii", $catId, $attrId);
} else {
@ -83,7 +79,6 @@ if ($catId) {
SELECT DISTINCT valueString, valueNumber, valueBool
FROM productAttributes
WHERE attributeID = ?
ORDER BY valueString, valueNumber
");
$vStmt->bind_param("i", $attrId);
}
@ -91,12 +86,28 @@ if ($catId) {
$vRes = $vStmt->get_result();
$values = [];
while ($vRow = $vRes->fetch_assoc()) {
if ($vRow['valueString'] !== null) $values[] = $vRow['valueString'];
elseif ($vRow['valueNumber'] !== null) $values[] = $vRow['valueNumber'];
elseif ($vRow['valueBool'] !== null) $values[] = $vRow['valueBool'] ? 'Ja' : 'Nein';
if ($attr['dataType'] === 'boolean' || $vRow['valueBool'] !== null) {
$val = $vRow['valueBool'] ? 'Ja' : 'Nein';
if (!in_array($val, $values)) $values[] = $val;
} elseif ($attr['dataType'] === 'number' || $vRow['valueNumber'] !== null) {
$val = $vRow['valueNumber'];
// strip trailing zero for decimals if desired, e.g. 5.00 -> 5
$val = rtrim(rtrim((string)$val, '0'), '.');
if (!in_array($val, $values)) $values[] = $val;
} elseif ($vRow['valueString'] !== null) {
$val = $vRow['valueString'];
if (!in_array($val, $values)) $values[] = $val;
}
}
$vStmt->close();
// Sort values
if ($attr['dataType'] === 'number') {
usort($values, function($a, $b) { return (float)$a <=> (float)$b; });
} else {
sort($values);
}
if (empty($values)) continue;
$paramName = "attr_" . $attrId;
$selectedValue = isset($_GET[$paramName]) ? $_GET[$paramName] : '';
@ -135,3 +146,4 @@ if ($catId) {
<?php endif; ?>