47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
/**
|
|
* getTreePath.php - Liefert den Pfad von einem Knoten bis zur Wurzel
|
|
*
|
|
* Parameter (GET):
|
|
* - id: Treeview-ID des Zielknotens
|
|
*
|
|
* Rückgabe: JSON-Array mit IDs von Root bis Zielknoten (inklusive)
|
|
* Beispiel: [98106, 98107, 98113, 98115]
|
|
*/
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
include "db_connection.php";
|
|
|
|
$conn = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
|
|
if (mysqli_connect_errno()) {
|
|
echo json_encode([]);
|
|
exit();
|
|
}
|
|
mysqli_set_charset($conn, "utf8");
|
|
|
|
$targetId = isset($_GET['id']) ? intval($_GET['id']) : 0;
|
|
if ($targetId === 0) {
|
|
echo json_encode([]);
|
|
exit();
|
|
}
|
|
|
|
// Pfad aufbauen: vom Zielknoten nach oben bis Root (parent_id = 0 oder leer)
|
|
$path = [];
|
|
$curId = $targetId;
|
|
$limit = 20; // Schutz vor Endlosschleife bei defekten Daten
|
|
|
|
while ($curId > 0 && $limit-- > 0) {
|
|
$res = mysqli_query($conn, "SELECT id, parent_id FROM Treeview WHERE id = $curId LIMIT 1");
|
|
if (!$res) break;
|
|
$row = mysqli_fetch_assoc($res);
|
|
if (!$row) break;
|
|
|
|
array_unshift($path, intval($row['id'])); // vorne einfügen → Root zuerst
|
|
$parentId = intval($row['parent_id']);
|
|
if ($parentId === 0) break;
|
|
$curId = $parentId;
|
|
}
|
|
|
|
mysqli_close($conn);
|
|
echo json_encode($path, JSON_UNESCAPED_UNICODE);
|
|
?>
|