File manager - Edit - /home/familylifersmpc/htdocs/www.familylifersmpc.com/view_announcements.php
Back
<?php error_reporting(0); include('editor-includes/web-config.php'); $conn = new mysqli($host, $user, $pass, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Handle delete request if (isset($_GET['delete_id'])) { $deleteId = intval($_GET['delete_id']); // Get image name before deleting $result = $conn->query("SELECT ImageName FROM announcementtbl WHERE ID = $deleteId"); $row = $result->fetch_assoc(); if (!empty($row['ImageName'])) { $imagePath = "UPLOADS_ANNOUNCEMENTS/" . $row['ImageName']; if (file_exists($imagePath)) { unlink($imagePath); } } // Delete from database $conn->query("DELETE FROM announcementtbl WHERE ID = $deleteId"); echo "<div class='alert alert-success text-center'>Announcement deleted successfully!</div>"; } // Handle new announcement submission if ($_SERVER['REQUEST_METHOD'] == 'POST') { $title = $conn->real_escape_string($_POST['title']); $description = $conn->real_escape_string($_POST['description']); $imagePos = $conn->real_escape_string($_POST['image_position']); // Image upload $imageName = ''; if (!empty($_FILES['image']['name'])) { $uploadDir = "UPLOADS_ANNOUNCEMENTS/"; if (!is_dir($uploadDir)) mkdir($uploadDir, 0777, true); $imageName = time() . "_" . basename($_FILES['image']['name']); $targetFile = $uploadDir . $imageName; move_uploaded_file($_FILES['image']['tmp_name'], $targetFile); } $conn->query("INSERT INTO announcementtbl (Title, Description, ImageName, ImagePosition) VALUES ('$title', '$description', '$imageName', '$imagePos')"); echo "<div class='alert alert-success text-center'>Announcement posted successfully!</div>"; } // --- Pagination Setup --- $postsPerPage = 5; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; if ($page < 1) $page = 1; $offset = ($page - 1) * $postsPerPage; // Count total announcements $totalResult = $conn->query("SELECT COUNT(*) as total FROM announcementtbl"); $totalRow = $totalResult->fetch_assoc(); $totalPosts = $totalRow['total']; $totalPages = ceil($totalPosts / $postsPerPage); // Fetch announcements for current page $result = $conn->query("SELECT * FROM announcementtbl ORDER BY ID DESC LIMIT $offset, $postsPerPage"); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Create Announcement</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"> <style> body { background: #f8f9fa; } .form-container { max-width: 600px; margin: 50px auto 30px; background: #fff; padding: 20px; border-radius: 15px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); } .announcement-card { max-width: 900px; margin: 20px auto; background: #fff; border-radius: 15px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); padding: 20px; } .announcement-img { width: 100%; max-width: 400px; border-radius: 10px; object-fit: cover; } .row-align { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; } @media (max-width: 768px) { .row-align { flex-direction: column; text-align: center; } .announcement-img { max-width: 100%; } } </style> </head> <body> <div class="container"> <!-- Announcement Form --> <h3 class="mb-3 text-center">Announcements</h3> <!-- Display Announcements --> <?php while ($row = $result->fetch_assoc()): ?> <div class="announcement-card"> <div class="text-end mb-2"> <a href="?delete_id=<?php echo $row['ID']; ?>&page=<?php echo $page; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this announcement?');"> Delete </a> </div> <div class="row-align"> <?php if ($row['ImagePosition'] == 'left'): ?> <?php if (!empty($row['ImageName'])): ?> <img src="UPLOADS_ANNOUNCEMENTS/<?php echo $row['ImageName']; ?>" class="announcement-img"> <?php endif; ?> <div class="announcement-content"> <h4><?php echo htmlspecialchars($row['Title']); ?></h4> <p><?php echo nl2br(htmlspecialchars($row['Description'])); ?></p> </div> <?php else: ?> <div class="announcement-content"> <h4><?php echo htmlspecialchars($row['Title']); ?></h4> <p><?php echo nl2br(htmlspecialchars($row['Description'])); ?></p> </div> <?php if (!empty($row['ImageName'])): ?> <img src="UPLOADS_ANNOUNCEMENTS/<?php echo $row['ImageName']; ?>" class="announcement-img"> <?php endif; ?> <?php endif; ?> </div> </div> <?php endwhile; ?> <!-- Pagination --> <nav> <ul class="pagination justify-content-center mt-4"> <?php if ($page > 1): ?> <li class="page-item"><a class="page-link" href="?page=<?php echo $page - 1; ?>">Previous</a></li> <?php endif; ?> <?php for ($i = 1; $i <= $totalPages; $i++): ?> <li class="page-item <?php echo ($i == $page) ? 'active' : ''; ?>"> <a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a> </li> <?php endfor; ?> <?php if ($page < $totalPages): ?> <li class="page-item"><a class="page-link" href="?page=<?php echo $page + 1; ?>">Next</a></li> <?php endif; ?> </ul> </nav> </div> </body> </html> <?php $conn->close(); ?>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings