add api directory

This commit is contained in:
tibo
2020-03-01 13:55:42 +00:00
parent be0fde7439
commit b3c7a26e3b
3 changed files with 122 additions and 0 deletions

35
api/parents.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
header('Content-Type: application/json');
try{
$pdo = new PDO('sqlite:../data/base.sqlite');
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // ERRMODE_WARNING | ERRMODE_EXCEPTION | ERRMODE_SILENT
} catch(Exception $e) {
echo "Impossible d'accéder à la base de données SQLite : ".$e->getMessage();
die();
}
$where_clause = 'WHERE 1 = 1 ';
$where_clause .= 'AND item = '.$_REQUEST['item'];
$sql = '
WITH arbre (id,level, path) AS(
SELECT parent, 0, '['|| tree.parent || ']'
FROM tree
WHERE_CLAUSE
UNION ALL
SELECT parent, arbre.level+1, arbre.path||'['||tree.parent||']'
FROM tree
INNER JOIN arbre ON arbre.id = tree.item
) SELECT arbre.*, item.code, item.title FROM arbre
INNER JOIN item ON arbre.id = item.id
ORDER BY path DESC
';
$query = $pdo->prepare(str_replace('WHERE_CLAUSE',$where_clause,$sql));
$query->execute();
echo json_encode( $query->fetchAll(PDO::FETCH_ASSOC));
?>