📁 File Manager
🏠 /
/
home
/
u857492117
/
domains
/
mangaldaicollege.org
/
public_html
/
Automation
Dosya Düzenle: books.php
<?php include 'connect.php'; /* ========================= DELETE BOOKING ========================= */ if (isset($_GET['delete_id'])) { $id = $_GET['delete_id']; mysqli_query($con, "DELETE FROM bookings WHERE id=$id"); echo "<script>alert('Deleted'); window.location='books.php';</script>"; } /* ========================= AJAX: FETCH STUDENT NAME ========================= */ if (isset($_POST['get_student'])) { $roll = $_POST['roll']; $query = mysqli_query($con, "SELECT Sname FROM student WHERE Sroll='$roll'"); if ($row = mysqli_fetch_assoc($query)) { echo $row['Sname']; } else { echo ""; } exit; } /* ========================= GROUP FUNCTION ========================= */ function getStudentsPerBook($group) { preg_match('/\/(\d+)/', $group, $match); return isset($match[1]) ? (int)$match[1] : 1; } /* ========================= BOOKING LOGIC ========================= */ if (isset($_POST['book_id'])) { $book_id = $_POST['book_id']; $name = trim($_POST['student_name']); $roll = trim($_POST['roll_no']); if ($name != "" && $roll != "") { $check = mysqli_query($con, "SELECT * FROM bookings WHERE book_id=$book_id AND student_name='$name'"); if (mysqli_num_rows($check) == 0) { $resBook = mysqli_query($con, "SELECT qty, book_group FROM books WHERE id=$book_id"); $bookData = mysqli_fetch_assoc($resBook); $qty = $bookData['qty']; $group = $bookData['book_group']; $students_per_book = getStudentsPerBook($group); $max_students = $qty * $students_per_book; $res = mysqli_query($con, "SELECT COUNT(*) as total FROM bookings WHERE book_id=$book_id"); $count = mysqli_fetch_assoc($res)['total']; if ($count < $max_students) { mysqli_query($con, "INSERT INTO bookings (book_id, student_name) VALUES ($book_id, '$name')"); echo "<script>alert('Booked Successfully');</script>"; } else { echo "<script>alert('Booking Full');</script>"; } } else { echo "<script>alert('Already Booked');</script>"; } } } /* ========================= SEARCH LOGIC ========================= */ $search = ""; if (isset($_GET['search'])) { $search = $_GET['search']; $query = "SELECT * FROM books WHERE book_name LIKE '%$search%'"; } else { $query = "SELECT * FROM books"; } ?> <!DOCTYPE html> <html> <head> <title>Book Allocation</title> <style> body { font-family: Arial; background:#f4f4f4; } h2 { text-align:center; } table { border-collapse: collapse; width: 100%; background:#fff; } th, td { border: 1px solid #ccc; padding: 8px; text-align: center; } th { background:#222; color:white; } input { padding:5px; margin:2px; } button { padding:5px 10px; cursor:pointer; } .full { background:red; color:white; } .delete { background:red; color:white; border:none; padding:2px 6px; } .search-box { text-align:center; margin:10px; } </style> </head> <body> <h2>📚 Book Allocation System</h2> <!-- 🔍 SEARCH --> <div class="search-box"> <form method="GET"> <input type="text" name="search" placeholder="Search Book Name" value="<?= $search ?>"> <button type="submit">Search</button> <a href="books.php">Reset</a> </form> </div> <table> <tr> <th>Book Name</th> <th>Author</th> <th>Group</th> <th>Qty</th> <th>Price</th> <th>Alloted Students</th> <th>Available</th> <th>Action</th> </tr> <?php $result = mysqli_query($con, $query); while ($row = mysqli_fetch_assoc($result)) { $book_id = $row['id']; $group = $row['book_group']; $qty = $row['qty']; $students_per_book = getStudentsPerBook($group); $max_students = $qty * $students_per_book; // Get students with ID $students = mysqli_query($con, "SELECT * FROM bookings WHERE book_id=$book_id"); $names = []; while ($s = mysqli_fetch_assoc($students)) { $names[] = $s['student_name'] . " <a class='delete' href='?delete_id=".$s['id']."' onclick='return confirm(\"Delete?\")'>X</a>"; } $booked = count($names); $available = $max_students - $booked; ?> <tr> <td><?= $row['book_name'] ?></td> <td><?= $row['author'] ?></td> <td><?= $group ?></td> <td><?= $qty ?></td> <td>₹<?= $row['price'] ?></td> <td><?= $booked ? implode("<br>", $names) : "None"; ?></td> <td><?= $available ?></td> <td> <?php if ($available > 0) { ?> <form method="POST"> <input type="hidden" name="book_id" value="<?= $book_id ?>"> <input type="text" name="roll_no" placeholder="Roll No" onkeyup="getStudentName(this.value, <?= $book_id ?>)" required> <input type="text" name="student_name" id="name<?= $book_id ?>" placeholder="Student Name" readonly required> <button type="submit">Book</button> </form> <?php } else { ?> <button class="full" disabled>Full</button> <?php } ?> </td> </tr> <?php } ?> </table> <script> function getStudentName(roll, book_id) { if (roll.length == 0) { document.getElementById("name"+book_id).value = ""; return; } var xhr = new XMLHttpRequest(); xhr.open("POST", "", true); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onload = function () { document.getElementById("name"+book_id).value = this.responseText; }; xhr.send("get_student=1&roll=" + roll); } </script> </body> </html>
💾 Kaydet
İptal