Commit f87c54d3 authored by Satria Pambingkas's avatar Satria Pambingkas
Browse files

Update CMS.php, index.php, style.css, classes/Post.php

No related merge requests found
Showing with 191 additions and 0 deletions
+191 -0
<?php
// File: classes/CMS.php
class CMS {
// Atribut
protected $title;
protected $author;
// Konstruktor
public function __construct($title, $author) {
$this->title = $title;
$this->author = $author;
}
// Method untuk menampilkan informasi CMS
public function displayInfo() {
echo "Title: " . $this->title . "<br>";
echo "Author: " . $this->author . "<br>";
}
}
?>
<?php
// File: classes/Post.php
require_once 'CMS.php';
class Post extends CMS {
private $content;
private $category;
public function __construct($title, $author, $content, $category) {
parent::__construct($title, $author);
$this->content = $content;
$this->category = $category;
}
public function displayPost() {
echo "<p class='info'>Title: " . $this->title . "</p>";
echo "<p class='info'>Author: " . $this->author . "</p>";
echo "<p class='content'>Content: " . $this->content . "</p>";
echo "<span class='category'>" . $this->category . "</span>";
}
}
?>
<?php
// File: index.php
// Import kelas CMS dan Post
require_once 'classes/Post.php';
// Mulai session untuk menyimpan post sementara
session_start();
// Inisialisasi array post jika belum ada
if (!isset($_SESSION['posts'])) {
$_SESSION['posts'] = [];
}
// Jika form disubmit, tambahkan post ke session
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] === 'add_post') {
$title = $_POST['title'];
$author = $_POST['author'];
$content = $_POST['content'];
$category = $_POST['category'];
// Buat objek Post baru
$newPost = new Post($title, $author, $content, $category);
// Simpan post ke session
$_SESSION['posts'][] = $newPost;
}
// Jika permintaan untuk menghapus post diterima
if (isset($_POST['action']) && $_POST['action'] === 'delete_post' && isset($_POST['post_index'])) {
$postIndex = $_POST['post_index'];
// Hapus post berdasarkan index
array_splice($_SESSION['posts'], $postIndex, 1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple CMS - Add Post</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Create a New Post</h1>
<!-- Form untuk membuat post baru -->
<form action="" method="POST" class="post-form">
<input type="hidden" name="action" value="add_post">
<label for="title">Title:</label>
<input type="text" id="title" name="title" required><br>
<label for="author">Author:</label>
<input type="text" id="author" name="author" required><br>
<label for="content">Content:</label>
<textarea id="content" name="content" required></textarea><br>
<label for="category">Category:</label>
<input type="text" id="category" name="category" required><br>
<button type="submit">Submit Post</button>
</form>
<hr>
<!-- Tampilkan semua post yang sudah dibuat -->
<h2>All Posts:</h2>
<?php
if (count($_SESSION['posts']) > 0) {
foreach ($_SESSION['posts'] as $index => $post) {
echo "<div class='post'><h2>Post " . ($index + 1) . ":</h2>";
$post->displayPost();
// Form untuk menghapus post
echo "
<form action='' method='POST'>
<input type='hidden' name='action' value='delete_post'>
<input type='hidden' name='post_index' value='$index'>
<button type='submit' class='delete-button'>Delete</button>
</form>";
echo "</div>";
}
} else {
echo "<p>No posts available.</p>";
}
?>
</body>
</html>
/* Styling for the post form */
.post-form {
background-color: #fff;
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.post-form label {
font-weight: bold;
display: block;
margin-bottom: 8px;
}
.post-form input[type="text"],
.post-form textarea {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.delete-button {
background-color: #f44336;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
margin-top: 10px;
}
.delete-button:hover {
background-color: #d32f2f;
}
.post-form button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
}
.post-form button:hover {
background-color: #45a049;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment