Advertisement
Guest User

Untitled

a guest
Apr 17th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.08 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'> <!--finding and loading in the logo of the website and positiong it -->
  11. <div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='GLLogo.png' /></div>
  12.  
  13. <div id='menu-wrapper'>
  14. <div id='menu'>
  15. <ul>
  16. <!--This part of the code links the two websites together so that the button takes you between them-->
  17. <li><a href='index.php'>Home</a></li>
  18. <li class='current_page_item'><a href='#'>Reservations</a></li>
  19. </ul>
  20. </div>
  21. </div>
  22.  
  23. <div>
  24. <h1>Reservations</h1>
  25. <?php
  26. //A simple loop that counts the number of rows in teh games.csv file
  27. $count = 0;
  28. $file = fopen('games.csv', 'r');
  29. while(($games[] = fgetcsv($file)) !== FALSE)
  30. {
  31. $count = $count + 1;
  32. }
  33.  
  34. fclose($file);
  35. //declerations of several variables
  36. $name = $gameID = $start = $reserve = "";
  37. $nameErr = $gameIDErr = $startrErr = $reserveErr ="";
  38. //checking whether all the forms boxes have been filled in by the user
  39.  
  40. if ($_SERVER["REQUEST_METHOD"] == "POST")
  41. {
  42. if (isset($_POST['submit']))
  43. {
  44. if(empty($_POST["name"]))
  45. {
  46. $nameErr = "Name is required";
  47. }
  48. else
  49. {
  50. $name = $_POST["name"];
  51. }
  52. if(empty($_POST["gameID"]))
  53. {
  54. $gameIDErr = "Game ID is required";
  55. }
  56. else
  57. {
  58. $gameID = $_POST["gameID"];
  59. }
  60. if(empty($_POST["start"]))
  61. {
  62. $startErr = "Start date is required";
  63. }
  64. else
  65. {
  66. $start = $_POST["start"];
  67. }
  68. if(empty($_POST["reserve"]))
  69. {
  70. $reserveErr = "Reserve is required";
  71. }
  72. else
  73. {
  74. $reserve = $_POST["reserve"];
  75. }
  76.  
  77.  
  78. //checks whether the gameID that the user entered is in fact one of the gameID that has been loaded in from the games.csv file
  79. $match = "no" ;
  80. $number_of_records = $count ;
  81. for ($i = 0;$i<$number_of_records;$i++)
  82. {
  83. if ($gameID == $games[$i][0])
  84. {
  85. $match = "yes";
  86. $gamename = $games[$i][2];
  87. $price = $games[$i][4];
  88. $validgameID = $games[$i][0];
  89.  
  90.  
  91. }
  92. }
  93. //echo statement that tells te user of a false entry inthe gameID
  94. if($match == "no")
  95. {
  96. $gameIDErr = "Game ID does not exist";
  97. }
  98.  
  99. //declaring all of the database information including the address, name and password
  100. $servername = "fdb5.awardspace.net";
  101. $username = "1972286_db1";
  102. $password = "ebscomputing1";
  103. $dbname = "1972286_db1";
  104. if ($match == "yes")
  105. {
  106. //checking whether the database has been connected and continueing or displaying a message depending on whether it has or not
  107. $conn = new MySQLi($servername, $username, $password, $dbname);
  108. if ($conn->connect_error)
  109. {
  110. //connection failed warning
  111. die("connection failed: " . $conn->connect_error);
  112. }
  113. else
  114. {
  115. //calcuates the end date of the users reservation so that the program can check whether that game is already reserved
  116. $Enddate = date('Y-m-d', strtotime($start." + $reserve days"));
  117. //first line checks for overlapping dates
  118. //second line checks where new booking starts before and ends after existing
  119. $sql = "SELECT gameID
  120. FROM reservation
  121. WHERE ((start BETWEEN'".$start."' and '".$Enddate."' OR Enddate between'".$start."' and '".$Enddate."') OR
  122. (start >'".$start."' and Enddate <'".$Enddate."'))
  123. AND gameID = '".$gameID."'";
  124.  
  125. $results = $conn->query($sql);
  126. //echo "no of records found ".$results->num_rows."<br>";
  127.  
  128. if($results->num_rows>0)
  129. {
  130. //prints a red message that alerts the user that reservation cannot be made on those dates
  131. echo '<p style="color: red; text-align">*already reserved on those dates, try again*</p>';
  132. echo "<br>";
  133. }
  134. else
  135. {
  136. //inserts the details of the reservations that have already been checked and adds them into the database
  137. $sql = "INSERT INTO reservation (name, gameID, start, reserve, Enddate)
  138. VALUES('".$name."','".$gameID."','".$start."','".$reserve."','".$Enddate."')";
  139. if($conn->query($sql) === TRUE)
  140. { //printing a message to the user that states that the record has been successfuly saved
  141. echo '<p style="color: green; text-align">*New record successfully created*</p>';
  142. }
  143. else
  144. {
  145. //printing a message that tells the user the record has not been saved correctly
  146. echo "Error: ", "<br>" .$conn->error;
  147. }
  148. }
  149.  
  150. $conn->close();
  151. }
  152. }
  153. else
  154. {
  155. //tells the user that they have entered a wrong entry eg. the gameID was incorrect
  156. echo '<p style="color: red; text-align">*sorry, record not saved due to a wrong entry*</p>';
  157. }
  158.  
  159. }
  160. }
  161.  
  162.  
  163. ?>
  164.  
  165.  
  166.  
  167. <!--some simple statements that change the colour of text to make it more user friendly-->
  168. <style>
  169. .error {color: #FF0000;}
  170. .record_Err {color: #FF0000;}
  171. </style>
  172. <!--creating the front end of the website as a form that the user will fill in to record a reservation-->
  173. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  174.  
  175. <p>Name:</p>
  176. <p> <!--defining the name and type of each item the user will fill in-->
  177. <input type="text" name="name">
  178. <span class="error">* <?php echo $nameErr;?></span>
  179. <br><br>
  180. gameID:</p>
  181. <p>
  182. <input type="text" name="gameID">
  183. <span class="error">* <?php echo $gameIDErr;?></span>
  184. <br><br>
  185. Start date of reservation: </p>
  186. <p>
  187. <input type="date" name="start">
  188. <span class="error">* <?php echo $startErr;?></span>
  189. <br>
  190. <p>Number of days for reservation:<br>
  191. <p>
  192. <input type="number" name="reserve" min="1" max="5">
  193. <span class="error">* <?php echo $reserveErr;?></span>
  194. <br><br>
  195. <input type="submit" name="submit" value="Submit"><br><br>
  196. <br>keyword:
  197. <input type="text" name="keyword"><br>
  198. <input type="submit" name="search" value="search"><div id='searchImage'><img title='Home' src='images/Magnifying_glass.png' width='40' height='30' alt='Magnifying_glass.png' /></div>
  199. </p>
  200.  
  201. </p>
  202. </form>
  203.  
  204. <?php
  205.  
  206. if (isset($_POST['search']))
  207. {
  208. //checking that the user actually inputted a keyword
  209. $keyword = $_POST["keyword"];
  210. echo "keyword entered was:" .$keyword;
  211. if ($keyword == "")
  212. {
  213. echo "<br>", "no search";
  214. }
  215.  
  216. else
  217. {
  218. echo "<br>", "these are the searches found:";
  219. ?>
  220. <!--loading in the table for the search results-->
  221. <table>
  222. <tr>
  223. <th>Game ID</th>
  224. <th>Genre</th>
  225. <th>Game Name</th>
  226. <th>Game Description</th>
  227. <th>Rental Cost per day</th>
  228. </tr>
  229.  
  230. <?php
  231. //searches the games variable that contains all the games for games that contain the keyword in the name,description and genre
  232. for($row =0; $row<= $count; $row++)
  233. {
  234. //checking if the keyword comes up in any of the 3 columns
  235. if ((strpos($games[$row][1],$keyword)!== FALSE) or (strpos($games[$row][2],$keyword)!== FALSE) or (strpos($games[$row][3],$keyword)!== FALSE))
  236. {
  237. echo"<tr>";
  238. for($col =0; $col <= 4; $col++)
  239. {
  240. if ($col == 4)
  241. {//displaying the pound sign for appropriate columns
  242. echo"<td> £".$games[$row][$col]."</td>";
  243. }
  244. else
  245. {
  246. echo"<td>". $games[$row][$col]."</td>";
  247. }
  248.  
  249. }
  250. }
  251. }
  252. ?>
  253. </table>
  254. <?php
  255. }
  256. }
  257. ?>
  258.  
  259. <?php
  260. //presenting the user with all teh information that they have entered as well as showing them the total price that their reservation will cost
  261. echo $name;
  262. echo"<br>";
  263. echo $gameID;
  264. echo"<br>";
  265. echo $start;
  266. echo"<br>";
  267. echo $reserve;
  268. echo"<br>";
  269. echo $match;
  270. echo"<br>";
  271. echo $gamename;
  272. $totalprice = $price * $reserve;
  273. echo "<br>";
  274. echo "The price for ".$reserve." days is £".$totalprice.".";
  275. echo "<br>";
  276. echo $Enddate;
  277.  
  278. ?>
  279.  
  280. </div>
  281.  
  282. </body>
  283. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement