Advertisement
HBiz

php checkbox array GET/POST

Jul 21st, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.18 KB | None | 0 0
  1. //Classes.php INITIAL TABLE OF CLASS CHOICES:
  2. -------------------------------------------------------------------------------------------
  3.  // Retrieve the class data from MySQL
  4.   $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";
  5.   $data = mysqli_query($dbc, $query);
  6.  
  7. // Loop through the array of class data, formatting it as HTML
  8.   echo '<table class="table table-striped table-hover">';
  9.   echo '<thead><tr><th>Date</th><th>Time</th><th>Venue</th><th>Spaces available</th><th>Select to book</th></tr></thead>';
  10.  
  11.   while ($row = mysqli_fetch_array($data)) {
  12.       echo '<tr><td>' . $row['new_date'] . '</td>';
  13.       echo '<td>' . $row['new_time'] . '</td>';
  14.       echo '<td>' . $row['venue'] . '</td>';
  15.       echo '<td>' . $row['free_spaces'] . '</td>';
  16.       echo '<form action="makebooking.php" method="get">';
  17.       echo '<td><input type="checkbox" name="class_id[]" value=' . $row['class_id'] . '</td></tr>';
  18.     }  
  19.     echo'</table>';
  20.  
  21.     // Make booking button
  22.       echo '<input type="submit" class="btn btn-large btn-primary pull-right" value="Book classes">';
  23.       echo '</form>';
  24.  
  25. -------------------------------------------------------------------------------------------
  26. //Makebooking.php $_GET AND $_POST TO REFLECT CHOICES AND ALLOW USER TO CONFIRM BEFORE UPDATING DB WITH BOOKING
  27. -------------------------------------------------------------------------------------------
  28. <?php
  29. //Start the session
  30. require_once('startsession.php');
  31. require_once('connectvars.php');
  32. require_once('header.php');
  33. ?>
  34.   <body>
  35.  
  36. <?php
  37. //Insert the navigation bar
  38. require_once('navbar.php');
  39.  
  40. //Check if the GET is set from classes.php
  41. if (isset($_GET['class_id'])) {
  42. // Grab the score data from the GET
  43.     foreach($_GET['class_id'] as $class_id) {
  44.         $_GET['class_id'] = $class_id;
  45.     }
  46. }
  47. //---------------------------------------------------------------------------------
  48. //Check if the shown bookings have been confirmed
  49.     else if (isset($_POST['class_id'])){
  50.         foreach($_POST['class_id'] as $class_id) {
  51.             $class_id = $_POST['class_id'];
  52.             $user_id = $_SESSION['user_id'];
  53.         }
  54. } else {
  55.     echo '<div class="alert">
  56.         <button type="button" class="close" data-dismiss="alert">&times;</button>
  57.         Please tick the boxes next to the classes you want to book.</br>
  58.         </div>';
  59. }
  60. //---------------------------------------------------------------------------------
  61. //If the confirm button has been hit:
  62. if (isset($_POST['submit'])) {
  63. //-------------------------------------------------------------------------------
  64. //Get user's ID from the users DB and set as $username
  65.     $who = "SELECT first_name, last_name, user_id FROM users WHERE email = '" . $_SESSION['email'] . "'";
  66.     $name = mysqli_query($dbc, $who);
  67.     $row = mysqli_fetch_array($name);
  68.     $username = $row['first_name'] . ' ' . $row['last_name'];
  69.    
  70.  // Connect to the database
  71.   $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  72.  
  73.     //UPDATE the bookings table;
  74.     $query = "INSERT INTO bookings (user_id, booking_name, class_id, time_stamp) VALUES ('$user_id', '$username', '$class_id', NOW())";
  75.         mysqli_query($dbc, $query);
  76.     // REDIRECT
  77.           $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/classes.php';
  78.     header('Location: ' . $home_url);
  79.     mysqli_close($dbc);
  80.     }
  81.  
  82. else if (isset($class_id)) {
  83. //---------------------------------------------------------------------------------
  84.  
  85. //table header
  86. echo '<table class="table table-bordered table-hover">';
  87.   echo '<thead><tr><th>Date</th><th>Time</th><th>Venue</th><th>Who\'s going?</th><th>Add someone</th></tr></thead>';
  88. //create the form
  89. echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
  90.  
  91. //Get the class IDs from the GET to use in the POST
  92.  
  93.     foreach ($_GET['class_id'] as $class_id) {
  94.     $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'";
  95.     $data = mysqli_query($dbc, $sql);
  96.  
  97. //---------------------------------------------------------------------------------
  98. //get table data
  99.     while ($row = mysqli_fetch_array($data)) {
  100.         $date = $row["new_date"];
  101.         $time = $row["new_time"];
  102.         $venue = $row["venue"];
  103.         $class_id = $row["class_id"];
  104.     }
  105.     //---------------------------------------------------------------------------------
  106. //Show a table of the selected classes
  107.  
  108.       echo '<input type="hidden" name="id" value= ' . $class_id . ' />';
  109.       echo '<td>' . $date . '</td>';
  110.       echo '<td>' . $time . '</td>';
  111.       echo '<td>' . $venue . '</td>';
  112.       echo '<td>' . $username . '</td>';
  113.       echo '<td><button class="btn btn-mini" type="button"><i class="icon-user"></i><i class="icon-plus"</i></button></td></tr>';
  114. }
  115.  echo'</table>';
  116.  
  117.     //Go Back button
  118.     echo '<a class="btn btn-link pull-left" href="classes.php"><i class="icon-arrow-left"></i> Go back</a>';
  119.  
  120.     // Make booking button
  121.       echo '<input type="submit" name="submit" class="btn btn-large btn-primary pull-right" value="Confirm">';
  122.       echo '</form>';
  123.  
  124. }
  125.  
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement