Advertisement
Guest User

Untitled

a guest
Mar 18th, 2016
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 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. <div id='tables'>
  22. <h1>Games</h1>
  23. <table>
  24. <tr>
  25. <th>Game ID</th>
  26. <th>Genre</th>
  27. <th>Game Name</th>
  28. <th>Game Description</th>
  29. <th>Rental Cost per day</th>
  30. </tr>
  31.  
  32.  
  33. <?php
  34. $count = 0;
  35. $file = fopen('games.csv', 'r');
  36. while(($text[] = fgetcsv($file)) !== FALSE){
  37. $count = $count + 1;
  38. }
  39.  
  40. fclose($file);
  41.  
  42. for($row =0; $row<= 7; $row++) {
  43. echo"<tr>";
  44. for($col =0; $col <= 4; $col++) {
  45. if ($col == 4) {
  46. echo"<td> £".$text[$row][$col]."</td>";
  47. }else{
  48. echo"<td>". $text[$row][$col]."</td>";
  49. }
  50. }
  51. echo"</tr>";
  52. echo "<br>";
  53.  
  54.  
  55. }
  56. ?>
  57. </table>
  58.  
  59.  
  60.  
  61.  
  62.  
  63. </div>
  64.  
  65.  
  66. </div>
  67.  
  68. </body>
  69. </html>
  70.  
  71.  
  72.  
  73. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  74. <html xmlns="http://www.w3.org/1999/xhtml">
  75. <head>
  76. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  77. <title>Games Library Reservations Page</title>
  78. <link href="styles.css" rel="stylesheet" type="text/css">
  79. </head>
  80.  
  81. <body>
  82. <div id='main'>
  83. <div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
  84. <div id='menu-wrapper'>
  85. <div id='menu'>
  86. <ul>
  87.  
  88. <li><a href='index.php'>Home</a></li>
  89. <li class='current_page_item'><a href='#'>Reservations</a></li>
  90. </ul>
  91. </div>
  92. </div>
  93.  
  94. <div>
  95. <h1>Reservations</h1>
  96. <p>Please fill out the form below with all the requested details.</p>
  97. <?php
  98. $count = 0; //Count loop is asigned the number 0 in order to be initialised.
  99. function find_user($games, $query1) {
  100. $file = fopen('games.csv', 'r'); //The file games.csv is read and opened.
  101. $result = false;
  102. while ($row = fgetcsv($f)) {
  103. if ($row[1] == $query1) {
  104. $result = $row[0];
  105. break;
  106. while(($games[] = fgetcsv($file)) !== FALSE){ //For all of the games in the file the count loop will have 1 added. (but it starts from 0) it is stored in the array $games
  107. $count = $count + 1; //The count loop is made equal to 1 + the previous value of count.
  108. }
  109.  
  110.  
  111.  
  112. }
  113. }
  114. echo $result;
  115. }
  116.  
  117.  
  118. fclose($file); //The file is closed.
  119.  
  120.  
  121.  
  122. $name = $gameID = $start = $reserve = ""; //All of these variables are made equal to an empty string.
  123. $nameErr = $gameIDErr = $startrErr = $reserveErr =""; //As are all of these.
  124.  
  125. if ($_SERVER["REQUEST_METHOD"] == "POST") { //If something is missing from the field then an error message will be displayed.
  126. if(empty($_POST["name"])) {
  127. $nameErr = "Name is required"; //The variables xErr are made equal to error messages.
  128. } else {
  129. $name = $_POST["name"]; //If there is nothing missing then the variable name is made equal to what the user puts in the form.
  130. }
  131. if(empty($_POST["gameID"])) {
  132. $gameIDErr = "gameID is required";
  133. } else {
  134. $gameID = $_POST["gameID"];
  135. }
  136. if(empty($_POST["start"])) {
  137. $startErr = "start is required";
  138. } else {
  139. $start = $_POST["start"];
  140. }
  141. if(empty($_POST["reserve"])) {
  142. $reserveErr = "reserve is required";
  143. }else{
  144. $reserve = $_POST["reserve"];
  145. }
  146.  
  147.  
  148. $match = "no" ; //Match needs to be asigned a value before being used. It is "no" by default.
  149. $number_of_records = $count - 1 ; //The number of records is made equal to my count loop - 1
  150. for ($i = 0;$i<$number_of_records;$i++) { // i is set as 0 and will have 1 added to it if it's value is smaller than the number of records. $i++ is the syntax to have 1 added to the variable i.
  151. if ($gameID == $games[$i][0]) { //If the gameID is equal to the the maximum record or bellow execute the code below.
  152. $match = "yes"; //The variable match is set to yes if the above condition is met.
  153. $gamename = $games[$i][2]; //the name of the game which is chosen is the found by going down to the right row and making $game name equal to the name in the column, hence the [2], which indicates the second column (game name) being in use.
  154. $price = $games[$i][4]; //The price is the row of the game chosen and then the 5th column, which is where the price is in the table (the column and rows first start at 0.
  155. $validgameID = $games[$i][0]; //The ValidgameID is the number which is in the first column of the table.(Rows and columns in PHP start at 0).
  156.  
  157.  
  158.  
  159. } //End of if loop.
  160. } //End of for loop.
  161. if($match == "no"){ //If match is 0 it means that an invalid game ID was inputed.
  162. $gameIDErr = "Game ID does not exist"; //The appropriate message is displayed to the user.
  163. }
  164.  
  165. $enddate = date('Y-m-d',strtotime($start." +{$reserve}days")); //The end date is calculated here using the function "date" and "strtotime".
  166. echo $enddate; //The end date is printed so that the user can see what they have selected.
  167.  
  168.  
  169. $servername = "localhost"; //My server name is called "localhost"
  170. $username = "vaudibert1"; //The username to my server is "vaudibert1"
  171. $password = "ebscs"; //The password to my database is "ebscs"
  172. $dbname = "vaudibert"; //The name of my database is "vaudibert"
  173. if ($match == "yes") { //Proving the game ID exists then the code bellow will be executed.
  174.  
  175. $conn = new MySQLi($servername, $username, $password, $dbname); //The variable "conn" connects me to my database by inputed my login details.
  176. if ($conn->connect_error) { //If there is an error in one of my details then there will be a connection error.
  177. die("connection failed: " . $conn->connect_error);//If there is an error then it will say connection failed and the system will stop trying to connect to the server.
  178. }
  179.  
  180. $CheckSQL = //This is the variable where I will see if a reservation is to be placed or not.
  181. //The code will select the game ID from my database called "reservations" where the start date is between the start date and end date or where the end date is between teh start date and end date. If the following is true that means that the reservations are overlaping and that somebody has already placed a reservation.
  182. "SELECT gameID
  183. FROM reservation
  184. WHERE (start BETWEEN '".$start."' AND '".$enddate."' OR enddate BETWEEN '".$start."' AND '".$enddate."')
  185. AND gameID = '".$gameID."'";
  186.  
  187. $result = $conn->query($CheckSQL); //The results of the above are stored in the variable "results"
  188. echo"number of rows found ".$result->num_rows."<br>"; //The number of matches will be displayed to the user
  189.  
  190. if ($result->num_rows>0){ //If the result is greater than one that means that 1 match or more was found.
  191. echo "already found, try again"; //The user is told to try another date.
  192.  
  193. } else { //If the above is not true then the code bellow will be executed.
  194.  
  195. //The inputs of the user (their name, gameID, start date or reservation, the number of days the game will be reserved for and the end date will be stored into my database.
  196. $sql = "INSERT INTO reservation (name, gameID, start, reserve, enddate)
  197. VALUES('".$name."','".$gameID."','".$start."','".$reserve."','".$enddate."')";
  198. if($conn->query($sql) === TRUE ) {
  199. echo "New record successfully created"; //the user is told their details were saved successfully.
  200. } else {
  201. echo "Error: ". $sql . "<br>" .$conn->error; //An error message is displayed if there is an error.
  202. }
  203.  
  204. $conn->close(); //once the above has been done the connection is terminated.
  205. }
  206. }
  207. } else {
  208. echo "error, gameID not real"; //If the above is not true then the gameID must be false.
  209. }
  210. ?>
  211. <style>
  212. .error {color: #FF0000;}
  213. </style>
  214. <?php
  215. //The code below is the form and form validation.
  216. //Name will be displayed above the text box which corresponds to the variable name, so that the user is aware of what they are meant to enter in each box.
  217. //If there is an error due to an invalid input then the variable "nameErr" will be called down, which displays an error message.
  218. //The same is applied to all of the variables which the user has to input.
  219. ?>
  220. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  221.  
  222. <p>Please enter your first and second name:</p>
  223. <p>
  224. <input type="text" name="name">
  225. <span class="error">* <?php echo $nameErr;?></span>
  226. <br><br>
  227. Please enter the GameID:</p>
  228. <p>
  229. <input type="text" name="gameID">
  230. <span class="error">* <?php echo $gameIDErr;?></span>
  231. <br><br>
  232. Please enter the start date of yourreservation: </p>
  233. <p>
  234. <input type="date" name="start">
  235. <span class="error">* <?php echo $startErr;?></span>
  236. <br>
  237. <p>Please enter the number of days the game will be reserved:<br>
  238. <p>
  239. <input type="number" name="reserve" min="1" max="5">
  240. <span class="error">* <?php echo $reserveErr;?></span>
  241. <br><br>
  242. <p>
  243. <p>Search for a game:<p>
  244. <input type="text" name="query1">
  245. <span class="error">* <?php echo $query1;?></span>
  246. <br><br>
  247. <input type="submit" name="submit" value="Submit">
  248. </p>
  249. <br><br>
  250. </p>
  251. </form>
  252.  
  253.  
  254.  
  255. <?php
  256.  
  257.  
  258.  
  259. //The below displays all of the users inputs so they can check over them.
  260. echo "<h2>Your Input:</h2>";
  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 $query1;
  270. echo"<br>";
  271. echo "The game name is: ". $gamename; //It displays the name of the chosen game (Which corresponds to the gameID)
  272. $totalprice = $price * $reserve; //The total price is the price of the game * the days it is reserved for
  273. echo "<br>";
  274. echo "The price for ".$reserve." days is £".$totalprice."."; //The price for x days is y price.
  275.  
  276.  
  277. ?>
  278.  
  279.  
  280. </div>
  281.  
  282. </body>
  283. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement