Guest User

Untitled

a guest
Mar 17th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.80 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 = $search = "";
  26. // Creates error variables
  27. $nameErr = $gameIDErr = $startDateErr = $NoOfDaysErr= $searchErr = "";
  28. // Checks if the request method is post.
  29. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  30. // Checks if the search submit button was clicked
  31. if (isset($_POST['searchSubmit'])) {
  32. $searchEmpty = false;
  33. // Checks if the user did not enter anything for their name
  34. if (empty($_POST["search"])) {
  35. // Creates a boolean for later use
  36. $searchEmpty = true;
  37. // Creates the error message that is displayed if nothing was entered.
  38. $searchErr = "Please enter a search value.";
  39. } else {
  40. // Reads in the search query
  41. $search = $_POST["search"];
  42. }
  43. } else {
  44. // Checks if the user did not enter anything for their name
  45. $empty = false;
  46. if (empty($_POST["name"])) {
  47. // Creates the error
  48. $empty = true;
  49. $nameErr = "Name is required.";
  50. } else {
  51. // Reads in the value for the name
  52. $name = $_POST["name"];
  53. }
  54. // Checks if the user did not enter anything for their gameID
  55. if (empty($_POST["gameID"])) {
  56. // Creates the error
  57. $empty = true;
  58. $gameIDErr = "The game ID is required";
  59. } else {
  60. // Reads in the value for the gameID
  61. $gameID = $_POST["gameID"];
  62. }
  63. // Checks if the user did not enter anything for the date
  64. if (empty($_POST["startDate"])) {
  65. // Creates the error
  66. $empty = true;
  67. $startDateErr = "The start date is required";
  68. } else {
  69. // Reads in the value for the start date.
  70. $startDate = $_POST["startDate"];
  71. }
  72. // Checks if the user did not enter anything for the number of days
  73. if (empty($_POST["NoOfDays"])) {
  74. // Creates the error
  75. $empty = true;
  76. $NoOfDaysErr = "The number of days is required.";
  77. } else {
  78. // Reads in the value for the number of days.
  79. $NoOfDays = $_POST["NoOfDays"];
  80. }
  81. if ($empty = true) {
  82. $empty = "* required field.";
  83. } else {
  84. $empty = false;
  85. }
  86. }
  87. }
  88. // Opens the file
  89. $file = fopen("games.csv","r");
  90. // Loops while it is not at the end of the file
  91. while(! feof($file)){
  92. // Reads in everything from the file and stores it in a multidimensional array
  93. $gameDetails[] = fgetcsv($file);
  94. }
  95. // Calculates the number of rows in the array
  96. $num_rows = count($gameDetails);
  97. // Eliminates the error in which it displays that there is an extra row
  98. $num_rows = $num_rows - 1;
  99. // Calculates the number of columns in the array
  100. $num_cols = max(array_map('count', $gameDetails));
  101. // Eliminates the error in which 1 extra column is read
  102. $num_cols = $num_cols;
  103. // Closes the file to avoid any errors
  104. fclose($file)
  105. ?>
  106. <!-- sets the colour of the error message to red -->
  107. <style>
  108. .error {color: #FF0000;}
  109. </style>
  110. <!-- Creates the form for the search -->
  111. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  112. <!-- Sets heading as search -->
  113. <h2>Search:</h2>
  114. <!-- Creates a search form -->
  115. Search: <input type="text" name="search" placeholder="Enter a key word or phrase">
  116. <!-- Creates the error if the box is empty -->
  117. <span class = "error">*<?php echo $searchErr;?></span>
  118. <br><br>
  119. <!-- Creates the submit button that is different to the other one as it has a different name -->
  120. <input type="submit" name="searchSubmit" value="Submit">
  121. </form>
  122.  
  123. <?php
  124. // Checks if the the server request method is post
  125. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  126. // Checks if there was nothing put into the search form
  127. if ($searchEmpty != true);
  128. // Checks if the submit button was clicked and if the search form is empty
  129. if (isset($_POST['searchSubmit']) and $searchEmpty != true){
  130. // Creates a boolean
  131. $resultsFound= false;
  132. // Creates the table headings
  133. echo "<br><table><tr>
  134. <th>Game ID</th>
  135. <th>Genre</th>
  136. <th>Game Name</th>
  137. <th>Game Description</th>
  138. <th>Rental Cost per day</th>
  139. </tr>";
  140. // Creates a for loop for the table
  141. for($row = 0; $row < $num_rows; $row++){
  142. // Sets everything to lower case and checks if there are any matches
  143. if ((strpos(strtolower($gameDetails[$row][2]), strtolower($search)) !== false) or (strpos(strtolower($gameDetails[$row][3]), strtolower($search)) !== false)) {
  144. // If there were matches, then set the boolean to true
  145. $resultsFound = true;
  146. echo "<tr>";
  147. // Creates another for loop for the table
  148. for($column = 0; $column < 5; $column++)
  149. {
  150. // Creates the different columns
  151. if ($column == 0){ echo "<td><a href='reservations.php?GameID=".$gameDetails[$row][0]."'/>".$gameDetails[$row][0]."</td>"; }
  152. else if ($column == 1) {
  153. echo "<td><a href='reservations.php?GameID=".$gameDetails[$row][0]."'>".$gameDetails[$row][1]."</a></td>";
  154. } else if ($column == 2) {
  155. echo "<td><a href='reservations.php?GameID=".$gameDetails[$row][0]."'>".$gameDetails[$row][2]."</a></td>";
  156. } else if ($column == 3) {
  157. echo "<td><a href='reservations.php?GameID=".$gameDetails[$row][0]."'>".$gameDetails[$row][3]."</a></td>";
  158. } else if ($column == 4) {
  159. echo "<td><a href='reservations.php?GameID=".$gameDetails[$row][0]."'>".$gameDetails[$row][4]."</a></td>";
  160. } else {
  161. echo "<td>".$gameDetails[$row][5]."</td>";
  162. }
  163. }
  164. echo "</tr>";
  165. }
  166. }
  167. // Checks if the boolean was unchanged, and if so, it displays an error message saying nothing was found.
  168. if ($resultsFound == false) {
  169. echo "<tr><td colspan='5'>No Results Found</td></tr>";
  170. }
  171. echo "</table>";
  172. }
  173. }
  174.  
  175. // Calculates the end date using the start date and number of days
  176. $endDate = date('Y-m-d', strtotime($startDate. " + $NoOfDays days"));
  177. ?>
  178.  
  179. <br>
  180. <!-- sets the key that is displayed at the top of the page-->
  181. <p><span class="error"><?php echo $empty;?></span></p>
  182. <!-- sets the method to post and the action so that it can display on the same page-->
  183. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  184. <h2>Reservation:</h2>
  185. <!-- creates the form for the name-->
  186. Name: <input type="text" name="name" placeholder="Enter your full name">
  187. <!-- creates the error for the name -->
  188. <span class="error">*<?php echo $nameErr;?></span>
  189. <br><br>
  190. <!-- creates the form for the gameID-->
  191. Game ID: <input type="number" name="gameID" min="1" placeholder="Enter the game ID" value="<?php echo $_GET['GameID']; ?>">
  192. <!-- creates the error for the gameID-->
  193. <span class="error">*<?php echo $gameIDErr;?></span>
  194. <br><br>
  195. <!-- creates the form for the start date-->
  196. Start Date: <input type="date" name="startDate">
  197. <!-- creates the error for the start date-->
  198. <span class="error">*<?php echo $startDateErr;?></span>
  199. <br><br>
  200. <!-- creates the from for the number of days-->
  201. Number Of Days: <input type="number" name="NoOfDays" min="1" max="5" placeholder="1-5" id="NoOfDays">
  202. <!-- creates the error for the number of days-->
  203. <span class="error">*<?php echo $NoOfDaysErr;?></span>
  204. <br><br>
  205. <!-- creates the submit button-->
  206. <input type="submit" name="submit" value="Submit">
  207. </form>
  208.  
  209. <?php
  210. // Creates the value that will be increased in the while loop
  211. $row = 0;
  212. // Creates the while loop that loops until the gameID entered is found in the table.
  213. while($gameDetails[$row][0] != $gameID and $row <= $num_rows) {
  214. $row++;
  215. }
  216. // Checks if the submit button was clicked
  217. if ($_POST["submit"] == "Submit") {
  218. $error = false;
  219. // Checks if the user did not enter anything in one of the forms, and if they didn't, it will set error to true.
  220. if ($_POST["NoOfDays"] == "") {
  221. $error = true;
  222. } elseif($_POST["name"] == "") {
  223. $error = true;
  224. } elseif($_POST["gameID"] == "") {
  225. $error = true;
  226. } elseif($_POST["startDate"] == "") {
  227. $error = true;
  228. } elseif($row>$num_rows) {
  229. $error = true;
  230. }
  231. }
  232.  
  233. // Checks if the submit button was clicked
  234. if ($_POST["submit"] == "Submit") {
  235. // Checks if there were no errors in the form
  236. if ($error != true) {
  237. // Sets the variables for connection details
  238. $servername = "localhost";
  239. $username = "wessamallami";
  240. $password = "circle8";
  241. $dbname = "wallami";
  242. // Connects to the database
  243. $conn = mysqli_connect($servername, $username, $password, $dbname);
  244. // Checks if the connection was unsuccessful and if it was, it displays an error
  245. if (!$conn) {
  246. echo"<br>";
  247. die("Sorry, connection failed. Please refresh and try again. If not, please try again later. Error: " . mysqli_connect_error());
  248. }
  249. // Creates the SQL code to check if the game is reserved already
  250. $checkSQL = "SELECT Game_ID
  251. FROM Reservations
  252. WHERE (Start_Date between '".$startDate."' and '".$endDate."' or End_Date between '".$startDate."' and '".$endDate."')
  253. AND Game_ID = '".$gameID."'";
  254. // Runs the SQL query
  255. $result = mysqli_query($conn, $checkSQL);
  256. $saved = false;
  257. // Checks if the query returned a result
  258. if (mysqli_num_rows($result) > 0) {
  259. // If it did, notfiy the user the game is reserved and do nothing more
  260. $reserved = true;
  261. $saved = false;
  262. } else {
  263. //If it did not return anything, then create the SQL query to insert the data into the database
  264. $sql = "INSERT INTO Reservations (Customer_Name, Game_ID, Start_Date, End_Date, Number_Of_Days_Reserved)
  265. VALUES ('".$name."', '".$gameID."', '".$startDate."', '".$endDate."', '".$NoOfDays."')";
  266. // Check if the query was run successfully
  267. if ($conn->query($sql)) {
  268. // If it was, tell the user the reservation was saved.
  269. $saved = true;
  270. } else {
  271. // If not, display the error to the user.
  272. echo "Error: " . $sql . "<br>" . $conn->error;
  273. }
  274. // Close the connection to prevent any further errors.
  275. $conn->close();
  276. }
  277. }
  278. }
  279. ?>
  280.  
  281.  
  282. <?php
  283. // Creates the error boolean
  284. $error = false;
  285. // Checks if the user clicked the submit button
  286. if ($_POST["submit"] == "Submit") {
  287. // Checks if the user did not enter anything in one of the forms, and if they didn't, it will set error to true.
  288. if ($_POST["NoOfDays"] == "") {
  289. $error = true;
  290. } elseif($_POST["name"] == "") {
  291. $error = true;
  292. } elseif($_POST["gameID"] == "") {
  293. $error = true;
  294. } elseif($_POST["startDate"] == "") {
  295. $error = true;
  296. // If the gameid doesn't exist it will tell the user
  297. } elseif($row>$num_rows) {
  298. $error = true;
  299. echo '<p style="color: red;">
  300. Please enter a valid game ID.
  301. </p>';
  302. } elseif($reserved != false) {
  303. echo '<br><br><br><p style="color: red;">
  304. Sorry this game is already reserved. Please try another date.
  305. </p>';
  306. } else {
  307. $error = false;
  308. // Displays the entered values
  309. echo "<h2>Values:</h2>";
  310. echo "Name: ".$name;
  311. echo "<br>";
  312. echo "Game ID: ".$gameID;
  313. echo "<br>";
  314. echo "Start Date: ".$startDate;
  315. echo "<br>";
  316. echo "Number Of Days: ".$NoOfDays;
  317. echo "<br>";
  318. // Displays the end date
  319. echo "End Date: ". $endDate;
  320. echo "<br>";
  321. // Displays the game name from the ID that was found in the table
  322. echo "Game Name: ".$gameDetails[$row][2];
  323. // Calculates the price of the game for one day
  324. $dayprice = $gameDetails[$row][4];
  325. // Calculates the total price
  326. $totalprice = $dayprice * $NoOfDays;
  327. echo "<br>";
  328. // Tells the user the price for the number of days they entered
  329. echo "The price for ".$NoOfDays." days is £".$totalprice.".";
  330. if ($saved != false) {
  331. echo '<br><p style="color: red;">
  332. Reservation saved.
  333. </p>';
  334. }
  335. }
  336. }
  337. ?>
  338.  
  339. <?php
  340.  
  341. // Checks if the submit button was clicked
  342. if ($_POST["submit"] == "Submit") {
  343. // Checks if there were no errors in the form
  344. if ($error = false) {
  345. // Sets the variables for connection details
  346. $servername = "localhost";
  347. $username = "wessamallami";
  348. $password = "circle8";
  349. $dbname = "wallami";
  350. // Connects to the database
  351. $conn = mysqli_connect($servername, $username, $password, $dbname);
  352. // Checks if the connection was unsuccessful and if it was, it displays an error
  353. if (!$conn) {
  354. echo"<br>";
  355. die("Sorry, connection failed. Please refresh and try again. If not, please try again later. Error: " . mysqli_connect_error());
  356. }
  357. // Creates the SQL code to check if the game is reserved already
  358. $checkSQL = "SELECT Game_ID
  359. FROM Reservations
  360. WHERE (Start_Date between '".$startDate."' and '".$endDate."' or End_Date between '".$startDate."' and '".$endDate."')
  361. AND Game_ID = '".$gameID."'";
  362. // Runs the SQL query
  363. $result = mysqli_query($conn, $checkSQL);
  364. $saved = false;
  365. // Checks if the query returned a result
  366. if (mysqli_num_rows($result) > 0) {
  367. // If it did, notfiy the user the game is reserved and do nothing more
  368. echo '<br><br><br><p style="color: red;">
  369. Sorry this game is already reserved. Please try another date.
  370. </p>';
  371. $saved = false;
  372. } else {
  373. //If it did not return anything, then create the SQL query to insert the data into the database
  374. $sql = "INSERT INTO Reservations (Customer_Name, Game_ID, Start_Date, End_Date, Number_Of_Days_Reserved)
  375. VALUES ('".$name."', '".$gameID."', '".$startDate."', '".$endDate."', '".$NoOfDays."')";
  376. // Check if the query was run successfully
  377. if ($conn->query($sql)) {
  378. // If it was, tell the user the reservation was saved.
  379. $saved = true;
  380. } else {
  381. // If not, display the error to the user.
  382. echo "Error: " . $sql . "<br>" . $conn->error;
  383. }
  384. // Close the connection to prevent any further errors.
  385. $conn->close();
  386. }
  387. }
  388. }
  389. ?>
  390.  
  391.  
  392. </div>
  393.  
  394. </div>
  395.  
  396. </body>
  397. </html>
Add Comment
Please, Sign In to add comment