Advertisement
Guest User

Untitled

a guest
Dec 29th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.98 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 Home 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 class='current_page_item'><a href='#'>Home</a></li>
  16. <li><a href='reservations.php'>Reservations</a></li>
  17. </ul>
  18. </div>
  19. </div>
  20.  
  21. <?php
  22. //check file exists
  23. if (file_exists('games.csv')){
  24. //open the csv file as read
  25. $file = fopen('games.csv', 'r');
  26. //Read the file into an array
  27. while(! feof($file)){
  28. $game_list[] = fgetcsv($file);
  29.  
  30. }
  31. fclose($file);
  32. } else {
  33. //Displays an error message to the user if the list of games can't be found
  34. die ("<br>Sorry the information cannot be displayed at this time.");
  35. }
  36.  
  37.  
  38.  
  39. ?>
  40.  
  41. <div id='tables'>
  42. <h1>Games</h1>
  43.  
  44. <?php
  45.  
  46. // Displays name of columns for the table
  47. echo "<table>
  48.  
  49. <tr>
  50. <th>Game ID</th>
  51. <th>Genre</th>
  52. <th>Game Name</th>
  53. <th>Game Description</th>
  54. <th>Rental Cost Per Day</th>
  55. </tr>";
  56.  
  57. // Set number of table rows
  58. $num_rows = count($game_list) - 1;
  59. // Set number of table columns
  60. $num_cols = 5;
  61.  
  62. // Start loop to generate rows
  63. for($row = 0; $row < $num_rows; $row++) {
  64. // Generate row HTML
  65. echo "<tr>";
  66.  
  67. //Start loop to generate columns (nested FOR loop!)
  68. for($col = 0 ; $col < $num_cols; $col++) {
  69. // Generate column HTML
  70. echo "<td>". $game_list[$row][$col] ."</td>";
  71. }
  72. // End of columns loop
  73. // Generate end of row HTML
  74. echo "</tr>";
  75. }
  76.  
  77. echo "</table>";
  78. ?>
  79.  
  80.  
  81.  
  82. </div>
  83.  
  84.  
  85. </div>
  86.  
  87. </body>
  88. </html>
  89.  
  90.  
  91.  
  92.  
  93.  
  94. ------------------------------------------Page 2----------------------------------------------
  95.  
  96. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  97. <html xmlns="http://www.w3.org/1999/xhtml">
  98. <head>
  99. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  100. <title>Games Library Reservations Page</title>
  101. <link href="styles.css" rel="stylesheet" type="text/css">
  102. </head>
  103.  
  104. <body>
  105. <div id='main'>
  106. <div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
  107. <div id='menu-wrapper'>
  108. <div id='menu'>
  109. <ul>
  110.  
  111. <li><a href='index.php'>Home</a></li>
  112. <li class='current_page_item'><a href='#'>Reservations</a></li>
  113. </ul>
  114. </div>
  115. </div>
  116. <?php
  117. //check file exists
  118. if (file_exists('games.csv')){
  119. //open the csv file as read
  120. $file = fopen('games.csv', 'r');
  121. //Read the file into an array
  122. while(! feof($file)){
  123. $game_list[] = fgetcsv($file);
  124.  
  125. }
  126. fclose($file);
  127.  
  128. } else {
  129. //Displays an error message to the user if the list of games can't be found
  130. die ("<br>Sorry this information cannot be displayed at this time.");
  131. }
  132.  
  133. ?>
  134.  
  135. <?php
  136. $found = false;
  137. $num_rows = count($game_list);
  138. $rows = 0;
  139. $row = 0;
  140. $cols = 0;
  141. $game_price = 0;
  142.  
  143.  
  144. //store contents of form un variables
  145. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  146. //If statement to check the user has entered a game ID.
  147. if (empty($_POST["gameid"])) {
  148. $nameErr = "The game ID is required";
  149. } else {
  150. $row = 0;
  151. $gameid = $_POST["gameid"];
  152. while ($found != true and $row < $num_rows) { // While loop to find out if the game ID that has been entered is valid.
  153. if ($gameid == $game_list[$row][$cols]){
  154. $found = true;
  155. } else{
  156. $row = 1 +$row;
  157. }
  158. }
  159. // If statement to find the game name using the game ID that has been entered by the user.
  160. if ($found == true) {
  161. //Puts name into variable
  162. $game_name = $game_list[$row][2];
  163.  
  164. }else{
  165. echo "Please enter a valid game ID";
  166. }
  167. }
  168.  
  169. }
  170. if (empty($_POST["name"])) {
  171. $nameErr = "Name is required";
  172. } else {
  173. $name = $_POST["name"];
  174. }
  175. if (empty($_POST["startdate"])) {
  176. $startdateErr = "Date is required";
  177. } else {
  178. $startdate = $_POST["startdate"];
  179. }
  180. if (empty($_POST["days"])) {
  181. $daysErr = "days is required";
  182. } else {
  183. $days = $_POST["days"];
  184. //If statement to check the game ID has been found
  185. if ($found == true) {
  186. //Calculates price using the game ID ans amount of days entered.
  187. $game_price = $game_list[$row][4];
  188. $total = $game_price * $days;
  189.  
  190. }
  191.  
  192. }
  193. $num_rows = count($game_list);
  194.  
  195. if ($_SERVER['REQUEST_METHOD'] == "POST"){
  196.  
  197. if (empty($_POST["search"])) {
  198.  
  199. } else {
  200.  
  201.  
  202. $search = $_POST["search"];
  203. $num_row = count($game_list);
  204. $successful = false;
  205.  
  206. $row = 0;
  207.  
  208.  
  209. //Displays column rows
  210. echo "<table>
  211.  
  212. <tr>
  213. <th>Game ID</th>
  214. <th>Genre</th>
  215. <th>Game Name</th>
  216. <th>Game Description</th>
  217. <th>Rental Cost Per Day</th>
  218. </tr>";
  219.  
  220. //For loop to find the keyword in the list of games array
  221. for ($row = 0; $row < $num_row; $row++){
  222.  
  223. //Checks to see if the keyword searched is in a game description or name
  224. if ((strpos(strtolower($game_list[$row][2]), strtolower($search)) !== false) or (strpos(strtolower($game_list[$row][3]), strtolower($search)) !== false)){
  225. $successfull = true;
  226.  
  227. // Set number of table columns
  228. $num_cols = 5;
  229.  
  230.  
  231.  
  232. // Generate row HTML
  233. echo "<tr>";
  234.  
  235. //Start loop to generate columns (nested FOR loop!)
  236. for($col = 0 ; $col < $num_cols; $col++) {
  237. // Generate column HTML
  238. echo "<td>". $game_list[$row][$col] ."</td>";
  239. }
  240. // End of columns loop
  241. // Generate end of row HTML
  242. echo "</tr>";
  243. $successful = true;
  244.  
  245.  
  246. }
  247. }
  248.  
  249.  
  250. if ($successful == false){
  251. echo "<tr>";
  252. echo "<br><b><center>No results were found.</center></b><br>";
  253. echo "</tr>";
  254. }
  255.  
  256.  
  257.  
  258. echo "</table>";
  259.  
  260. }
  261. }
  262.  
  263.  
  264. ?>
  265. <div>
  266. <h1>Reservations</h1>
  267. <h2>Search for a game</h2>
  268.  
  269. <!-- Form for user search -->
  270. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
  271. Search for a game: <input name="search" type="text" id="search" />
  272.  
  273. <input name="searchbutton" type="submit" value="Search" />
  274. </form>
  275.  
  276. <h2>Rent a Game</h2>
  277.  
  278. </div>
  279. <div id='form'>
  280.  
  281. <!-- Form for user reservation entry -->
  282. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
  283.  
  284. <!-- Textbox for the game id -->
  285. <p>Game ID:
  286. <label for="gameid"></label>
  287. <input name="gameid" type="text" id="gameid" />
  288. <span class="error">* <a href='index.php'>Link to list of games</a><?php echo $gameidErr;?></span>
  289. </p>
  290.  
  291. <!-- Textbox for the persons name -->
  292. <p>Your name:
  293. <input name="name" type="text" id="name"/>
  294. <span class="error">* <?php echo $nameErr;?></span>
  295. </p>
  296.  
  297. <!-- Date form for the start date -->
  298. <p>Start date of reservation:
  299. <input type="date" name="startdate" id="date" value="dd/mm/yyyy"/>
  300. <span class="error">* <?php echo $startdateErr;?></span>
  301. </p>
  302.  
  303. <!-- Number of days the reservation is -->
  304. <p>No. Days the resveration is:
  305. <input name="days" type="number" id="days" min="1" max="5"/>
  306. <span class="error">Max of 5* <?php echo $daysErr;?></span>
  307. </p>
  308. <input name="submit" type="submit" value="Submit" />
  309. </form>
  310. </div>
  311.  
  312. <?php
  313.  
  314. if ($_SERVER["REQUEST_METHOD"] == "POST") { //If statement to stop the variables displaying to the user, before they have been entered into the form.
  315.  
  316.  
  317. //Connect to mysql database.
  318. $servername = "fdb6.awardspace.net";
  319. $username = "1963949_general";
  320. $password = "mugoftea12";
  321. $dbname = "1963949_general";
  322.  
  323. //Create connection
  324. $conn = new mysqli($servername, $username, $password, $dbname);
  325.  
  326. //Check connection
  327. if ($conn->connect_error) {//if error stop loading html and display error
  328. die("connection failed: ". $conn->connect_error);
  329. }
  330.  
  331.  
  332. //Calculates the end date.
  333. $enddate = date('Y-m-d', strtotime($startdate. " + $days days"));
  334.  
  335.  
  336.  
  337. //SQL to see if game wanted has already been reserved in the requested dates.
  338. $sql = "SELECT game_id
  339. FROM game_lending
  340. WHERE game_id = '".$gameid."'
  341. AND start_date between '".$startdate."' and '".$enddate."'
  342. OR end_date between '".$startdate."' and '".$enddate."'";
  343.  
  344.  
  345. $result = $conn->query($sql);
  346. $numberOfResults = $result->num_rows;
  347.  
  348.  
  349. if ($result->num_rows>0){//checks to see if variable made by the SQL query has any results
  350.  
  351. //Message if reservations full
  352. echo "<b><br>Reservation already taken, please pick another date.</br></b>";
  353.  
  354. } else {//If none found, program will carry on
  355.  
  356.  
  357. //SQL to insert users info to database
  358. $insert = "INSERT INTO game_lending(name, game_id, start_date, end_date, days)
  359. VALUES ('".$name."', '".$gameid."', '".$startdate."', '".$enddate."', '".$days."')";
  360.  
  361.  
  362. //Checks to see if data was successfully entered
  363. if ($conn->query($insert) === TRUE) {
  364. echo "<p>Your reservation has been made.<br>";
  365.  
  366. echo "<b><br> Reservation details: </b>";
  367. echo "<br>Game Name - " . $game_name . "<br>";
  368. echo "GameID - " .$gameid . "<br>";
  369. echo "Name - " .$name . "<br>";
  370. echo "Start Date - " .$startdate . "<br>";
  371. echo "Days - " .$days . "<br>";
  372. echo "Price - " .$game_price . "<br>";
  373. echo "Total - " .$total . "<br>";
  374. echo "End Date - " .$enddate . "<br>";
  375.  
  376. } else {
  377. echo "Error: ". $sql. "<br>". $conn->error;
  378. }
  379. }
  380.  
  381. $conn->close();
  382.  
  383. }
  384.  
  385. ?>
  386. </div>
  387. </body>
  388. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement