📅 Week 12 - Day 2: Blog Development

Wednesday | Duration: 3 hours | Building Core Features 💻

🎯 Learning Objectives

📄 Header Component (includes/header.php)

<?php // includes/header.php $pageTitle = isset($pageTitle) ? $pageTitle : 'My Blog'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php echo $pageTitle; ?></title> <link rel="stylesheet" href="css/style.css"> </head> <body> <header> <div class="container"> <h1>📝 My Awesome Blog</h1> <p>Sharing thoughts and ideas with the world</p> </div> </header> <nav> <ul> <li><a href="index.php">Home</a></li> <li><a href="create_post.php">Create Post</a></li> </ul> </nav>

📄 Footer Component (includes/footer.php)

<!-- includes/footer.php --> <footer> <div class="container"> <p>© <?php echo date('Y'); ?> My Awesome Blog. All rights reserved.</p> <p>Built with PHP & MySQL</p> </div> </footer> </body> </html>

🏠 Homepage - Display All Posts (index.php)

<?php // index.php $pageTitle = "Home - My Blog"; require_once 'config/database.php'; include 'includes/header.php'; ?> <div class="container"> <h2 style="margin-bottom: 30px;">Latest Blog Posts</h2> <?php // Fetch all posts, newest first $sql = "SELECT * FROM posts ORDER BY created_at DESC"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while ($post = mysqli_fetch_assoc($result)) { // Create excerpt (first 150 characters) $excerpt = substr($post['content'], 0, 150) . '...'; ?> <div class="post-card"> <h3 style="color: #f5576c; margin-bottom: 10px;"> <a href="view_post.php?id=<?php echo $post['id']; ?>" style="text-decoration: none; color: inherit;"> <?php echo htmlspecialchars($post['title']); ?> </a> </h3> <div style="color: #666; font-size: 14px; margin-bottom: 15px;"> <span>✍️ <?php echo htmlspecialchars($post['author']); ?></span> <span style="margin: 0 10px;">|</span> <span>📅 <?php echo date('F j, Y', strtotime($post['created_at'])); ?></span> <span style="margin: 0 10px;">|</span> <span>👁️ <?php echo $post['views']; ?> views</span> <?php if ($post['category']): ?> <span style="margin: 0 10px;">|</span> <span>🏷️ <?php echo htmlspecialchars($post['category']); ?></span> <?php endif; ?> </div> <p style="color: #555; line-height: 1.6; margin-bottom: 15px;"> <?php echo htmlspecialchars($excerpt); ?> </p> <a href="view_post.php?id=<?php echo $post['id']; ?>" class="btn">Read More →</a> </div> <?php } } else { echo "<p>No blog posts yet. <a href='create_post.php'>Create the first one!</a></p>"; } mysqli_close($conn); ?> </div> <?php include 'includes/footer.php'; ?>

👁️ View Single Post (view_post.php)

<?php // view_post.php require_once 'config/database.php'; // Get post ID from URL $post_id = isset($_GET['id']) ? (int)$_GET['id'] : 0; if ($post_id > 0) { // Fetch post $stmt = mysqli_prepare($conn, "SELECT * FROM posts WHERE id = ?"); mysqli_stmt_bind_param($stmt, "i", $post_id); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); $post = mysqli_fetch_assoc($result); if ($post) { // Increment view count $updateViews = mysqli_prepare($conn, "UPDATE posts SET views = views + 1 WHERE id = ?"); mysqli_stmt_bind_param($updateViews, "i", $post_id); mysqli_stmt_execute($updateViews); $pageTitle = htmlspecialchars($post['title']) . " - My Blog"; } else { header("Location: index.php"); exit(); } } else { header("Location: index.php"); exit(); } include 'includes/header.php'; ?> <div class="container"> <div class="post-card"> <h2 style="color: #f5576c; margin-bottom: 15px;"> <?php echo htmlspecialchars($post['title']); ?> </h2> <div style="color: #666; font-size: 14px; margin-bottom: 25px; padding-bottom: 15px; border-bottom: 2px solid #f093fb;"> <span>✍️ <strong><?php echo htmlspecialchars($post['author']); ?></strong></span> <span style="margin: 0 10px;">|</span> <span>📅 <?php echo date('F j, Y \a\t g:i A', strtotime($post['created_at'])); ?></span> <span style="margin: 0 10px;">|</span> <span>👁️ <?php echo $post['views']; ?> views</span> <?php if ($post['category']): ?> <span style="margin: 0 10px;">|</span> <span>🏷️ <?php echo htmlspecialchars($post['category']); ?></span> <?php endif; ?> </div> <div style="color: #333; line-height: 1.8; font-size: 16px;"> <?php echo nl2br(htmlspecialchars($post['content'])); ?> </div> <div style="margin-top: 30px; padding-top: 20px; border-top: 1px solid #ddd;"> <a href="edit_post.php?id=<?php echo $post['id']; ?>" class="btn">✏️ Edit Post</a> <a href="delete_post.php?id=<?php echo $post['id']; ?>" class="btn" style="background: #dc3545;" onclick="return confirm('Are you sure you want to delete this post?');">🗑️ Delete Post</a> <a href="index.php" class="btn" style="background: #6c757d;">← Back to Home</a> </div> </div> </div> <?php mysqli_close($conn); include 'includes/footer.php'; ?>

