Initial commit
This commit is contained in:
44
api.php
Normal file
44
api.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
// api.php - Gibt die News als JSON an die Website aus
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
try {
|
||||
$db = new PDO('sqlite:news_database.sqlite');
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// Tabelle sichern
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS news (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
date TEXT,
|
||||
department TEXT,
|
||||
title TEXT,
|
||||
content TEXT,
|
||||
full_text TEXT,
|
||||
image TEXT,
|
||||
link TEXT
|
||||
)");
|
||||
|
||||
$stmt = $db->query('SELECT * FROM news ORDER BY id DESC');
|
||||
$news = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
if (count($news) === 0) {
|
||||
$news = [
|
||||
[
|
||||
"id" => 1,
|
||||
"date" => date("d.m.Y"),
|
||||
"department" => "System",
|
||||
"title" => "Willkommen beim neuen News-System",
|
||||
"content" => "Die Datenbank ist bereit. Klicke auf Weiterlesen, um zu sehen, wie das Popup funktioniert.",
|
||||
"full_text" => "Herzlichen Glückwunsch! Die SQLite-Datenbank wurde erfolgreich eingerichtet.\n\nHier im Popup kannst du beliebig lange Texte schreiben. Absätze aus dem Admin-Formular werden hier sauber übernommen.\n\nUm echte News zu erstellen, gehe einfach auf die admin.php und tippe deinen ersten Artikel ein!",
|
||||
"image" => "https://images.unsplash.com/photo-1518605368461-1e1e38ce8ba6?ixlib=rb-4.0.3&auto=format&fit=crop&w=800&q=80",
|
||||
"link" => ""
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
echo json_encode($news);
|
||||
|
||||
} catch(PDOException $e) {
|
||||
echo json_encode(["error" => $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user