Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Classes.php INITIAL TABLE OF CLASS CHOICES:
- -------------------------------------------------------------------------------------------
- // Retrieve the class data from MySQL
- $query = "SELECT DATE_FORMAT(date, '%a, %d %b %Y') AS new_date, DATE_FORMAT(time, '%H:%i') AS new_time, venue, free_spaces, class_id FROM classes WHERE time_stamp > CURRENT_TIMESTAMP ORDER BY time_stamp ASC LIMIT 8";
- $data = mysqli_query($dbc, $query);
- // Loop through the array of class data, formatting it as HTML
- echo '<table class="table table-striped table-hover">';
- echo '<thead><tr><th>Date</th><th>Time</th><th>Venue</th><th>Spaces available</th><th>Select to book</th></tr></thead>';
- while ($row = mysqli_fetch_array($data)) {
- echo '<tr><td>' . $row['new_date'] . '</td>';
- echo '<td>' . $row['new_time'] . '</td>';
- echo '<td>' . $row['venue'] . '</td>';
- echo '<td>' . $row['free_spaces'] . '</td>';
- echo '<form action="makebooking.php" method="get">';
- echo '<td><input type="checkbox" name="class_id[]" value=' . $row['class_id'] . '</td></tr>';
- }
- echo'</table>';
- // Make booking button
- echo '<input type="submit" class="btn btn-large btn-primary pull-right" value="Book classes">';
- echo '</form>';
- -------------------------------------------------------------------------------------------
- //Makebooking.php $_GET AND $_POST TO REFLECT CHOICES AND ALLOW USER TO CONFIRM BEFORE UPDATING DB WITH BOOKING
- -------------------------------------------------------------------------------------------
- <?php
- //Start the session
- require_once('startsession.php');
- require_once('connectvars.php');
- require_once('header.php');
- ?>
- <body>
- <?php
- //Insert the navigation bar
- require_once('navbar.php');
- //Check if the GET is set from classes.php
- if (isset($_GET['class_id'])) {
- // Grab the score data from the GET
- foreach($_GET['class_id'] as $class_id) {
- $_GET['class_id'] = $class_id;
- }
- }
- //---------------------------------------------------------------------------------
- //Check if the shown bookings have been confirmed
- else if (isset($_POST['class_id'])){
- foreach($_POST['class_id'] as $class_id) {
- $class_id = $_POST['class_id'];
- $user_id = $_SESSION['user_id'];
- }
- } else {
- echo '<div class="alert">
- <button type="button" class="close" data-dismiss="alert">×</button>
- Please tick the boxes next to the classes you want to book.</br>
- </div>';
- }
- //---------------------------------------------------------------------------------
- //If the confirm button has been hit:
- if (isset($_POST['submit'])) {
- //-------------------------------------------------------------------------------
- //Get user's ID from the users DB and set as $username
- $who = "SELECT first_name, last_name, user_id FROM users WHERE email = '" . $_SESSION['email'] . "'";
- $name = mysqli_query($dbc, $who);
- $row = mysqli_fetch_array($name);
- $username = $row['first_name'] . ' ' . $row['last_name'];
- // Connect to the database
- $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
- //UPDATE the bookings table;
- $query = "INSERT INTO bookings (user_id, booking_name, class_id, time_stamp) VALUES ('$user_id', '$username', '$class_id', NOW())";
- mysqli_query($dbc, $query);
- // REDIRECT
- $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/classes.php';
- header('Location: ' . $home_url);
- mysqli_close($dbc);
- }
- else if (isset($class_id)) {
- //---------------------------------------------------------------------------------
- //table header
- echo '<table class="table table-bordered table-hover">';
- echo '<thead><tr><th>Date</th><th>Time</th><th>Venue</th><th>Who\'s going?</th><th>Add someone</th></tr></thead>';
- //create the form
- echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
- //Get the class IDs from the GET to use in the POST
- foreach ($_GET['class_id'] as $class_id) {
- $sql = "SELECT class_id, DATE_FORMAT(date, '%a, %d %b') AS new_date, DATE_FORMAT(time, '%H:%i') AS new_time, venue FROM classes WHERE class_id = '$class_id'";
- $data = mysqli_query($dbc, $sql);
- //---------------------------------------------------------------------------------
- //get table data
- while ($row = mysqli_fetch_array($data)) {
- $date = $row["new_date"];
- $time = $row["new_time"];
- $venue = $row["venue"];
- $class_id = $row["class_id"];
- }
- //---------------------------------------------------------------------------------
- //Show a table of the selected classes
- echo '<input type="hidden" name="id" value= ' . $class_id . ' />';
- echo '<td>' . $date . '</td>';
- echo '<td>' . $time . '</td>';
- echo '<td>' . $venue . '</td>';
- echo '<td>' . $username . '</td>';
- echo '<td><button class="btn btn-mini" type="button"><i class="icon-user"></i><i class="icon-plus"</i></button></td></tr>';
- }
- echo'</table>';
- //Go Back button
- echo '<a class="btn btn-link pull-left" href="classes.php"><i class="icon-arrow-left"></i> Go back</a>';
- // Make booking button
- echo '<input type="submit" name="submit" class="btn btn-large btn-primary pull-right" value="Confirm">';
- echo '</form>';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement