From 944eebf1382605fd0f8475526d304c1276bd5cee Mon Sep 17 00:00:00 2001 From: Lokesh Patra Date: Sat, 16 Nov 2024 11:55:37 +0530 Subject: [PATCH] Foundation Set! [ v1 ] --- add_book.css => v1 Foundation/add_book.css | 0 add_book.html => v1 Foundation/add_book.html | 52 ++--- add_book.php => v1 Foundation/add_book.php | 90 ++++---- books.json => v1 Foundation/books.json | 0 books.xml => v1 Foundation/books.xml | 0 common.css => v1 Foundation/common.css | 94 ++++---- .../dashboard.html | 58 ++--- db_config.php => v1 Foundation/db_config.php | 30 +-- .../export_books.php | 32 +-- .../export_books.py | 50 ++--- index.css => v1 Foundation/index.css | 86 +++---- index.html => v1 Foundation/index.html | 56 ++--- login.css => v1 Foundation/login.css | 0 login.html => v1 Foundation/login.html | 50 ++--- login.php => v1 Foundation/login.php | 48 ++-- logout.php => v1 Foundation/logout.php | 10 +- register.css => v1 Foundation/register.css | 0 register.html => v1 Foundation/register.html | 56 ++--- register.php => v1 Foundation/register.php | 60 ++--- search.js => v1 Foundation/search.js | 68 +++--- .../search_books.css | 0 .../search_books.html | 210 +++++++++--------- .../search_books.php | 108 ++++----- validation.js => v1 Foundation/validation.js | 26 +-- 24 files changed, 592 insertions(+), 592 deletions(-) rename add_book.css => v1 Foundation/add_book.css (100%) rename add_book.html => v1 Foundation/add_book.html (97%) rename add_book.php => v1 Foundation/add_book.php (96%) rename books.json => v1 Foundation/books.json (100%) rename books.xml => v1 Foundation/books.xml (100%) rename common.css => v1 Foundation/common.css (94%) rename dashboard.html => v1 Foundation/dashboard.html (96%) rename db_config.php => v1 Foundation/db_config.php (96%) rename export_books.php => v1 Foundation/export_books.php (97%) rename export_books.py => v1 Foundation/export_books.py (96%) rename index.css => v1 Foundation/index.css (93%) rename index.html => v1 Foundation/index.html (96%) rename login.css => v1 Foundation/login.css (100%) rename login.html => v1 Foundation/login.html (96%) rename login.php => v1 Foundation/login.php (96%) rename logout.php => v1 Foundation/logout.php (94%) rename register.css => v1 Foundation/register.css (100%) rename register.html => v1 Foundation/register.html (96%) rename register.php => v1 Foundation/register.php (96%) rename search.js => v1 Foundation/search.js (97%) rename search_books.css => v1 Foundation/search_books.css (100%) rename search_books.html => v1 Foundation/search_books.html (97%) rename search_books.php => v1 Foundation/search_books.php (97%) rename validation.js => v1 Foundation/validation.js (98%) diff --git a/add_book.css b/v1 Foundation/add_book.css similarity index 100% rename from add_book.css rename to v1 Foundation/add_book.css diff --git a/add_book.html b/v1 Foundation/add_book.html similarity index 97% rename from add_book.html rename to v1 Foundation/add_book.html index b84961e..67fce45 100644 --- a/add_book.html +++ b/v1 Foundation/add_book.html @@ -1,27 +1,27 @@ - - - - - - Add Book - - - -

Add a New Book

-
- -
- - -
- - -
- - -
- - -
- + + + + + + Add Book + + + +

Add a New Book