➕ Create New Post (create_post.php)

<?php // create_post.php $pageTitle = "Create New Post - My Blog"; require_once 'config/database.php'; $errors = []; $success = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get and validate inputs $title = trim($_POST['title']); $content = trim($_POST['content']); $author = trim($_POST['author']); $category = trim($_POST['category']); // Validation if (empty($title) || strlen($title) < 5) { $errors[] = "Title must be at least 5 characters"; } if (empty($content) || strlen($content) < 20) { $errors[] = "Content must be at least 20 characters"; } if (empty($author)) { $errors[] = "Author name is required"; } // If no errors, insert into database if (empty($errors)) { $stmt = mysqli_prepare($conn, "INSERT INTO posts (title, content, author, category) VALUES (?, ?, ?, ?)"); mysqli_stmt_bind_param($stmt, "ssss", $title, $content, $author, $category); if (mysqli_stmt_execute($stmt)) { $post_id = mysqli_insert_id($conn); header("Location: view_post.php?id=" . $post_id); exit(); } else { $errors[] = "Error creating post: " . mysqli_error($conn); } mysqli_stmt_close($stmt); } } include 'includes/header.php'; ?> <div class="container"> <div class="post-card"> <h2 style="color: #f5576c; margin-bottom: 30px;">Create New Blog Post</h2> <?php if (!empty($errors)): ?> <div style="background: #f8d7da; color: #721c24; padding: 15px; border-radius: 5px; margin-bottom: 20px;"> <ul style="margin-left: 20px;"> <?php foreach ($errors as $error): ?> <li><?php echo htmlspecialchars($error); ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <form method="POST" action=""> <div style="margin-bottom: 20px;"> <label style="display: block; margin-bottom: 5px; font-weight: bold;">Post Title:</label> <input type="text" name="title" style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px;" value="<?php echo isset($_POST['title']) ? htmlspecialchars($_POST['title']) : ''; ?>" required> </div> <div style="margin-bottom: 20px;"> <label style="display: block; margin-bottom: 5px; font-weight: bold;">Author Name:</label> <input type="text" name="author" style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px;" value="<?php echo isset($_POST['author']) ? htmlspecialchars($_POST['author']) : ''; ?>" required> </div> <div style="margin-bottom: 20px;"> <label style="display: block; margin-bottom: 5px; font-weight: bold;">Category (optional):</label> <select name="category" style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px;"> <option value="">-- Select Category --</option> <option value="Technology">Technology</option> <option value="Career">Career</option> <option value="Personal">Personal</option> <option value="Tutorial">Tutorial</option> <option value="General">General</option> </select> </div> <div style="margin-bottom: 20px;"> <label style="display: block; margin-bottom: 5px; font-weight: bold;">Content:</label> <textarea name="content" rows="12" style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px; resize: vertical;" required><?php echo isset($_POST['content']) ? htmlspecialchars($_POST['content']) : ''; ?></textarea> </div> <button type="submit" class="btn">📝 Publish Post</button> <a href="index.php" class="btn" style="background: #6c757d; text-decoration: none;">Cancel</a> </form> </div> </div> <?php mysqli_close($conn); include 'includes/footer.php'; ?>

✏️ Today's Tasks

  1. Create header and footer components
  2. Build homepage (index.php) showing all posts
  3. Create view_post.php for single post view
  4. Build create_post.php with form
  5. Test all pages and fix any issues

✅ Today's Deliverable

← Previous Course Home Next: Day 3 →