add api directory
This commit is contained in:
35
api/parents.php
Normal file
35
api/parents.php
Normal 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));
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user