+
+ +
+ + +
+ + +
+ + +
+ + +
+ \ No newline at end of file diff --git a/add_book.php b/v1 Foundation/add_book.php similarity index 96% rename from add_book.php rename to v1 Foundation/add_book.php index c7f02c1..670cb0a 100644 --- a/add_book.php +++ b/v1 Foundation/add_book.php @@ -1,46 +1,46 @@ -query($query); - $books = []; - - while ($row = $result->fetch_assoc()) { - $books[] = $row; - } - - // Encode the array to JSON and save it to the books.json file - file_put_contents('books.json', json_encode($books, JSON_PRETTY_PRINT)); -} - -if ($_SERVER["REQUEST_METHOD"] === "POST") { - // Retrieve form data - $title = $_POST['title'] ?? ''; - $author = $_POST['author'] ?? ''; - $publication_year = $_POST['publication_year'] ?? 0; - $genre = $_POST['genre'] ?? ''; - - // Validate form data - if (empty($title) || empty($author) || empty($publication_year) || empty($genre)) { - die("All fields are required."); - } - - // Insert data into the database - $stmt = $conn->prepare("INSERT INTO books (title, author, publication_year, genre) VALUES (?, ?, ?, ?)"); - if (!$stmt) { - die("Prepare failed: " . $conn->error); - } - - $stmt->bind_param("ssis", $title, $author, $publication_year, $genre); - - if ($stmt->execute()) { - updateJsonFile($conn); // Update the JSON file after adding the book - echo "Book added successfully! Add another book"; - } else { - die("Error adding book: " . $stmt->error); - } -} +query($query); + $books = []; + + while ($row = $result->fetch_assoc()) { + $books[] = $row; + } + + // Encode the array to JSON and save it to the books.json file + file_put_contents('books.json', json_encode($books, JSON_PRETTY_PRINT)); +} + +if ($_SERVER["REQUEST_METHOD"] === "POST") { + // Retrieve form data + $title = $_POST['title'] ?? ''; + $author = $_POST['author'] ?? ''; + $publication_year = $_POST['publication_year'] ?? 0; + $genre = $_POST['genre'] ?? ''; + + // Validate form data + if (empty($title) || empty($author) || empty($publication_year) || empty($genre)) { + die("All fields are required."); + } + + // Insert data into the database + $stmt = $conn->prepare("INSERT INTO books (title, author, publication_year, genre) VALUES (?, ?, ?, ?)"); + if (!$stmt) { + die("Prepare failed: " . $conn->error); + } + + $stmt->bind_param("ssis", $title, $author, $publication_year, $genre); + + if ($stmt->execute()) { + updateJsonFile($conn); // Update the JSON file after adding the book + echo "Book added successfully! Add another book"; + } else { + die("Error adding book: " . $stmt->error); + } +} ?> \ No newline at end of file diff --git a/books.json b/v1 Foundation/books.json similarity index 100% rename from books.json rename to v1 Foundation/books.json diff --git a/books.xml b/v1 Foundation/books.xml similarity index 100% rename from books.xml rename to v1 Foundation/books.xml diff --git a/common.css b/v1 Foundation/common.css similarity index 94% rename from common.css rename to v1 Foundation/common.css index e80b1be..37fe235 100644 --- a/common.css +++ b/v1 Foundation/common.css @@ -1,48 +1,48 @@ -body { - font-family: Arial, sans-serif; - margin: 0; - padding: 0; - background-color: #f4f4f9; -} - -header { - background-color: #333; - color: white; - padding: 1em; - text-align: center; -} - -form { - width: 50%; - margin: 2em auto; - padding: 2em; - background: white; - border: 1px solid #ccc; - border-radius: 8px; -} - -form label { - display: block; - margin: 0.5em 0 0.2em; -} - -form input { - width: 100%; - padding: 0.5em; - margin-bottom: 1em; - border: 1px solid #ccc; - border-radius: 4px; -} - -form button { - background-color: #333; - color: white; - padding: 0.7em 1.5em; - border: none; - border-radius: 4px; - cursor: pointer; -} - -form button:hover { - background-color: #555; +body { + font-family: Arial, sans-serif; + margin: 0; + padding: 0; + background-color: #f4f4f9; +} + +header { + background-color: #333; + color: white; + padding: 1em; + text-align: center; +} + +form { + width: 50%; + margin: 2em auto; + padding: 2em; + background: white; + border: 1px solid #ccc; + border-radius: 8px; +} + +form label { + display: block; + margin: 0.5em 0 0.2em; +} + +form input { + width: 100%; + padding: 0.5em; + margin-bottom: 1em; + border: 1px solid #ccc; + border-radius: 4px; +} + +form button { + background-color: #333; + color: white; + padding: 0.7em 1.5em; + border: none; + border-radius: 4px; + cursor: pointer; +} + +form button:hover { + background-color: #555; } \ No newline at end of file diff --git a/dashboard.html b/v1 Foundation/dashboard.html similarity index 96% rename from dashboard.html rename to v1 Foundation/dashboard.html index 6041438..7b65ec3 100644 --- a/dashboard.html +++ b/v1 Foundation/dashboard.html @@ -1,29 +1,29 @@ - - - - - - Dashboard - - - -
-

Welcome to the Library Management System

-

- -

- Logout -
-
- Add Book - Search Books -
- - + + + + + + Dashboard + + + +
+

Welcome to the Library Management System

+

+ +

+ Logout +
+
+ Add Book + Search Books +
+ + diff --git a/db_config.php b/v1 Foundation/db_config.php similarity index 96% rename from db_config.php rename to v1 Foundation/db_config.php index 260d84e..1f2191e 100644 --- a/db_config.php +++ b/v1 Foundation/db_config.php @@ -1,16 +1,16 @@ -connect_error) { - die("Connection failed: " . $conn->connect_error); -} +connect_error) { + die("Connection failed: " . $conn->connect_error); +} ?> \ No newline at end of file diff --git a/export_books.php b/v1 Foundation/export_books.php similarity index 97% rename from export_books.php rename to v1 Foundation/export_books.php index bc4a7bf..380c6ea 100644 --- a/export_books.php +++ b/v1 Foundation/export_books.php @@ -1,16 +1,16 @@ -&1"); // Ensure paths with spaces are handled properly - -// Check if the script executed successfully and output the results -if ($output) { - echo "Books exported to books.xml successfully!
"; - echo "Python script output: " . nl2br($output); // nl2br to add line breaks for better readability -} else { - echo "Failed to export books to XML. Error: " . nl2br($output); -} -?> +&1"); // Ensure paths with spaces are handled properly + +// Check if the script executed successfully and output the results +if ($output) { + echo "Books exported to books.xml successfully!
"; + echo "Python script output: " . nl2br($output); // nl2br to add line breaks for better readability +} else { + echo "Failed to export books to XML. Error: " . nl2br($output); +} +?> diff --git a/export_books.py b/v1 Foundation/export_books.py similarity index 96% rename from export_books.py rename to v1 Foundation/export_books.py index 257a9fb..c0610ed 100644 --- a/export_books.py +++ b/v1 Foundation/export_books.py @@ -1,26 +1,26 @@ -import json -import xml.etree.ElementTree as ET -import os - -# Ensure the books.json file exists and is populated -if os.path.exists('books.json'): - with open('books.json', 'r') as file: - books = json.load(file) - - # Create XML root element - root = ET.Element('books') - - # Add book entries to XML - for book in books: - book_element = ET.SubElement(root, 'book') - for key, value in book.items(): - child = ET.SubElement(book_element, key) - child.text = str(value) - - # Write the XML data to a file - tree = ET.ElementTree(root) - tree.write('books.xml', encoding='utf-8', xml_declaration=True) - print("Books exported to books.xml") - -else: +import json +import xml.etree.ElementTree as ET +import os + +# Ensure the books.json file exists and is populated +if os.path.exists('books.json'): + with open('books.json', 'r') as file: + books = json.load(file) + + # Create XML root element + root = ET.Element('books') + + # Add book entries to XML + for book in books: + book_element = ET.SubElement(root, 'book') + for key, value in book.items(): + child = ET.SubElement(book_element, key) + child.text = str(value) + + # Write the XML data to a file + tree = ET.ElementTree(root) + tree.write('books.xml', encoding='utf-8', xml_declaration=True) + print("Books exported to books.xml") + +else: print("Error: books.json not found.") \ No newline at end of file diff --git a/index.css b/v1 Foundation/index.css similarity index 93% rename from index.css rename to v1 Foundation/index.css index 9a7cf0d..d59bcce 100644 --- a/index.css +++ b/v1 Foundation/index.css @@ -1,43 +1,43 @@ -main { - text-align: center; - padding: 2em; - background-color: #f9f9f9; -} - -h2 { - color: #333; -} - -p { - font-size: 1.2em; - margin: 1em 0; -} - -.actions { - margin-top: 2em; -} - -.actions .btn { - text-decoration: none; - color: white; - background-color: #333; - padding: 0.8em 1.5em; - border-radius: 5px; - margin: 0 1em; - font-size: 1em; - transition: background-color 0.3s; -} - -.actions .btn:hover { - background-color: #555; -} - -footer { - text-align: center; - padding: 1em 0; - background-color: #333; - color: white; - position: absolute; - bottom: 0; - width: 100%; -} +main { + text-align: center; + padding: 2em; + background-color: #f9f9f9; +} + +h2 { + color: #333; +} + +p { + font-size: 1.2em; + margin: 1em 0; +} + +.actions { + margin-top: 2em; +} + +.actions .btn { + text-decoration: none; + color: white; + background-color: #333; + padding: 0.8em 1.5em; + border-radius: 5px; + margin: 0 1em; + font-size: 1em; + transition: background-color 0.3s; +} + +.actions .btn:hover { + background-color: #555; +} + +footer { + text-align: center; + padding: 1em 0; + background-color: #333; + color: white; + position: absolute; + bottom: 0; + width: 100%; +} diff --git a/index.html b/v1 Foundation/index.html similarity index 96% rename from index.html rename to v1 Foundation/index.html index e231dec..8af837a 100644 --- a/index.html +++ b/v1 Foundation/index.html @@ -1,28 +1,28 @@ - - - - - - Library Management System - - - - -
-

Welcome to the Library Management System

-
-
-
-

Manage Your Books Efficiently

-

Register or Log In to manage books, search the library, and more!

- -
-
- - - + + + + + + Library Management System + + + + +
+

Welcome to the Library Management System

+
+
+
+

Manage Your Books Efficiently

+

Register or Log In to manage books, search the library, and more!

+ +
+
+ + + diff --git a/login.css b/v1 Foundation/login.css similarity index 100% rename from login.css rename to v1 Foundation/login.css diff --git a/login.html b/v1 Foundation/login.html similarity index 96% rename from login.html rename to v1 Foundation/login.html index 13b9a42..7113be1 100644 --- a/login.html +++ b/v1 Foundation/login.html @@ -1,25 +1,25 @@ - - - - - - User Login - - - - -
-

Library Management System

-
-
-

Login

- -
- - -
- - -
- - + + + + + + User Login + + + + +
+

Library Management System

+
+
+

Login

+ +
+ + +
+ + +
+ + diff --git a/login.php b/v1 Foundation/login.php similarity index 96% rename from login.php rename to v1 Foundation/login.php index b5d63f2..8562ff5 100644 --- a/login.php +++ b/v1 Foundation/login.php @@ -1,25 +1,25 @@ -prepare("SELECT * FROM users WHERE email = ? OR username = ?"); - $stmt->bind_param("ss", $username_email, $username_email); - $stmt->execute(); - $result = $stmt->get_result(); - $user = $result->fetch_assoc(); - - if ($user && password_verify($password, $user['password'])) { - $_SESSION['user_id'] = $user['id']; - $_SESSION['username'] = $user['username']; - header("Location: dashboard.html"); - exit; - } else { - echo "Invalid credentials. Try again"; - } -} +prepare("SELECT * FROM users WHERE email = ? OR username = ?"); + $stmt->bind_param("ss", $username_email, $username_email); + $stmt->execute(); + $result = $stmt->get_result(); + $user = $result->fetch_assoc(); + + if ($user && password_verify($password, $user['password'])) { + $_SESSION['user_id'] = $user['id']; + $_SESSION['username'] = $user['username']; + header("Location: dashboard.html"); + exit; + } else { + echo "Invalid credentials. Try again"; + } +} ?> \ No newline at end of file diff --git a/logout.php b/v1 Foundation/logout.php similarity index 94% rename from logout.php rename to v1 Foundation/logout.php index d0d6b64..9749cbc 100644 --- a/logout.php +++ b/v1 Foundation/logout.php @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/register.css b/v1 Foundation/register.css similarity index 100% rename from register.css rename to v1 Foundation/register.css diff --git a/register.html b/v1 Foundation/register.html similarity index 96% rename from register.html rename to v1 Foundation/register.html index cfa2f70..419df81 100644 --- a/register.html +++ b/v1 Foundation/register.html @@ -1,28 +1,28 @@ - - - - - - User Registration - - - - -
-

Library Management System

-
-
-

Register

- -
- - -
- - -
- - -
- - + + + + + + User Registration + + + + +
+

Library Management System

+
+
+

Register

+ +
+ + +
+ + +
+ + +
+ + diff --git a/register.php b/v1 Foundation/register.php similarity index 96% rename from register.php rename to v1 Foundation/register.php index 68cb857..226ecdb 100644 --- a/register.php +++ b/v1 Foundation/register.php @@ -1,30 +1,30 @@ -prepare("SELECT * FROM users WHERE email = ?"); - $checkQuery->bind_param("s", $email); - $checkQuery->execute(); - $result = $checkQuery->get_result(); - - if ($result->num_rows > 0) { - echo "Email already exists! Go back"; - exit; - } - - // Insert user into the database - $stmt = $conn->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)"); - $stmt->bind_param("sss", $username, $email, $password); - - if ($stmt->execute()) { - echo "Registration successful! Login here"; - } else { - echo "Error registering user."; - } -} -?> +prepare("SELECT * FROM users WHERE email = ?"); + $checkQuery->bind_param("s", $email); + $checkQuery->execute(); + $result = $checkQuery->get_result(); + + if ($result->num_rows > 0) { + echo "Email already exists! Go back"; + exit; + } + + // Insert user into the database + $stmt = $conn->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)"); + $stmt->bind_param("sss", $username, $email, $password); + + if ($stmt->execute()) { + echo "Registration successful! Login here"; + } else { + echo "Error registering user."; + } +} +?> diff --git a/search.js b/v1 Foundation/search.js similarity index 97% rename from search.js rename to v1 Foundation/search.js index 3160929..b2ae60a 100644 --- a/search.js +++ b/v1 Foundation/search.js @@ -1,34 +1,34 @@ -document.getElementById("searchBookForm").addEventListener("submit", async function (e) { - e.preventDefault(); - const title = document.getElementById("search_title").value.trim(); - const author = document.getElementById("search_author").value.trim(); - const year = document.getElementById("search_year").value.trim(); - - // Fetch search results using PHP backend - const response = await fetch("search_books.php", { - method: "POST", - headers: { "Content-Type": "application/x-www-form-urlencoded" }, - body: new URLSearchParams({ title, author, year }), - }); - const books = await response.json(); - - const tableBody = document.getElementById("results").querySelector("tbody"); - tableBody.innerHTML = ""; // Clear previous results - - books.forEach(book => { - const row = ` - ${book.id} - ${book.title} - ${book.author} - ${book.publication_year} - ${book.genre} - `; - tableBody.innerHTML += row; - }); -}); - -// Export to XML handler -document.getElementById("exportXML").addEventListener("click", async function () { - await fetch("export_books.php"); - alert("Books exported to XML!"); -}); +document.getElementById("searchBookForm").addEventListener("submit", async function (e) { + e.preventDefault(); + const title = document.getElementById("search_title").value.trim(); + const author = document.getElementById("search_author").value.trim(); + const year = document.getElementById("search_year").value.trim(); + + // Fetch search results using PHP backend + const response = await fetch("search_books.php", { + method: "POST", + headers: { "Content-Type": "application/x-www-form-urlencoded" }, + body: new URLSearchParams({ title, author, year }), + }); + const books = await response.json(); + + const tableBody = document.getElementById("results").querySelector("tbody"); + tableBody.innerHTML = ""; // Clear previous results + + books.forEach(book => { + const row = ` + ${book.id} + ${book.title} + ${book.author} + ${book.publication_year} + ${book.genre} + `; + tableBody.innerHTML += row; + }); +}); + +// Export to XML handler +document.getElementById("exportXML").addEventListener("click", async function () { + await fetch("export_books.php"); + alert("Books exported to XML!"); +}); diff --git a/search_books.css b/v1 Foundation/search_books.css similarity index 100% rename from search_books.css rename to v1 Foundation/search_books.css diff --git a/search_books.html b/v1 Foundation/search_books.html similarity index 97% rename from search_books.html rename to v1 Foundation/search_books.html index 969174e..56afe9e 100644 --- a/search_books.html +++ b/v1 Foundation/search_books.html @@ -1,106 +1,106 @@ - - - - - - Search Books - - - -

Search Books

-
- -
- - -
- - -
- - - - -
- -

Search Results:

- - - - - - - - - - - - -
TitleAuthorPublication YearGenre
- - - + + + + + + Search Books + + + +

Search Books

+
+ +
+ + +
+ + +
+ + + + +
+ +

Search Results:

+ + + + + + + + + + + + +
TitleAuthorPublication YearGenre
+ + + \ No newline at end of file diff --git a/search_books.php b/v1 Foundation/search_books.php similarity index 97% rename from search_books.php rename to v1 Foundation/search_books.php index 828acaf..0c8924b 100644 --- a/search_books.php +++ b/v1 Foundation/search_books.php @@ -1,54 +1,54 @@ -real_escape_string($title) . "%'"; - } - if ($author) { - $query .= " AND author LIKE '%" . $conn->real_escape_string($author) . "%'"; - } - if ($publication_year) { - $query .= " AND publication_year = " . (int)$publication_year; - } - - // Execute the query and store results - $search_results = $conn->query($query); - - // Prepare the result as an array - $books = []; - if ($search_results && $search_results->num_rows > 0) { - while ($row = $search_results->fetch_assoc()) { - $books[] = $row; - } - - // Update the books.json file - file_put_contents('books.json', json_encode($books, JSON_PRETTY_PRINT)); - - // Update the books.xml file - $xml = new SimpleXMLElement(''); - foreach ($books as $book) { - $book_xml = $xml->addChild('book'); - $book_xml->addChild('title', $book['title']); - $book_xml->addChild('author', $book['author']); - $book_xml->addChild('publication_year', $book['publication_year']); - $book_xml->addChild('genre', $book['genre']); - } - $xml->asXML('books.xml'); - } - - // Return the results as JSON for frontend use - echo json_encode($books); - exit(); -} -?> +real_escape_string($title) . "%'"; + } + if ($author) { + $query .= " AND author LIKE '%" . $conn->real_escape_string($author) . "%'"; + } + if ($publication_year) { + $query .= " AND publication_year = " . (int)$publication_year; + } + + // Execute the query and store results + $search_results = $conn->query($query); + + // Prepare the result as an array + $books = []; + if ($search_results && $search_results->num_rows > 0) { + while ($row = $search_results->fetch_assoc()) { + $books[] = $row; + } + + // Update the books.json file + file_put_contents('books.json', json_encode($books, JSON_PRETTY_PRINT)); + + // Update the books.xml file + $xml = new SimpleXMLElement(''); + foreach ($books as $book) { + $book_xml = $xml->addChild('book'); + $book_xml->addChild('title', $book['title']); + $book_xml->addChild('author', $book['author']); + $book_xml->addChild('publication_year', $book['publication_year']); + $book_xml->addChild('genre', $book['genre']); + } + $xml->asXML('books.xml'); + } + + // Return the results as JSON for frontend use + echo json_encode($books); + exit(); +} +?> diff --git a/validation.js b/v1 Foundation/validation.js similarity index 98% rename from validation.js rename to v1 Foundation/validation.js index 0f52322..1f36f84 100644 --- a/validation.js +++ b/v1 Foundation/validation.js @@ -1,13 +1,13 @@ -document.getElementById("addBookForm").addEventListener("submit", function (e) { - const title = document.getElementById("title").value.trim(); - const author = document.getElementById("author").value.trim(); - const publicationYear = parseInt(document.getElementById("publication_year").value); - const genre = document.getElementById("genre").value.trim(); - - if (!title || !author || !genre || isNaN(publicationYear) || publicationYear <= 2000) { - e.preventDefault(); - alert("All fields are required, and the publication year must be greater than 2000."); - } else { - alert("Book successfully added!"); - } -}); +document.getElementById("addBookForm").addEventListener("submit", function (e) { + const title = document.getElementById("title").value.trim(); + const author = document.getElementById("author").value.trim(); + const publicationYear = parseInt(document.getElementById("publication_year").value); + const genre = document.getElementById("genre").value.trim(); + + if (!title || !author || !genre || isNaN(publicationYear) || publicationYear <= 2000) { + e.preventDefault(); + alert("All fields are required, and the publication year must be greater than 2000."); + } else { + alert("Book successfully added!"); + } +});