Guest User

Peanut Butter Sandwich

a guest
Mar 8th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Games Library Reservations Page</title>
  6. <link href="styles.css" rel="stylesheet" type="text/css">
  7. </head>
  8.  
  9. <body>
  10. <div id='main'>
  11. <div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
  12. <div id='menu-wrapper'>
  13. <div id='menu'>
  14. <ul>
  15. <li><a href='index.php'>Home</a></li>
  16. <li class='current_page_item'><a href='#'>Reservations</a></li>
  17. </ul>
  18. </div>
  19. </div>
  20.  
  21. <div>
  22. <h1>Reservations</h1>
  23. <?php
  24. // Creates variables
  25. $name = $gameID = $startDate = $NoOfDays = "";
  26. // Creates error variables
  27. $nameErr = $gameIDErr = $startDateErr = $NoOfDaysErr = "";
  28. // Checks if the request method is post.
  29. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  30. // Checks if the user did not enter anything for their name
  31. if (empty($_POST["name"])) {
  32. // Creates the error
  33. $nameErr = "Name is required.";
  34. } else {
  35. // Reads in the value for the name
  36. $name = $_POST["name"];
  37. }
  38. // Checks if the user did not enter anything for their gameID
  39. if (empty($_POST["gameID"])) {
  40. // Creates the error
  41. $gameIDErr = "The game ID is required";
  42. } else {
  43. // Reads in the value for the gameID
  44. $gameID = $_POST["gameID"];
  45. }
  46. // Checks if the user did not enter anything for the date
  47. if (empty($_POST["startDate"])) {
  48. // Creates the error
  49. $startDateErr = "The start date is required";
  50. } else {
  51. // Reads in the value for the start date.
  52. $startDate = $_POST["startDate"];
  53. }
  54. // Checks if the user did not enter anything for the number of days
  55. if (empty($_POST["NoOfDays"])) {
  56. // Creates the error
  57. $NoOfDaysErr = "The number of days is required.";
  58. } else {
  59. // Reads in the value for the number of days.
  60. $NoOfDays = $_POST["NoOfDays"];
  61. }
  62. }
  63. // Opens the file
  64. $file = fopen("games.csv","r");
  65. // Loops while it is not at the end of the file
  66. while(! feof($file)){
  67. // Reads in everything from the file and stores it in a multidimensional array
  68. $gameDetails[] = fgetcsv($file);
  69. }
  70. // Calculates the number of rows in the array
  71. $num_rows = count($gameDetails);
  72. // Eliminates the error in which it displays that there is an extra row
  73. $num_rows = $num_rows - 1;
  74. // Closes the file to avoid any errors
  75. fclose($file)
  76. ?>
  77. <!-- sets the colour of the error message to red -->
  78. <style>
  79. .error {color: #FF0000;}
  80. </style>
  81. <!-- sets the key that is displayed at the top of the page-->
  82. <p><span class="error">* required field.</span></p>
  83. <!-- sets the method to post and the action so that it can display on the same page-->
  84. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  85. <!-- creates the form for the name-->
  86. Name: <input type="text" name="name">
  87. <!-- creates the error for the name -->
  88. <span class="error">*<?php echo $nameErr;?></span>
  89. <br><br>
  90. <!-- creates the form for the gameID-->
  91. Game ID: <input type="number" name="gameID" min="1">
  92. <!-- creates the error for the gameID-->
  93. <span class="error">*<?php echo $gameIDErr;?></span>
  94. <br><br>
  95. <!-- creates the form for the start date-->
  96. Start Date: <input type="date" name="startDate">
  97. <!-- creates the error for the start date-->
  98. <span class="error">*<?php echo $startDateErr;?></span>
  99. <br><br>
  100. <!-- creates the from for the number of days-->
  101. Number Of Days: <input type="number" name="NoOfDays" min="1" max="5" <id="NoOfDays">
  102. <!-- creates the error for the number of days-->
  103. <span class="error">*<?php echo $NoOfDaysErr;?></span>
  104. <br><br>
  105. <!-- creates the submit button-->
  106. <input type="submit" name="submit" value="Submit">
  107. </form>
  108. <?php
  109. // Creates the error boolean
  110. $error = false;
  111. // Creates the value that will be increased in the while loop
  112. $row = 0;
  113. // Creates the while loop that loops until the gameID entered is found in the table.
  114. while($gameDetails[$row][0] != $gameID and $row <= $num_rows) {
  115. $row++;
  116. }
  117. // Checks if the user clicked the submit button
  118. if ($_POST["submit"] == "Submit") {
  119. // Checks if the user did not enter anything in one of the forms, and if they didn't, it will set error to true.
  120. if ($_POST["NoOfDays"] == "") {
  121. $error = true;
  122. } elseif($_POST["name"] == "") {
  123. $error = true;
  124. } elseif($_POST["gameID"] == "") {
  125. $error = true;
  126. } elseif($_POST["startDate"] == "") {
  127. $error = true;
  128. // If the gameid doesn't exist it will tell the user
  129. } elseif($row>$num_rows) {
  130. $error = true;
  131. echo "Please enter a valid game ID.";
  132. } else {
  133. $error = false;
  134. // Displays the entered values
  135. echo "<h2>Values:</h2>";
  136. echo "Name: ".$name;
  137. echo "<br>";
  138. echo "Game ID: ".$gameID;
  139. echo "<br>";
  140. echo "Start Date: ".$startDate;
  141. echo "<br>";
  142. echo "Number Of Days: ".$NoOfDays;
  143. echo "<br>";
  144. $endDate = date('Y-m-d', strtotime($startDate. " + $NoOfDays days"));
  145. echo "End Date: ". $endDate;
  146. echo "<br>";
  147. // Displays the game name from the ID that was found in the table
  148. echo "Game Name: ".$gameDetails[$row][2];
  149. // Calculates the price of the game for one day
  150. $dayprice = $gameDetails[$row][4];
  151. // Calculates the total price
  152. $totalprice = $dayprice * $NoOfDays;
  153. echo "<br>";
  154. // Tells the user the price for the number of days they entered
  155. echo "The price for ".$NoOfDays." days is £".$totalprice.".";
  156. }
  157. }
  158. ?>
  159.  
  160.  
  161.  
  162.  
  163. <?php
  164. if ($_POST["submit"] == "Submit") {
  165. if ($error != true) {
  166. $servername = "localhost";
  167. $username = "wessamallami";
  168. $password = "circle8";
  169. $dbname = "wallami";
  170. $conn = mysqli_connect($servername, $username, $password, $dbname);
  171. if (!$conn) {
  172. die("Connection failed: " . mysqli_connect_error());
  173. }
  174. $checkSQL = "SELECT Game_ID
  175. FROM Reservations
  176. WHERE (Start_Date between '".$startDate."' and '".$endDate."' or End_Date between '".$StartDate."' and '".$EndDate."')
  177. AND Game_ID = '".$gameID."'";
  178. $result = mysqli_query($conn, $checkSQL);
  179. if (mysqli_num_rows($result) > 0) {
  180. $reserved = true;
  181. echo "<br><br>Sorry, this game is already reserved during these days. Please try another date.";
  182. } else {
  183. $sql = "INSERT INTO Reservations (Customer_Name, Game_ID, Start_Date, End_Date, Number_Of_Days_Reserved)
  184. VALUES ('".$name."', '".$gameID."', '".$startDate."', '".$endDate."', '".$NoOfDays."')";
  185. if ($conn->query($sql)) {
  186. echo "<br><br> Reservation Saved.";
  187. } else {
  188. echo "Error: " . $sql . "<br>" . $conn->error;
  189. }
  190. $conn->close();
  191. }
  192. }
  193. }
  194. ?>
  195.  
  196.  
  197.  
  198. </div>
  199.  
  200. </div>
  201.  
  202. </body>
  203. </html>
Add Comment
Please, Sign In to add comment