Viewing file: question_crud.php (6.88 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<!DOCTYPE html> <html lang="en">
<?php include 'head.php';?> <style> table { border-collapse: collapse; width: 100%; margin-top: 20px; }
table, th, td { border: 1px solid #ddd; }
th, td { padding: 8px; text-align: left; }
th { background-color: #f2f2f2; font-weight: bold; } .custom-image { width: 100px; height: 100px; } </style> <body>
<!-- ======= Header ======= --> <?php include 'menubar.php';?>
<?php include 'sidebar.php';?>
<main id="main" class="main">
<?php // question_crud.php include 'connect.php';
// Handle Delete if (isset($_GET['delete'])) { $id = intval($_GET['delete']); $conn->query("DELETE FROM question_set_paper WHERE ID=$id"); header("Location: question_crud.php"); exit(); }
// Handle Save (Add / Update) if (isset($_POST['save'])) { $id = isset($_POST['id']) ? intval($_POST['id']) : 0; $question = $conn->real_escape_string($_POST['question']); $wrong1 = $conn->real_escape_string($_POST['wronganswer1']); $wrong2 = $conn->real_escape_string($_POST['wronganswer2']); $wrong3 = $conn->real_escape_string($_POST['wronganswer3']); $wrong4 = $conn->real_escape_string($_POST['wronganswer4']); $correct = $conn->real_escape_string($_POST['correctanswer']); $paper_set = $conn->real_escape_string($_POST['paper_set']);
if ($id > 0) { // Update $sql = "UPDATE question_set_paper SET question='$question', wronganswer1='$wrong1', wronganswer2='$wrong2', wronganswer3='$wrong3', wronganswer4='$wrong4', correctanswer='$correct', paper_set='$paper_set' WHERE ID=$id"; } else { // Insert $sql = "INSERT INTO question_set_paper (question, wronganswer1, wronganswer2, wronganswer3, wronganswer4, correctanswer, paper_set) VALUES ('$question','$wrong1','$wrong2','$wrong3','$wrong4','$correct','$paper_set')"; } $conn->query($sql); }
// Fetch Paper Sets for Dropdown $paperSets = []; $res = $conn->query("SELECT DISTINCT paper_set FROM question_set_paper"); while ($row = $res->fetch_assoc()) { $paperSets[] = $row['paper_set']; }
// Filter by Paper Set $filter = isset($_GET['filter']) ? $conn->real_escape_string($_GET['filter']) : ''; $query = "SELECT * FROM question_set_paper"; if ($filter != '') { $query .= " WHERE paper_set='$filter'"; } $result = $conn->query($query);
// For Editing $editData = null; if (isset($_GET['edit'])) { $id = intval($_GET['edit']); $editRes = $conn->query("SELECT * FROM question_set_paper WHERE ID=$id"); $editData = $editRes->fetch_assoc(); } ?>
<!DOCTYPE html> <html> <head> <title>Question CRUD</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } table { border-collapse: collapse; width: 100%; margin-top: 20px; } th, td { border: 1px solid #ccc; padding: 8px; text-align: left; } th { background: #f4f4f4; } .btn { padding: 6px 12px; text-decoration: none; border: 1px solid #333; background: #eee; margin-right: 5px; } .btn:hover { background: #ddd; } textarea { width: 100%; height: 50px; } input[type=text] { width: 100%; padding: 5px; } select { padding: 5px; } </style> </head> <body>
<h2>Question Management</h2>
<!-- Filter --> <form method="get" style="margin-bottom:15px;"> <label>Filter by Paper Set: </label> <select name="filter" onchange="this.form.submit()"> <option value="">All</option> <?php foreach($paperSets as $ps): ?> <option value="<?= $ps ?>" <?= ($filter==$ps?'selected':'') ?>><?= $ps ?></option> <?php endforeach; ?> </select> </form>
<!-- Add/Edit Button --> <a href="question_crud.php?add=1" class="btn">➕ Add Question</a> <a href="question_crud.php" class="btn">📋 Show List</a>
<?php if (isset($_GET['add']) || $editData): ?> <!-- Add/Edit Form --> <h3><?= $editData ? "Edit Question" : "Add Question" ?></h3> <form method="post"> <input type="hidden" name="id" value="<?= $editData['ID'] ?? '' ?>"> <label>Paper Set:</label><br> <select name="paper_set" required> <option value="">-- Select Paper Set --</option> <?php foreach($paperSets as $ps): ?> <option value="<?= $ps ?>" <?= ($editData && $editData['paper_set']==$ps?'selected':'') ?>><?= $ps ?></option> <?php endforeach; ?> </select> <br><br>
<label>Question:</label><br> <textarea name="question" required><?= $editData['question'] ?? '' ?></textarea><br><br>
<label>Wrong Answer 1:</label><br> <textarea name="wronganswer1" required><?= $editData['wronganswer1'] ?? '' ?></textarea><br><br>
<label>Wrong Answer 2:</label><br> <textarea name="wronganswer2" required><?= $editData['wronganswer2'] ?? '' ?></textarea><br><br>
<label>Wrong Answer 3:</label><br> <textarea name="wronganswer3" required><?= $editData['wronganswer3'] ?? '' ?></textarea><br><br>
<label>Wrong Answer 4:</label><br> <textarea name="wronganswer4" required><?= $editData['wronganswer4'] ?? '' ?></textarea><br><br>
<label>Correct Answer:</label><br> <select name="correctanswer" required> <option value="">-- Select --</option> <?php foreach(['a','b','c','d'] as $opt): ?> <option value="<?= $opt ?>" <?= ($editData && $editData['correctanswer']==$opt?'selected':'') ?>><?= strtoupper($opt) ?></option> <?php endforeach; ?> </select> <br><br>
<button type="submit" name="save" class="btn">💾 Save</button> </form> <?php else: ?> <!-- List of Questions --> <table> <tr> <th>ID</th> <th>Paper Set</th> <th>Question</th> <th>Wrong Answers</th> <th>Correct Answer</th> <th>Actions</th> </tr> <?php while($row = $result->fetch_assoc()): ?> <tr> <td><?= $row['ID'] ?></td> <td><?= $row['paper_set'] ?></td> <td><?= htmlspecialchars($row['question']) ?></td> <td> 1: <?= htmlspecialchars($row['wronganswer1']) ?><br> 2: <?= htmlspecialchars($row['wronganswer2']) ?><br> 3: <?= htmlspecialchars($row['wronganswer3']) ?><br> 4: <?= htmlspecialchars($row['wronganswer4']) ?> </td> <td><?= strtoupper($row['correctanswer']) ?></td> <td> <a href="question_crud.php?edit=<?= $row['ID'] ?>" class="btn">✏ Edit</a> <a href="question_crud.php?delete=<?= $row['ID'] ?>" class="btn" onclick="return confirm('Delete this question?')">🗑 Delete</a> </td> </tr> <?php endwhile; ?> </table> <?php endif; ?>
</body> </html>
</main><!-- End #main -->
<?php include 'footer.php';?>
</body>
</html>
|