From 876d9ca9e309a4253525c5448f54bf268d6a2b66 Mon Sep 17 00:00:00 2001 From: Fabian Schieder Date: Mon, 30 Mar 2026 23:05:26 +0200 Subject: [PATCH] Enhance attribute fetching to include data type and improve value sorting --- attrbar.php | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/attrbar.php b/attrbar.php index 7d02885..0991338 100644 --- a/attrbar.php +++ b/attrbar.php @@ -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) { +