Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Games Library Reservations Page</title>
- <link href="styles.css" rel="stylesheet" type="text/css">
- </head>
- //HERRO WORLD
- <body>
- <div id='main'>
- <div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
- <div id='menu-wrapper'>
- <div id='menu'>
- <ul>
- <li><a href='index.php'>Home</a></li>
- <li class='current_page_item'><a href='#'>Reservations</a></li>
- </ul>
- </div>
- </div>
- <div>
- <h1>Reservations</h1>
- <?php
- $file = fopen("games.csv","r");
- if(fopen("games.csv","r")) {
- while(! feof($file)){
- $gameinfo[] = fgetcsv($file);
- }
- }
- else{ echo "<h2 class='errormsg'>The game info could not be loaded.</h2><p>The 'games.csv' file could not be found on the server</p><br>"; }
- fclose($file); //this line of code closes the csv file to avoid corruption
- $numOfRows = count($gameinfo) - 1; // We have to subtract one because otherwise there will be an extra row in the table
- $numOfCols = max(array_map('count', $gameinfo)); // this code detects how many columns there are in the csv file
- // therefore this code makes the web application more dynamic
- $numOfRows = count($gameinfo) - 1; // We have to subtract one because otherwise there will be an extra row in the table
- $numOfCols = max(array_map('count', $gameinfo)); // this code detects how many columns
- //GABEN = AWESOME
- function retrieveGameName($gameinfo, $gameID) {
- $numOfRows = count($gameinfo) - 1; // We have to subtract one because otherwise there will be an extra row in the table
- $numOfCols = max(array_map('count', $gameinfo)); // this code detects how many columns
- $row = 0;
- while ($gameinfo[$row][0] != $gameID && $row < $numOfRows) {
- $row++;
- }
- $gameName = $gameinfo[$row][2];
- return $gameName;
- }
- function retrieveRentalPrice($gameinfo, $gameID) {
- $numOfRows = count($gameinfo) - 1; // We have to subtract one because otherwise there will be an extra row in the table
- $numOfCols = max(array_map('count', $gameinfo)); // this code detects how many columns
- $row = 0;
- while ($gameinfo[$row][0] != $gameID && $row < $numOfRows) {
- $row++;
- }
- $price = $gameinfo[$row][4];
- return $price;
- }
- function doesGameExist($gameinfo, $usergameID){
- $counter = 0;
- $gamefound = false;
- while ($counter < count($gameinfo) && $gamefound == false)
- {
- if($gameinfo[$counter][0] == $usergameID)
- {
- $gamefound = true;
- }
- $counter++;
- }
- //echo $gamefound;
- return $gamefound;
- }
- // ANDY WAS HERE
- $name = $gameID = $startDate = $NoOfDays = "";
- $nameErr = $gameIDErr = $startDateErr = $NoOfDaysErr = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["name"])) {
- $nameErr = "Name is required.";
- } else {
- $name = $_POST["name"];
- //GABEN - PLS DELETE
- if (strtolower($name) == strtolower("gaben"))
- {
- $rentalPrice = 0;
- echo "IN GABEN WE TRUST. 100% OFF ALL RENTALS.";
- }
- }
- // ANDY WAS HERE
- if (empty($_POST["gameID"])) {
- $gameIDErr = "The game ID is required";
- } else {
- $gameID = $_POST["gameID"];
- if (!doesGameExist($gameinfo, $gameID)){
- $gameIDErr = "This is an invalid Game ID.";
- }
- else { $gamename = retrieveGameName($gameinfo, $gameID); }
- }
- if (empty($_POST["startDate"])) {
- $startDateErr = "The start date is required";
- } else {
- $now = new DateTime();
- $startDate = $_POST["startDate"];
- if ($now < $startDate)
- {
- $startDateErr = "Invalid Date: You cannot go back in time.";
- }
- }
- $NoOfDays = $_POST["NoOfDays"];
- $noOfDaysMin1 = $NoOfDays - 1;
- $endDate = date('Y-m-d', strtotime($startDate. " + $noOfDaysMin1 days"));
- //Get Price
- $rentalPrice = retrieveRentalPrice($gameinfo, $gameID) * $NoOfDays;
- //GABEN - PLS DELETE!!
- if (strtolower($name)=="gaben")
- {
- $rentalPrice = 0;
- }
- else{ $rentalPrice = retrieveRentalPrice($gameinfo, $gameID) * $NoOfDays; }
- //NORMAL.
- //$rentalPrice = retrieveRentalPrice($gameinfo, $gameID) * $NoOfDays;
- }
- // ANDY WAS HERE
- $isReserved = false;
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if ($error != true ) {
- //connect to mysql database
- $servername = "localhost";
- $username = "acwtechn_andy";
- $password = "dLGNyb=Jm#Ag";
- $dbname = "acwtechn_andyschool";
- $conn = new mysqli($servername, $username, $password);
- $conn ->select_db($dbname);
- // this checks if i have connected successfully
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $yesBOIS= "SELECT GameID
- FROM Reservations
- WHERE (StartDate BETWEEN '".$startDate."' AND '".$endDate."' OR EndDate BETWEEN '".$startDate."' AND '".$endDate."')
- AND GameID = '".$gameID."'";
- $tommoresult = mysqli_query($conn, $yesBOIS);
- if (mysqli_num_rows($tommoresult) > 0) {
- echo "<br><b><span class='errormsg'>Please pick another date, another pupil has reserved this game within your chosen time period.</b></span>";
- $isReserved = false;
- } else {
- $hello23443sql = "INSERT INTO Reservations (Name, GameID, StartDate, EndDate, NumberOfDays)
- VALUES ('".$name."', '".$gameID."', '".$startDate."', '".$endDate."', '".$NoOfDays."')";
- if ($conn->query($sql)) {
- echo "<br><br> Reservation Saved.";
- $isReserved = true;
- } else {
- echo "Error: " . $hello23443sql. "<br>" . $conn->error;
- $isReserved = false;
- }
- $conn->close();
- }
- }
- }
- ?>
- <style>
- .error {color: #FF0000;}
- </style>
- <p><span class="error">* required field.</span></p>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- //DONT COPY ANY OF THIS CODE EXACTLY
- Name: <input type="text" name="name">
- <span class="error">*<?php echo $nameErr;?></span>
- <br><br>
- Game ID: <input type="number" name="gameID">
- <span class="error">*<?php echo $gameIDErr;?></span>
- <br><br>
- Start Date: <input type="date" name="startDate">
- <span class="error">*<?php echo $startDateErr;?></span>
- <br><br>
- Number Of Days: <input type="number" name="NoOfDays" min="1" max="5" <id="NoOfDays">
- <span class="error">*</span>
- <br><br>
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if ($nameErr == "" && $gameIDErr == "" && $startDateErr == "" && $NoOfDaysErr == ""){
- echo $name; echo "<br>"; echo $gameID; echo "<br>"; echo $gamename; echo "<br>"; echo $startDate."<br>".$endDate."<br>".$NoOfDays;
- if ($isReserved == true){ echo "<br><b>".$gamename." has been reserved and has been logged in our database successfully."; }
- else { echo "<br><b>We were unable to reserve this game for you.";}
- }
- }
- // ANDY WAS HERE
- ?>
- </div>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment