Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Games Library Reservations Page</title>
- <link href="styles.css" rel="stylesheet" type="text/css">
- </head>
- <body>
- <div id='main'>
- <div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
- <div id='menu-wrapper'>
- <div id='menu'>
- <ul>
- <li><a href='index.php'>Home</a></li>
- <li class='current_page_item'><a href='#'>Reservations</a></li>
- </ul>
- </div>
- </div>
- <div>
- <h1>Reservations</h1>
- <?php
- // Creates variables
- $name = $gameID = $startDate = $NoOfDays = $search = "";
- // Creates error variables
- $nameErr = $gameIDErr = $startDateErr = $NoOfDaysErr= $searchErr = "";
- // Checks if the request method is post.
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- // Checks if the search submit button was clicked
- if (isset($_POST['searchSubmit'])) {
- $searchEmpty = false;
- // Checks if the user did not enter anything for their name
- if (empty($_POST["search"])) {
- // Creates a boolean for later use
- $searchEmpty = true;
- // Creates the error message that is displayed if nothing was entered.
- $searchErr = "Please enter a search value.";
- } else {
- // Reads in the search query
- $search = $_POST["search"];
- }
- } else {
- // Checks if the user did not enter anything for their name
- $empty = false;
- if (empty($_POST["name"])) {
- // Creates the error
- $empty = true;
- $nameErr = "Name is required.";
- } else {
- // Reads in the value for the name
- $name = $_POST["name"];
- }
- // Checks if the user did not enter anything for their gameID
- if (empty($_POST["gameID"])) {
- // Creates the error
- $empty = true;
- $gameIDErr = "The game ID is required";
- } else {
- // Reads in the value for the gameID
- $gameID = $_POST["gameID"];
- }
- // Checks if the user did not enter anything for the date
- if (empty($_POST["startDate"])) {
- // Creates the error
- $empty = true;
- $startDateErr = "The start date is required";
- } else {
- // Reads in the value for the start date.
- $startDate = $_POST["startDate"];
- }
- // Checks if the user did not enter anything for the number of days
- if (empty($_POST["NoOfDays"])) {
- // Creates the error
- $empty = true;
- $NoOfDaysErr = "The number of days is required.";
- } else {
- // Reads in the value for the number of days.
- $NoOfDays = $_POST["NoOfDays"];
- }
- if ($empty = true) {
- $empty = "* required field.";
- } else {
- $empty = false;
- }
- }
- }
- // Opens the file
- $file = fopen("games.csv","r");
- // Loops while it is not at the end of the file
- while(! feof($file)){
- // Reads in everything from the file and stores it in a multidimensional array
- $gameDetails[] = fgetcsv($file);
- }
- // Calculates the number of rows in the array
- $num_rows = count($gameDetails);
- // Eliminates the error in which it displays that there is an extra row
- $num_rows = $num_rows - 1;
- // Calculates the number of columns in the array
- $num_cols = max(array_map('count', $gameDetails));
- // Eliminates the error in which 1 extra column is read
- $num_cols = $num_cols;
- // Closes the file to avoid any errors
- fclose($file)
- ?>
- <!-- sets the colour of the error message to red -->
- <style>
- .error {color: #FF0000;}
- </style>
- <!-- Creates the form for the search -->
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- <!-- Sets heading as search -->
- <h2>Search:</h2>
- <!-- Creates a search form -->
- Search: <input type="text" name="search" placeholder="Enter a key word or phrase">
- <!-- Creates the error if the box is empty -->
- <span class = "error">*<?php echo $searchErr;?></span>
- <br><br>
- <!-- Creates the submit button that is different to the other one as it has a different name -->
- <input type="submit" name="searchSubmit" value="Submit">
- </form>
- <?php
- // Checks if the the server request method is post
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- // Checks if there was nothing put into the search form
- if ($searchEmpty != true);
- // Checks if the submit button was clicked and if the search form is empty
- if (isset($_POST['searchSubmit']) and $searchEmpty != true){
- // Creates a boolean
- $resultsFound= false;
- // Creates the table headings
- echo "<br><table><tr>
- <th>Game ID</th>
- <th>Genre</th>
- <th>Game Name</th>
- <th>Game Description</th>
- <th>Rental Cost per day</th>
- </tr>";
- // Creates a for loop for the table
- for($row = 0; $row < $num_rows; $row++){
- // Sets everything to lower case and checks if there are any matches
- if ((strpos(strtolower($gameDetails[$row][2]), strtolower($search)) !== false) or (strpos(strtolower($gameDetails[$row][3]), strtolower($search)) !== false)) {
- // If there were matches, then set the boolean to true
- $resultsFound = true;
- echo "<tr>";
- // Creates another for loop for the table
- for($column = 0; $column < 5; $column++)
- {
- // Creates the different columns
- if ($column == 0){ echo "<td><a href='reservations.php?GameID=".$gameDetails[$row][0]."'/>".$gameDetails[$row][0]."</td>"; }
- else if ($column == 1) {
- echo "<td><a href='reservations.php?GameID=".$gameDetails[$row][0]."'>".$gameDetails[$row][1]."</a></td>";
- } else if ($column == 2) {
- echo "<td><a href='reservations.php?GameID=".$gameDetails[$row][0]."'>".$gameDetails[$row][2]."</a></td>";
- } else if ($column == 3) {
- echo "<td><a href='reservations.php?GameID=".$gameDetails[$row][0]."'>".$gameDetails[$row][3]."</a></td>";
- } else if ($column == 4) {
- echo "<td><a href='reservations.php?GameID=".$gameDetails[$row][0]."'>".$gameDetails[$row][4]."</a></td>";
- } else {
- echo "<td>".$gameDetails[$row][5]."</td>";
- }
- }
- echo "</tr>";
- }
- }
- // Checks if the boolean was unchanged, and if so, it displays an error message saying nothing was found.
- if ($resultsFound == false) {
- echo "<tr><td colspan='5'>No Results Found</td></tr>";
- }
- echo "</table>";
- }
- }
- // Calculates the end date using the start date and number of days
- $endDate = date('Y-m-d', strtotime($startDate. " + $NoOfDays days"));
- ?>
- <br>
- <!-- sets the key that is displayed at the top of the page-->
- <p><span class="error"><?php echo $empty;?></span></p>
- <!-- sets the method to post and the action so that it can display on the same page-->
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- <h2>Reservation:</h2>
- <!-- creates the form for the name-->
- Name: <input type="text" name="name" placeholder="Enter your full name">
- <!-- creates the error for the name -->
- <span class="error">*<?php echo $nameErr;?></span>
- <br><br>
- <!-- creates the form for the gameID-->
- Game ID: <input type="number" name="gameID" min="1" placeholder="Enter the game ID" value="<?php echo $_GET['GameID']; ?>">
- <!-- creates the error for the gameID-->
- <span class="error">*<?php echo $gameIDErr;?></span>
- <br><br>
- <!-- creates the form for the start date-->
- Start Date: <input type="date" name="startDate">
- <!-- creates the error for the start date-->
- <span class="error">*<?php echo $startDateErr;?></span>
- <br><br>
- <!-- creates the from for the number of days-->
- Number Of Days: <input type="number" name="NoOfDays" min="1" max="5" placeholder="1-5" id="NoOfDays">
- <!-- creates the error for the number of days-->
- <span class="error">*<?php echo $NoOfDaysErr;?></span>
- <br><br>
- <!-- creates the submit button-->
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- // Creates the value that will be increased in the while loop
- $row = 0;
- // Creates the while loop that loops until the gameID entered is found in the table.
- while($gameDetails[$row][0] != $gameID and $row <= $num_rows) {
- $row++;
- }
- // Checks if the submit button was clicked
- if ($_POST["submit"] == "Submit") {
- $error = false;
- // Checks if the user did not enter anything in one of the forms, and if they didn't, it will set error to true.
- if ($_POST["NoOfDays"] == "") {
- $error = true;
- } elseif($_POST["name"] == "") {
- $error = true;
- } elseif($_POST["gameID"] == "") {
- $error = true;
- } elseif($_POST["startDate"] == "") {
- $error = true;
- } elseif($row>$num_rows) {
- $error = true;
- }
- }
- // Checks if the submit button was clicked
- if ($_POST["submit"] == "Submit") {
- // Checks if there were no errors in the form
- if ($error != true) {
- // Sets the variables for connection details
- $servername = "localhost";
- $username = "wessamallami";
- $password = "circle8";
- $dbname = "wallami";
- // Connects to the database
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- // Checks if the connection was unsuccessful and if it was, it displays an error
- if (!$conn) {
- echo"<br>";
- die("Sorry, connection failed. Please refresh and try again. If not, please try again later. Error: " . mysqli_connect_error());
- }
- // Creates the SQL code to check if the game is reserved already
- $checkSQL = "SELECT Game_ID
- FROM Reservations
- WHERE (Start_Date between '".$startDate."' and '".$endDate."' or End_Date between '".$startDate."' and '".$endDate."')
- AND Game_ID = '".$gameID."'";
- // Runs the SQL query
- $result = mysqli_query($conn, $checkSQL);
- $saved = false;
- // Checks if the query returned a result
- if (mysqli_num_rows($result) > 0) {
- // If it did, notfiy the user the game is reserved and do nothing more
- $reserved = true;
- $saved = false;
- } else {
- //If it did not return anything, then create the SQL query to insert the data into the database
- $sql = "INSERT INTO Reservations (Customer_Name, Game_ID, Start_Date, End_Date, Number_Of_Days_Reserved)
- VALUES ('".$name."', '".$gameID."', '".$startDate."', '".$endDate."', '".$NoOfDays."')";
- // Check if the query was run successfully
- if ($conn->query($sql)) {
- // If it was, tell the user the reservation was saved.
- $saved = true;
- } else {
- // If not, display the error to the user.
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- // Close the connection to prevent any further errors.
- $conn->close();
- }
- }
- }
- ?>
- <?php
- // Creates the error boolean
- $error = false;
- // Checks if the user clicked the submit button
- if ($_POST["submit"] == "Submit") {
- // Checks if the user did not enter anything in one of the forms, and if they didn't, it will set error to true.
- if ($_POST["NoOfDays"] == "") {
- $error = true;
- } elseif($_POST["name"] == "") {
- $error = true;
- } elseif($_POST["gameID"] == "") {
- $error = true;
- } elseif($_POST["startDate"] == "") {
- $error = true;
- // If the gameid doesn't exist it will tell the user
- } elseif($row>$num_rows) {
- $error = true;
- echo '<p style="color: red;">
- Please enter a valid game ID.
- </p>';
- } elseif($reserved != false) {
- echo '<br><br><br><p style="color: red;">
- Sorry this game is already reserved. Please try another date.
- </p>';
- } else {
- $error = false;
- // Displays the entered values
- echo "<h2>Values:</h2>";
- echo "Name: ".$name;
- echo "<br>";
- echo "Game ID: ".$gameID;
- echo "<br>";
- echo "Start Date: ".$startDate;
- echo "<br>";
- echo "Number Of Days: ".$NoOfDays;
- echo "<br>";
- // Displays the end date
- echo "End Date: ". $endDate;
- echo "<br>";
- // Displays the game name from the ID that was found in the table
- echo "Game Name: ".$gameDetails[$row][2];
- // Calculates the price of the game for one day
- $dayprice = $gameDetails[$row][4];
- // Calculates the total price
- $totalprice = $dayprice * $NoOfDays;
- echo "<br>";
- // Tells the user the price for the number of days they entered
- echo "The price for ".$NoOfDays." days is £".$totalprice.".";
- if ($saved != false) {
- echo '<br><p style="color: red;">
- Reservation saved.
- </p>';
- }
- }
- }
- ?>
- <?php
- // Checks if the submit button was clicked
- if ($_POST["submit"] == "Submit") {
- // Checks if there were no errors in the form
- if ($error = false) {
- // Sets the variables for connection details
- $servername = "localhost";
- $username = "wessamallami";
- $password = "circle8";
- $dbname = "wallami";
- // Connects to the database
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- // Checks if the connection was unsuccessful and if it was, it displays an error
- if (!$conn) {
- echo"<br>";
- die("Sorry, connection failed. Please refresh and try again. If not, please try again later. Error: " . mysqli_connect_error());
- }
- // Creates the SQL code to check if the game is reserved already
- $checkSQL = "SELECT Game_ID
- FROM Reservations
- WHERE (Start_Date between '".$startDate."' and '".$endDate."' or End_Date between '".$startDate."' and '".$endDate."')
- AND Game_ID = '".$gameID."'";
- // Runs the SQL query
- $result = mysqli_query($conn, $checkSQL);
- $saved = false;
- // Checks if the query returned a result
- if (mysqli_num_rows($result) > 0) {
- // If it did, notfiy the user the game is reserved and do nothing more
- echo '<br><br><br><p style="color: red;">
- Sorry this game is already reserved. Please try another date.
- </p>';
- $saved = false;
- } else {
- //If it did not return anything, then create the SQL query to insert the data into the database
- $sql = "INSERT INTO Reservations (Customer_Name, Game_ID, Start_Date, End_Date, Number_Of_Days_Reserved)
- VALUES ('".$name."', '".$gameID."', '".$startDate."', '".$endDate."', '".$NoOfDays."')";
- // Check if the query was run successfully
- if ($conn->query($sql)) {
- // If it was, tell the user the reservation was saved.
- $saved = true;
- } else {
- // If not, display the error to the user.
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- // Close the connection to prevent any further errors.
- $conn->close();
- }
- }
- }
- ?>
- </div>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment