Collections/config.php~
2025-11-11 17:00:02 +01:00

46 lines
1.3 KiB
PHP
Executable File

<?php
require __DIR__ . '/vendor/autoload.php';
// Database configuration
$dbFile = __DIR__.'/collections.sqlite';
try {
$db = new PDO('sqlite:' . $dbFile);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Create tables if they don't exist
$db->exec('CREATE TABLE IF NOT EXISTS categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE
)');
$db->exec('CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT,
category_id INTEGER,
FOREIGN KEY (category_id) REFERENCES categories(id)
)');
} catch (PDOException $e) {
error_log("Database connection failed: " . $e->getMessage());
die("Database connection failed: could not open database file. Please check server logs for details.");
}
// Mustache setup
$mustache = new Mustache_Engine([
'loader' => new Mustache_Loader_FilesystemLoader(__DIR__ . '/templates'),
]);
// Helper function to get categories
function getCategories($db) {
try {
$stmt = $db->query('SELECT id, name FROM categories ORDER BY name');
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Error fetching categories: " . $e->getMessage());
return [];
}
}
?>