Enhance attribute fetching to include data type and improve value sorting
This commit is contained in:
parent
1ef0f7c8bf
commit
876d9ca9e3
40
attrbar.php
40
attrbar.php
@ -20,13 +20,12 @@ if (isset($categoriesConfig[$currentCategory])) {
|
|||||||
// Fetch available attributes for the category if selected
|
// Fetch available attributes for the category if selected
|
||||||
$attributes = [];
|
$attributes = [];
|
||||||
if ($catId) {
|
if ($catId) {
|
||||||
// Only show attributes related to the products in the active category
|
// Show attributes related to the active category
|
||||||
$stmtAttr = $conn->prepare("
|
$stmtAttr = $conn->prepare("
|
||||||
SELECT DISTINCT a.attributeID, a.name, a.unit
|
SELECT a.attributeID, a.name, a.unit, a.dataType
|
||||||
FROM attributes a
|
FROM attributes a
|
||||||
JOIN productAttributes pa ON a.attributeID = pa.attributeID
|
JOIN categoryAttributes ca ON a.attributeID = ca.attributeID
|
||||||
JOIN products p ON pa.productID = p.productID
|
WHERE ca.categoryID = ?
|
||||||
WHERE p.categoryID = ?
|
|
||||||
ORDER BY a.name
|
ORDER BY a.name
|
||||||
");
|
");
|
||||||
$stmtAttr->bind_param("i", $catId);
|
$stmtAttr->bind_param("i", $catId);
|
||||||
@ -37,12 +36,10 @@ if ($catId) {
|
|||||||
}
|
}
|
||||||
$stmtAttr->close();
|
$stmtAttr->close();
|
||||||
} else {
|
} else {
|
||||||
// Or all active across any product?
|
// If no category selected, just fall back
|
||||||
// User typically filters by category first. But if 'all', maybe just get top attributes.
|
|
||||||
$resAttr = $conn->query("
|
$resAttr = $conn->query("
|
||||||
SELECT DISTINCT a.attributeID, a.name, a.unit
|
SELECT a.attributeID, a.name, a.unit, a.dataType
|
||||||
FROM attributes a
|
FROM attributes a
|
||||||
JOIN productAttributes pa ON a.attributeID = pa.attributeID
|
|
||||||
ORDER BY a.name LIMIT 5
|
ORDER BY a.name LIMIT 5
|
||||||
");
|
");
|
||||||
if ($resAttr) {
|
if ($resAttr) {
|
||||||
@ -75,7 +72,6 @@ if ($catId) {
|
|||||||
FROM productAttributes pa
|
FROM productAttributes pa
|
||||||
JOIN products p ON pa.productID = p.productID
|
JOIN products p ON pa.productID = p.productID
|
||||||
WHERE p.categoryID = ? AND pa.attributeID = ?
|
WHERE p.categoryID = ? AND pa.attributeID = ?
|
||||||
ORDER BY pa.valueString, pa.valueNumber
|
|
||||||
");
|
");
|
||||||
$vStmt->bind_param("ii", $catId, $attrId);
|
$vStmt->bind_param("ii", $catId, $attrId);
|
||||||
} else {
|
} else {
|
||||||
@ -83,7 +79,6 @@ if ($catId) {
|
|||||||
SELECT DISTINCT valueString, valueNumber, valueBool
|
SELECT DISTINCT valueString, valueNumber, valueBool
|
||||||
FROM productAttributes
|
FROM productAttributes
|
||||||
WHERE attributeID = ?
|
WHERE attributeID = ?
|
||||||
ORDER BY valueString, valueNumber
|
|
||||||
");
|
");
|
||||||
$vStmt->bind_param("i", $attrId);
|
$vStmt->bind_param("i", $attrId);
|
||||||
}
|
}
|
||||||
@ -91,12 +86,28 @@ if ($catId) {
|
|||||||
$vRes = $vStmt->get_result();
|
$vRes = $vStmt->get_result();
|
||||||
$values = [];
|
$values = [];
|
||||||
while ($vRow = $vRes->fetch_assoc()) {
|
while ($vRow = $vRes->fetch_assoc()) {
|
||||||
if ($vRow['valueString'] !== null) $values[] = $vRow['valueString'];
|
if ($attr['dataType'] === 'boolean' || $vRow['valueBool'] !== null) {
|
||||||
elseif ($vRow['valueNumber'] !== null) $values[] = $vRow['valueNumber'];
|
$val = $vRow['valueBool'] ? 'Ja' : 'Nein';
|
||||||
elseif ($vRow['valueBool'] !== null) $values[] = $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();
|
$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;
|
if (empty($values)) continue;
|
||||||
$paramName = "attr_" . $attrId;
|
$paramName = "attr_" . $attrId;
|
||||||
$selectedValue = isset($_GET[$paramName]) ? $_GET[$paramName] : '';
|
$selectedValue = isset($_GET[$paramName]) ? $_GET[$paramName] : '';
|
||||||
@ -135,3 +146,4 @@ if ($catId) {
|
|||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user