Advertisement
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>
- <body>
- <div id='main'> <!--finding and loading in the logo of the website and positiong it -->
- <div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='GLLogo.png' /></div>
- <div id='menu-wrapper'>
- <div id='menu'>
- <ul>
- <!--This part of the code links the two websites together so that the button takes you between them-->
- <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
- //A simple loop that counts the number of rows in teh games.csv file
- $count = 0;
- $file = fopen('games.csv', 'r');
- while(($games[] = fgetcsv($file)) !== FALSE)
- {
- $count = $count + 1;
- }
- fclose($file);
- //declerations of several variables
- $name = $gameID = $start = $reserve = "";
- $nameErr = $gameIDErr = $startrErr = $reserveErr ="";
- //checking whether all the forms boxes have been filled in by the user
- if ($_SERVER["REQUEST_METHOD"] == "POST")
- {
- if (isset($_POST['submit']))
- {
- if(empty($_POST["name"]))
- {
- $nameErr = "Name is required";
- }
- else
- {
- $name = $_POST["name"];
- }
- if(empty($_POST["gameID"]))
- {
- $gameIDErr = "Game ID is required";
- }
- else
- {
- $gameID = $_POST["gameID"];
- }
- if(empty($_POST["start"]))
- {
- $startErr = "Start date is required";
- }
- else
- {
- $start = $_POST["start"];
- }
- if(empty($_POST["reserve"]))
- {
- $reserveErr = "Reserve is required";
- }
- else
- {
- $reserve = $_POST["reserve"];
- }
- //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
- $match = "no" ;
- $number_of_records = $count ;
- for ($i = 0;$i<$number_of_records;$i++)
- {
- if ($gameID == $games[$i][0])
- {
- $match = "yes";
- $gamename = $games[$i][2];
- $price = $games[$i][4];
- $validgameID = $games[$i][0];
- }
- }
- //echo statement that tells te user of a false entry inthe gameID
- if($match == "no")
- {
- $gameIDErr = "Game ID does not exist";
- }
- //declaring all of the database information including the address, name and password
- $servername = "fdb5.awardspace.net";
- $username = "1972286_db1";
- $password = "ebscomputing1";
- $dbname = "1972286_db1";
- if ($match == "yes")
- {
- //checking whether the database has been connected and continueing or displaying a message depending on whether it has or not
- $conn = new MySQLi($servername, $username, $password, $dbname);
- if ($conn->connect_error)
- {
- //connection failed warning
- die("connection failed: " . $conn->connect_error);
- }
- else
- {
- //calcuates the end date of the users reservation so that the program can check whether that game is already reserved
- $Enddate = date('Y-m-d', strtotime($start." + $reserve days"));
- //first line checks for overlapping dates
- //second line checks where new booking starts before and ends after existing
- $sql = "SELECT gameID
- FROM reservation
- WHERE ((start BETWEEN'".$start."' and '".$Enddate."' OR Enddate between'".$start."' and '".$Enddate."') OR
- (start >'".$start."' and Enddate <'".$Enddate."'))
- AND gameID = '".$gameID."'";
- $results = $conn->query($sql);
- //echo "no of records found ".$results->num_rows."<br>";
- if($results->num_rows>0)
- {
- //prints a red message that alerts the user that reservation cannot be made on those dates
- echo '<p style="color: red; text-align">*already reserved on those dates, try again*</p>';
- echo "<br>";
- }
- else
- {
- //inserts the details of the reservations that have already been checked and adds them into the database
- $sql = "INSERT INTO reservation (name, gameID, start, reserve, Enddate)
- VALUES('".$name."','".$gameID."','".$start."','".$reserve."','".$Enddate."')";
- if($conn->query($sql) === TRUE)
- { //printing a message to the user that states that the record has been successfuly saved
- echo '<p style="color: green; text-align">*New record successfully created*</p>';
- }
- else
- {
- //printing a message that tells the user the record has not been saved correctly
- echo "Error: ", "<br>" .$conn->error;
- }
- }
- $conn->close();
- }
- }
- else
- {
- //tells the user that they have entered a wrong entry eg. the gameID was incorrect
- echo '<p style="color: red; text-align">*sorry, record not saved due to a wrong entry*</p>';
- }
- }
- }
- ?>
- <!--some simple statements that change the colour of text to make it more user friendly-->
- <style>
- .error {color: #FF0000;}
- .record_Err {color: #FF0000;}
- </style>
- <!--creating the front end of the website as a form that the user will fill in to record a reservation-->
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- <p>Name:</p>
- <p> <!--defining the name and type of each item the user will fill in-->
- <input type="text" name="name">
- <span class="error">* <?php echo $nameErr;?></span>
- <br><br>
- gameID:</p>
- <p>
- <input type="text" name="gameID">
- <span class="error">* <?php echo $gameIDErr;?></span>
- <br><br>
- Start date of reservation: </p>
- <p>
- <input type="date" name="start">
- <span class="error">* <?php echo $startErr;?></span>
- <br>
- <p>Number of days for reservation:<br>
- <p>
- <input type="number" name="reserve" min="1" max="5">
- <span class="error">* <?php echo $reserveErr;?></span>
- <br><br>
- <input type="submit" name="submit" value="Submit"><br><br>
- <br>keyword:
- <input type="text" name="keyword"><br>
- <input type="submit" name="search" value="search"><div id='searchImage'><img title='Home' src='Images/magni.png' width='40' height='30' alt='Magni.png' /></div>
- </p>
- </p>
- </form>
- <?php
- if (isset($_POST['search']))
- {
- //checking that the user actually inputted a keyword
- $keyword =strtolower($_POST["keyword"]);
- echo "keyword entered was:" .$keyword;
- if ($keyword == "")
- {
- echo "<br>", "no search";
- }
- else
- {
- echo "<br>", "these are the searches found:";
- ?>
- <table>
- <tr>
- <th>Game ID</th>
- <th>Genre</th>
- <th>Game Name</th>
- <th>Game Description</th>
- <th>Rental Cost per day</th>
- </tr>
- <?php
- //searches the games variable that contains all the games for games that contain the keyword in the name,description and genre
- for($row =0; $row<= $count; $row++)
- {
- //checking if the keyword comes up in any of the 3 columns
- if ((strpos($games[$row][1],$keyword)!== FALSE) or (strpos($games[$row][2],$keyword)!== FALSE) or (strpos($games[$row][3],$keyword)!== FALSE))
- {
- echo"<tr>";
- for($col =0; $col <= 4; $col++)
- {
- if ($col == 4)
- {//displaying the pound sign for appropriate columns
- echo"<td> £".$games[$row][$col]."</td>";
- }
- else
- {
- echo"<td>". $games[$row][$col]."</td>";
- }
- }
- }
- }
- ?>
- </table>
- <?php
- }
- }
- ?>
- <?php
- //presenting the user with all teh information that they have entered as well as showing them the total price that their reservation will cost
- echo $name;
- echo"<br>";
- echo $gameID;
- echo"<br>";
- echo $start;
- echo"<br>";
- echo $reserve;
- echo"<br>";
- echo $match;
- echo"<br>";
- echo $gamename;
- $totalprice = $price * $reserve;
- echo "<br>";
- echo "The price for ".$reserve." days is £".$totalprice.".";
- echo "<br>";
- echo $Enddate;
- ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement