Guest User

ReservationsR

a guest
Mar 26th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.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.  
  8. <style>
  9. .error {color: #FF0000;}
  10. </style>
  11.  
  12. </head>
  13.  
  14. <body>
  15. <div id='main'>
  16. <div id='titleImage'><img title='Home' src='GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
  17. <div id='menu-wrapper'>
  18. <div id='menu'>
  19. <ul>
  20.  
  21. <li><a href='IndexR.php'>Home</a></li>
  22. <li class='current_page_item'><a href='#'>Reservations</a></li>
  23. </ul>
  24. </div>
  25. </div>
  26.  
  27. <div>
  28. <h1>Reservations</h1>
  29. </div>
  30.  
  31.  
  32.  
  33. <?php
  34.  
  35. // Define variables and set to empty values.
  36. $gameID = $startDate = $name = $days = "";
  37. $gameerr = $startdateerr = $nameerr = $dayserr = $submit = $submitSearch = $searchResult = "";
  38. $validation = 0;
  39.  
  40. // Saves contents of the game file.
  41. $file = fopen("games.csv","r");
  42. if(fopen("games.csv","r")) {
  43. while(! feof($file)){
  44. $gameinfo[] = fgetcsv($file);
  45. }
  46. }
  47. fclose($file);
  48.  
  49. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  50. if (isset($_POST['searchButton'])) {
  51. // Checks if there is no user input and displays an error message if there isn't.
  52. if (empty($_POST["search"])) {
  53. $submitSearch = "No input to search for.";
  54. } else {
  55. // Converts string to lowercase so that the search isn't case sensitive.
  56. $keyword = strtolower($_POST["search"]);
  57. $rowsNumber = count($gameinfo) - 1;
  58. // Searches through the game details to find a game that matched with the keyword.
  59. for($row = 0; $row < $rowsNumber; $row++) {
  60. $currentName = strtolower($gameinfo[$row][2]);
  61. $currentDesc = strtolower($gameinfo[$row][3]);
  62. if (strpos($currentName, $keyword) !== false) {
  63. $searchResult = $gameinfo[$row][2];
  64. $searchFound = 1;
  65. } else {
  66. if (strpos($currentDesc, $keyword) !== false) {
  67. $searchResult = $gameinfo[$row][2];
  68. $searchFound = 1;
  69. }
  70. }
  71. if ($searchFound == 1) {
  72. // Displays the results of the search.
  73. $LD = $row + 1;
  74. $submitSearch = $submitSearch." <br><a href=http://tinyurl.com/jkkdzzy?GID=".$LD.">".$searchResult."</a><br>";
  75. $searchFound = 0;
  76. }
  77. }
  78. }
  79. }
  80. }
  81.  
  82. // Store contents of form in variables.
  83. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  84. if (isset($_POST['submit'])) {
  85. // Checks if there is no user input and displays an error message if there isn't.
  86. if (empty($_POST["gameID"])) {
  87. $gameerr = "Game ID is required.";
  88. $validation = 0;
  89. } else {
  90. // Saves the raw ID of the chosen game.
  91. $ID = $_POST["gameID"];
  92.  
  93. // Goes through all of the games in the file until it finds the matching index.
  94. $found = 0;
  95. $rowsNumber = count($gameinfo) - 1;
  96. for($row = 0; $row < $rowsNumber; $row++) {
  97. // If the game index is found, the search stops and the value is stored.
  98. if ($gameinfo[$row][0] == $_POST["gameID"]) {
  99. $gameID = $gameinfo[$row][2];// Stores the name of the game corresponding to the inputted game ID.
  100. $found = 1;
  101. $validation = $validation + 1;
  102. $row = $rowsNumber;
  103. }
  104. }
  105. // If the game index is not found, then an error message is displayed.
  106. if ($found == 0) {
  107. $gameerr = "Game ID doesn't exist.";
  108. $validation = 0;
  109. }
  110. }
  111. if (empty($_POST["name"])) {
  112. $nameerr = "Your name is required.";
  113. $validation = 0;
  114. } else {
  115. $name = $_POST["name"];
  116. $validation = $validation + 1;
  117. }
  118. if (empty($_POST["days"])) {
  119. $dayserr = "Amount of days is required.";
  120. $validation = 0;
  121. } else {
  122. // Checks if the number of days inputted exceeds the number 5.
  123. if ($_POST["days"] > 5) {
  124. $dayserr = "5 is the maximum number of days you can rent.";
  125. $validation = 0;
  126. } else {
  127. $days = $_POST["days"];
  128. $validation = $validation + 1;
  129. }
  130. }
  131. if (empty($_POST["startDate"])) {
  132. $startdateerr = "Start date of reservation is required.";
  133. $validation = 0;
  134. } else {
  135. $currentDate = date('Y-m-d');
  136. // Checks if the inputted date is in the past, making it invalid.
  137. if ($_POST["startDate"] > $currentDate) {
  138.  
  139. $startDate = $_POST["startDate"];// Saves the start date.
  140. $endDate = date('Y-m-d', strtotime($startDate. ' + '.$days. ' days'));// Saves the end date.
  141.  
  142. if ($validation == 3) {
  143.  
  144. $validation = 0;
  145. // Mysql database.
  146. $servername = "fdb5.awardspace.net";
  147. $username = "1967104_general";
  148. $password = "ebscomputing1";
  149. $dbname = "1967104_general";
  150.  
  151. // Creates a connection
  152. $conn = new MySQLi($servername, $username, $password, $dbname);
  153.  
  154. // Checks the connection.
  155. if ($conn->connect_error) { // Displays an error if it can't connect.
  156. die("Connection failed: " . $conn->connect_error);
  157. }
  158. // Checks if the dates overlap.
  159. $overlap = "SELECT ID
  160. FROM ReservationTable
  161. WHERE (start BETWEEN '".$startDate."' AND '".$endDate."' OR Enddate BETWEEN '".$startDate."' AND '".$endDate."')
  162. AND ID = '".$ID."'";
  163. $result = mysqli_query($conn, $overlap);
  164. if (mysqli_num_rows($result) > 0) {
  165. $submit = "Please pick another date.";
  166. } else {
  167.  
  168. //create SQL INSERT INTO query
  169. $sql = "INSERT INTO ReservationTable (ID, gameID, start, Enddate, reserve)
  170. VALUES ('".$ID."', '".$gameID."', '".$startDate."', '".$endDate."', '".$days."')";
  171.  
  172. if ($conn->query($sql) === TRUE) { // Sends the query to the database table and checks the results.
  173. // Displays the total cost of your reservation.
  174. $submit = "Reservation saved. Total cost: £".$gameinfo[$ID][4] * $days;
  175.  
  176. } else {
  177. $submit = "Error: " . $sql . "<br>" . $conn->error;
  178. }
  179. $conn->close();// Disconnects from the database.
  180. }
  181. }
  182. } else {
  183. $validation = 0;
  184. $startdateerr = "Please use a valid date.";
  185. }
  186. }
  187. }
  188. }
  189. ?>
  190.  
  191. <!-- A form used to search for a game. -->
  192. <form method="post" action"<?php echo htmlspecialchars($_SEARCH["PHP_SELF"]);?>">
  193.  
  194. Search: <input type ="text" name="search" />
  195. <br /><br />
  196.  
  197. <input type="submit" name="searchButton" value="Search" />
  198. <span class="error"> <?php echo $submitSearch;?></span>
  199. <br /><br />
  200. <br /><br />
  201.  
  202. </form>
  203.  
  204. <span class="error">* required field.</span>
  205.  
  206. <!-- A form that takes in the user input and the values are sent for processing. -->
  207. <form method="post" action"<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  208.  
  209. Game ID: <input type="text" name="gameID" value = "<?php echo $_GET['GID']?>"/>
  210. <span class="error">* <?php echo $gameerr;?></span>
  211. <br /><br />
  212.  
  213. Name: <input type="text" name="name" />
  214. <span class="error">* <?php echo $nameerr;?></span>
  215. <br /><br />
  216.  
  217. Start date of the reservation: <input type="date" name="startDate" />
  218. <span class="error">* <?php echo $startdateerr;?></span>
  219. <br /><br />
  220.  
  221. Amount of reserved days: <input type="text" name="days" />
  222. <span class="error">* <?php echo $dayserr;?></span>
  223. <br /><br />
  224.  
  225. <input type="submit" name="submit" value="Reserve">
  226. <span class="error"> <?php echo $submit;?></span>
  227. <br /><br />
  228. </form>
  229.  
  230.  
  231. <?php
  232.  
  233. // Display contents of form
  234. echo "<h2>Your Input:</h2>";
  235. echo $gameID;
  236. echo "<br>";
  237. echo $name;
  238. echo "<br>";
  239. echo $startDate;
  240. echo "<br>";
  241. echo $days;
  242.  
  243. ?>
  244. </div>
  245. </body>
  246. </html>
Add Comment
Please, Sign In to add comment