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 Home Page</title>
- <link href="styles.css" rel="stylesheet" type="text/css">
- </head>
- <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 class='current_page_item'><a href='#'>Home</a></li>
- <li><a href='reservations.php'>Reservations</a></li>
- </ul>
- </div>
- </div>
- <?php
- //check file exists
- if (file_exists('games.csv')){
- //open the csv file as read
- $file = fopen('games.csv', 'r');
- //Read the file into an array
- while(! feof($file)){
- $game_list[] = fgetcsv($file);
- }
- fclose($file);
- } else {
- //Displays an error message to the user if the list of games can't be found
- die ("<br>Sorry the information cannot be displayed at this time.");
- }
- ?>
- <div id='tables'>
- <h1>Games</h1>
- <?php
- // Displays name of columns for the table
- echo "<table>
- <tr>
- <th>Game ID</th>
- <th>Genre</th>
- <th>Game Name</th>
- <th>Game Description</th>
- <th>Rental Cost Per Day</th>
- </tr>";
- // Set number of table rows
- $num_rows = count($game_list) - 1;
- // Set number of table columns
- $num_cols = 5;
- // Start loop to generate rows
- for($row = 0; $row < $num_rows; $row++) {
- // Generate row HTML
- echo "<tr>";
- //Start loop to generate columns (nested FOR loop!)
- for($col = 0 ; $col < $num_cols; $col++) {
- // Generate column HTML
- echo "<td>". $game_list[$row][$col] ."</td>";
- }
- // End of columns loop
- // Generate end of row HTML
- echo "</tr>";
- }
- echo "</table>";
- ?>
- </div>
- </div>
- </body>
- </html>
- ------------------------------------------Page 2----------------------------------------------
- <!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'>
- <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>
- <?php
- //check file exists
- if (file_exists('games.csv')){
- //open the csv file as read
- $file = fopen('games.csv', 'r');
- //Read the file into an array
- while(! feof($file)){
- $game_list[] = fgetcsv($file);
- }
- fclose($file);
- } else {
- //Displays an error message to the user if the list of games can't be found
- die ("<br>Sorry this information cannot be displayed at this time.");
- }
- ?>
- <?php
- $found = false;
- $num_rows = count($game_list);
- $rows = 0;
- $row = 0;
- $cols = 0;
- $game_price = 0;
- //store contents of form un variables
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- //If statement to check the user has entered a game ID.
- if (empty($_POST["gameid"])) {
- $nameErr = "The game ID is required";
- } else {
- $row = 0;
- $gameid = $_POST["gameid"];
- while ($found != true and $row < $num_rows) { // While loop to find out if the game ID that has been entered is valid.
- if ($gameid == $game_list[$row][$cols]){
- $found = true;
- } else{
- $row = 1 +$row;
- }
- }
- // If statement to find the game name using the game ID that has been entered by the user.
- if ($found == true) {
- //Puts name into variable
- $game_name = $game_list[$row][2];
- }else{
- echo "Please enter a valid game ID";
- }
- }
- }
- if (empty($_POST["name"])) {
- $nameErr = "Name is required";
- } else {
- $name = $_POST["name"];
- }
- if (empty($_POST["startdate"])) {
- $startdateErr = "Date is required";
- } else {
- $startdate = $_POST["startdate"];
- }
- if (empty($_POST["days"])) {
- $daysErr = "days is required";
- } else {
- $days = $_POST["days"];
- //If statement to check the game ID has been found
- if ($found == true) {
- //Calculates price using the game ID ans amount of days entered.
- $game_price = $game_list[$row][4];
- $total = $game_price * $days;
- }
- }
- $num_rows = count($game_list);
- if ($_SERVER['REQUEST_METHOD'] == "POST"){
- if (empty($_POST["search"])) {
- } else {
- $search = $_POST["search"];
- $num_row = count($game_list);
- $successful = false;
- $row = 0;
- //Displays column rows
- echo "<table>
- <tr>
- <th>Game ID</th>
- <th>Genre</th>
- <th>Game Name</th>
- <th>Game Description</th>
- <th>Rental Cost Per Day</th>
- </tr>";
- //For loop to find the keyword in the list of games array
- for ($row = 0; $row < $num_row; $row++){
- //Checks to see if the keyword searched is in a game description or name
- if ((strpos(strtolower($game_list[$row][2]), strtolower($search)) !== false) or (strpos(strtolower($game_list[$row][3]), strtolower($search)) !== false)){
- $successfull = true;
- // Set number of table columns
- $num_cols = 5;
- // Generate row HTML
- echo "<tr>";
- //Start loop to generate columns (nested FOR loop!)
- for($col = 0 ; $col < $num_cols; $col++) {
- // Generate column HTML
- echo "<td>". $game_list[$row][$col] ."</td>";
- }
- // End of columns loop
- // Generate end of row HTML
- echo "</tr>";
- $successful = true;
- }
- }
- if ($successful == false){
- echo "<tr>";
- echo "<br><b><center>No results were found.</center></b><br>";
- echo "</tr>";
- }
- echo "</table>";
- }
- }
- ?>
- <div>
- <h1>Reservations</h1>
- <h2>Search for a game</h2>
- <!-- Form for user search -->
- <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
- Search for a game: <input name="search" type="text" id="search" />
- <input name="searchbutton" type="submit" value="Search" />
- </form>
- <h2>Rent a Game</h2>
- </div>
- <div id='form'>
- <!-- Form for user reservation entry -->
- <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
- <!-- Textbox for the game id -->
- <p>Game ID:
- <label for="gameid"></label>
- <input name="gameid" type="text" id="gameid" />
- <span class="error">* <a href='index.php'>Link to list of games</a><?php echo $gameidErr;?></span>
- </p>
- <!-- Textbox for the persons name -->
- <p>Your name:
- <input name="name" type="text" id="name"/>
- <span class="error">* <?php echo $nameErr;?></span>
- </p>
- <!-- Date form for the start date -->
- <p>Start date of reservation:
- <input type="date" name="startdate" id="date" value="dd/mm/yyyy"/>
- <span class="error">* <?php echo $startdateErr;?></span>
- </p>
- <!-- Number of days the reservation is -->
- <p>No. Days the resveration is:
- <input name="days" type="number" id="days" min="1" max="5"/>
- <span class="error">Max of 5* <?php echo $daysErr;?></span>
- </p>
- <input name="submit" type="submit" value="Submit" />
- </form>
- </div>
- <?php
- if ($_SERVER["REQUEST_METHOD"] == "POST") { //If statement to stop the variables displaying to the user, before they have been entered into the form.
- //Connect to mysql database.
- $servername = "fdb6.awardspace.net";
- $username = "1963949_general";
- $password = "mugoftea12";
- $dbname = "1963949_general";
- //Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- //Check connection
- if ($conn->connect_error) {//if error stop loading html and display error
- die("connection failed: ". $conn->connect_error);
- }
- //Calculates the end date.
- $enddate = date('Y-m-d', strtotime($startdate. " + $days days"));
- //SQL to see if game wanted has already been reserved in the requested dates.
- $sql = "SELECT game_id
- FROM game_lending
- WHERE game_id = '".$gameid."'
- AND start_date between '".$startdate."' and '".$enddate."'
- OR end_date between '".$startdate."' and '".$enddate."'";
- $result = $conn->query($sql);
- $numberOfResults = $result->num_rows;
- if ($result->num_rows>0){//checks to see if variable made by the SQL query has any results
- //Message if reservations full
- echo "<b><br>Reservation already taken, please pick another date.</br></b>";
- } else {//If none found, program will carry on
- //SQL to insert users info to database
- $insert = "INSERT INTO game_lending(name, game_id, start_date, end_date, days)
- VALUES ('".$name."', '".$gameid."', '".$startdate."', '".$enddate."', '".$days."')";
- //Checks to see if data was successfully entered
- if ($conn->query($insert) === TRUE) {
- echo "<p>Your reservation has been made.<br>";
- echo "<b><br> Reservation details: </b>";
- echo "<br>Game Name - " . $game_name . "<br>";
- echo "GameID - " .$gameid . "<br>";
- echo "Name - " .$name . "<br>";
- echo "Start Date - " .$startdate . "<br>";
- echo "Days - " .$days . "<br>";
- echo "Price - " .$game_price . "<br>";
- echo "Total - " .$total . "<br>";
- echo "End Date - " .$enddate . "<br>";
- } else {
- echo "Error: ". $sql. "<br>". $conn->error;
- }
- }
- $conn->close();
- }
- ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement