Guest User

DONT COPY IT EXACTLT. Change var names etc

a guest
Mar 8th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.24 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. //HERRO WORLD
  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.  
  16.                 <li><a href='index.php'>Home</a></li>
  17.                 <li class='current_page_item'><a href='#'>Reservations</a></li>
  18.             </ul>
  19.         </div>
  20.     </div>
  21.    
  22.     <div>
  23.         <h1>Reservations</h1>
  24. <?php
  25. $file = fopen("games.csv","r");
  26.         if(fopen("games.csv","r")) {
  27.             while(! feof($file)){
  28.                 $gameinfo[] = fgetcsv($file);
  29.             }
  30.         }
  31.         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>"; }
  32.         fclose($file); //this line of code closes the csv file to avoid corruption
  33.        
  34.         $numOfRows = count($gameinfo) - 1; // We have to subtract one because otherwise there will be an extra row in the table
  35.         $numOfCols = max(array_map('count', $gameinfo)); // this code detects how many columns there are in the csv file
  36.         // therefore this code makes the web application more dynamic
  37.  
  38.         $numOfRows = count($gameinfo) - 1; // We have to subtract one because otherwise there will   be an extra row in the table
  39.         $numOfCols = max(array_map('count', $gameinfo)); // this code detects how many columns
  40.  
  41. //GABEN = AWESOME
  42.     function retrieveGameName($gameinfo, $gameID) {
  43.         $numOfRows = count($gameinfo) - 1; // We have to subtract one because otherwise there will   be an extra row in the table
  44.         $numOfCols = max(array_map('count', $gameinfo)); // this code detects how many columns
  45.  
  46.         $row = 0;
  47.         while ($gameinfo[$row][0] != $gameID && $row < $numOfRows) {
  48.             $row++;        
  49.         }
  50.         $gameName = $gameinfo[$row][2];
  51.         return $gameName;
  52.     }
  53.    
  54.     function retrieveRentalPrice($gameinfo, $gameID) {
  55.         $numOfRows = count($gameinfo) - 1; // We have to subtract one because otherwise there will   be an extra row in the table
  56.         $numOfCols = max(array_map('count', $gameinfo)); // this code detects how many columns
  57.  
  58.         $row = 0;
  59.         while ($gameinfo[$row][0] != $gameID && $row < $numOfRows) {
  60.             $row++;        
  61.         }
  62.         $price = $gameinfo[$row][4];
  63.         return $price;
  64.     }
  65.  
  66.     function doesGameExist($gameinfo, $usergameID){
  67.             $counter = 0;
  68.             $gamefound = false;
  69.            
  70.             while ($counter < count($gameinfo) && $gamefound == false)
  71.             {
  72.             if($gameinfo[$counter][0] == $usergameID)
  73.                 {
  74.                     $gamefound = true;
  75.                 }
  76.                 $counter++;    
  77.             }
  78.             //echo $gamefound;
  79.             return $gamefound;
  80.         }
  81.    
  82. // ANDY WAS HERE
  83.     $name = $gameID = $startDate = $NoOfDays = "";
  84.     $nameErr = $gameIDErr = $startDateErr = $NoOfDaysErr = "";
  85.     if ($_SERVER["REQUEST_METHOD"] == "POST") {    
  86.         if (empty($_POST["name"])) {
  87.             $nameErr = "Name is required.";
  88.         }   else {
  89.             $name = $_POST["name"];
  90.            
  91.             //GABEN - PLS DELETE
  92.             if (strtolower($name) == strtolower("gaben"))
  93.             {
  94.                 $rentalPrice = 0;
  95.                 echo "IN GABEN WE TRUST. 100% OFF ALL RENTALS.";   
  96.             }  
  97.         }
  98. // ANDY WAS HERE
  99.         if (empty($_POST["gameID"])) {
  100.             $gameIDErr = "The game ID is required";
  101.         }   else {
  102.             $gameID = $_POST["gameID"];
  103.             if (!doesGameExist($gameinfo, $gameID)){
  104.                 $gameIDErr = "This is an invalid Game ID.";                
  105.             }
  106.             else { $gamename = retrieveGameName($gameinfo, $gameID); }
  107.         }
  108.         if (empty($_POST["startDate"])) {
  109.             $startDateErr = "The start date is required";
  110.         }   else {
  111.             $now = new DateTime();
  112.             $startDate = $_POST["startDate"];
  113.             if ($now < $startDate)
  114.             {
  115.                 $startDateErr = "Invalid Date: You cannot go back in time.";
  116.             }
  117.            
  118.            
  119.         }
  120.         $NoOfDays = $_POST["NoOfDays"];
  121.         $noOfDaysMin1 = $NoOfDays - 1;
  122.         $endDate = date('Y-m-d', strtotime($startDate. " + $noOfDaysMin1 days"));
  123.        
  124.         //Get Price
  125.         $rentalPrice = retrieveRentalPrice($gameinfo, $gameID) * $NoOfDays;
  126.         //GABEN - PLS DELETE!!
  127.         if (strtolower($name)=="gaben")
  128.         {
  129.             $rentalPrice = 0;
  130.         }
  131.         else{ $rentalPrice = retrieveRentalPrice($gameinfo, $gameID) * $NoOfDays; }
  132.  
  133.         //NORMAL.
  134.         //$rentalPrice = retrieveRentalPrice($gameinfo, $gameID) * $NoOfDays;      
  135.        
  136.     }  
  137. // ANDY WAS HERE
  138.    
  139.     $isReserved = false;
  140.     if ($_SERVER["REQUEST_METHOD"] == "POST") {
  141.         if ($error != true ) {
  142.             //connect to mysql database
  143.             $servername = "localhost";
  144.             $username = "acwtechn_andy";
  145.             $password = "dLGNyb=Jm#Ag";
  146.             $dbname = "acwtechn_andyschool";
  147.             $conn = new mysqli($servername, $username, $password);
  148.             $conn ->select_db($dbname);
  149.             // this checks if i have connected successfully
  150.             if ($conn->connect_error) {
  151.                 die("Connection failed: " . $conn->connect_error);
  152.             }
  153.            $yesBOIS= "SELECT GameID
  154.                         FROM Reservations
  155.                         WHERE (StartDate BETWEEN '".$startDate."' AND '".$endDate."' OR EndDate BETWEEN '".$startDate."' AND '".$endDate."')
  156.                         AND GameID = '".$gameID."'";
  157.             $tommoresult = mysqli_query($conn, $yesBOIS);
  158.             if (mysqli_num_rows($tommoresult) > 0) {
  159.                 echo "<br><b><span class='errormsg'>Please pick another date, another pupil has reserved this game within your chosen time period.</b></span>";
  160.                 $isReserved = false;
  161.             } else {
  162.                 $hello23443sql = "INSERT INTO Reservations (Name, GameID, StartDate, EndDate, NumberOfDays)
  163.                         VALUES ('".$name."', '".$gameID."', '".$startDate."', '".$endDate."', '".$NoOfDays."')";   
  164.                 if ($conn->query($sql)) {
  165.                     echo "<br><br> Reservation Saved.";
  166.                     $isReserved = true;
  167.                 } else {
  168.                     echo "Error: " . $hello23443sql. "<br>" . $conn->error;
  169.                     $isReserved = false;
  170.                 }
  171.                 $conn->close();
  172.         }
  173.     }
  174.     }
  175.    
  176.    
  177.    
  178. ?>
  179. <style>
  180.     .error {color: #FF0000;}
  181. </style>
  182. <p><span class="error">* required field.</span></p>
  183. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  184.    
  185.     //DONT COPY ANY OF THIS CODE EXACTLY
  186.     Name: <input type="text" name="name">
  187.     <span class="error">*<?php echo $nameErr;?></span>
  188.     <br><br>
  189.     Game ID: <input type="number" name="gameID">
  190.     <span class="error">*<?php echo $gameIDErr;?></span>
  191.     <br><br>
  192.     Start Date: <input type="date" name="startDate">
  193.     <span class="error">*<?php echo $startDateErr;?></span>
  194.     <br><br>
  195.     Number Of Days: <input type="number" name="NoOfDays" min="1" max="5" <id="NoOfDays">
  196.     <span class="error">*</span>
  197.     <br><br>
  198.     <input type="submit" name="submit" value="Submit">
  199. </form>
  200. <?php
  201.  
  202.  
  203.  
  204.     if ($_SERVER["REQUEST_METHOD"] == "POST") {
  205.         if ($nameErr == "" && $gameIDErr == "" && $startDateErr == "" && $NoOfDaysErr == ""){
  206.             echo $name; echo "<br>"; echo $gameID; echo "<br>"; echo $gamename; echo "<br>"; echo $startDate."<br>".$endDate."<br>".$NoOfDays;
  207.             if ($isReserved == true){   echo "<br><b>".$gamename." has been reserved and has been logged in our database successfully."; }
  208.             else { echo "<br><b>We were unable to reserve this game for you.";}
  209.         }
  210.     }
  211.  
  212.   // ANDY WAS HERE
  213. ?>
  214.        
  215.        
  216.        
  217.        
  218.     </div>
  219.    
  220. </div>
  221.  
  222. </body>
  223. </html>
Add Comment
Please, Sign In to add comment