66 lines
1.9 KiB
PHP
66 lines
1.9 KiB
PHP
<?php
|
|
require_once __DIR__ . "/../config/db.php";
|
|
|
|
function getArticles($mysqli, $limit = null) {
|
|
$articles = [];
|
|
$sql = "SELECT * FROM articles ORDER BY id DESC";
|
|
if ($limit !== null) {
|
|
$sql .= " LIMIT " . intval($limit);
|
|
}
|
|
$result = $mysqli->query($sql);
|
|
if ($result && $result->num_rows > 0) {
|
|
while ($row = $result->fetch_assoc()) {
|
|
$articles[] = $row;
|
|
}
|
|
}
|
|
return $articles;
|
|
}
|
|
|
|
// Function alias for compatibility with any controllers using showThreeArticles
|
|
function getThreeArticles($mysqli) {
|
|
return getArticles($mysqli, 3);
|
|
}
|
|
|
|
function getArticleById($mysqli, $id) {
|
|
$sql = "SELECT * FROM articles WHERE id = ?";
|
|
$stmt = $mysqli->prepare($sql);
|
|
if ($stmt) {
|
|
$stmt->bind_param("i", $id);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
return $result->fetch_assoc();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function addArticle($mysqli, $title, $excerpt, $date_published, $tag, $url) {
|
|
$sql = "INSERT INTO articles (title, excerpt, date_published, tag, url) VALUES (?, ?, ?, ?, ?)";
|
|
$stmt = $mysqli->prepare($sql);
|
|
if ($stmt) {
|
|
$stmt->bind_param("sssss", $title, $excerpt, $date_published, $tag, $url);
|
|
return $stmt->execute();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function editArticle($mysqli, $id, $title, $excerpt, $date_published, $tag, $url) {
|
|
$sql = "UPDATE articles SET title = ?, excerpt = ?, date_published = ?, tag = ?, url = ? WHERE id = ?";
|
|
$stmt = $mysqli->prepare($sql);
|
|
if ($stmt) {
|
|
$stmt->bind_param("sssssi", $title, $excerpt, $date_published, $tag, $url, $id);
|
|
return $stmt->execute();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function deleteArticle($mysqli, $id) {
|
|
$sql = "DELETE FROM articles WHERE id = ?";
|
|
$stmt = $mysqli->prepare($sql);
|
|
if ($stmt) {
|
|
$stmt->bind_param("i", $id);
|
|
return $stmt->execute();
|
|
}
|
|
return false;
|
|
}
|
|
?>
|