This is was a task for a university assignment for a web programming subject.

The task:

  • Read in a file containing class availability
  • Display a class timetable
  • Display session times, maximum class size, how many available spots in the class (from file)
  • Provide a channel for the user to book a the class
  • If a booking is made update the details that are output to the class file.

To keep things simple, I decided to display the timetable in the main content area of the website and place a booking form in the sidebar that contained:

  • Field – Name
  • Field – Activity – Drop down box where you can select the activity, eg, Personal Training session
  • Field – Session Day & Time – Drop down box that listed the day and session time, eg, Monday – 6.30 to 7.30am
  • Button – Make A Booking

The PHP To Read In The File, Check & Output The File

<?php

// Read file in for personal training classes
$file = fopen("activity-pt.txt", "r");
$ptDayTime = array();

while (!feof($file)) {
   $ptDayTime[] = fgets($file);
}

// Read file in for aerobics classes
$file = fopen("activity-ab.txt", "r");
$abDayTime = array();

while (!feof($file)) {
   $abDayTime[] = fgets($file);
}

// Check form
if ($_POST['submit']) {

// Variables
$ptformName = $_POST['ptFormName'];
$formActivity = $_POST['formActivity'];
$formDayTime = $_POST['formDayTime'];
$count = 0;

  // Check name is selected
	if (empty($_POST['ptFormName'])){
	  echo "<p>Please enter your name!</p>";
	} else {
	  $ptFormName = $_POST['ptFormName'];
		$count++;
	}

	// Checks activity is selected
	if ($_POST['formActivity'] == 'null') {
	  echo "<p>Please select your required activity</p>";
	} else {
	  $formActivity = $_POST['formActivity'];
		$count++;
	}

	// Checks day and time is selected
	if ($_POST['formDayTime'] == 'null') {
	  echo "<p>Please select your session day and time!</p>";
	} else {
	  $formDayTime = $_POST['formDayTime'];
		$count++;
	}
}

// Personal Training Successful booking
if ($count > 2 && $formActivity == 'pt' && $ptDayTime[$formDayTime] < 11) {
$ptDayTime[$formDayTime] = $ptDayTime[$formDayTime] + 1;
$ptOutputString = $ptDayTime[0]."n"
	  .$ptDayTime[1]."n"
		.$ptDayTime[2]."n"
		.$ptDayTime[3]."n"
		.$ptDayTime[4]."n"
		.$ptDayTime[5]."n"
		.$ptDayTime[6]."n"
		.$ptDayTime[7]."n"
		.$ptDayTime[8]."n"
		.$ptDayTime[9]."n"
		.$ptDayTime[10]."n"
		.$ptDayTime[11]."n"
		.$ptDayTime[12]."n"
		.$ptDayTime[13]."n"
		.$ptDayTime[14]."n"
		.$ptDayTime[15]."n"
		.$ptDayTime[16]."n"
		.$ptDayTime[17]."n"
		.$ptDayTime[18]."n"
		.$ptDayTime[19]."n"
		.$ptDayTime[20]."n";

  $pt = fopen("activity-pt.txt", "w");
	fwrite($pt, $ptOutputString);
	fclose($pt);
}

// Aerobics Successful booking
if ($count > 2 && $formActivity == 'ab' && $abDayTime[$formDayTime] < 11) {
$abDayTime[$formDayTime] = $abDayTime[$formDayTime] + 1;
$abOutputString = $abDayTime[0]."n"
	  .$abDayTime[1]."n"
		.$abDayTime[2]."n"
		.$abDayTime[3]."n"
		.$abDayTime[4]."n"
		.$abDayTime[5]."n"
		.$abDayTime[6]."n"
		.$abDayTime[7]."n"
		.$abDayTime[8]."n"
		.$abDayTime[9]."n"
		.$abDayTime[10]."n"
		.$abDayTime[11]."n"
		.$abDayTime[12]."n"
		.$abDayTime[13]."n"
		.$abDayTime[14]."n"
		.$abDayTime[15]."n"
		.$abDayTime[16]."n"
		.$abDayTime[17]."n"
		.$abDayTime[18]."n"
		.$abDayTime[19]."n"
		.$abDayTime[20]."n";

  $ab = fopen("activity-ab.txt", "w");
	fwrite($ab, $abOutputString);
	fclose($ab);
}

?>

The Timetable HTML

<div id="content">
<h1>Timetable, Bookings &amp; Availability</h1>
<p>Feel free to make a booking and check availability</p>
<h2>Personal Training</h2>
<div id="timetable-pt">
  <div id="days">
	  <div class="rowD">
		Day
		</div>
		<div class="row1">
		Monday
		</div>
		<div class="row2">
		Tuesday
		</div>
		<div class="row3">
		Wednesday
		</div>
		<div class="row4">
		Thursday
		</div>
		<div class="row5">
		Friday
		</div>
		<div class="row6">
		Saturday
		</div>
		<div class="row7">
		Sunday
		</div>
	</div><!-- #days ends -->
	<div id="session1">
		<div class="rowT1">
		Session Time<br/>
		6.30am - 7.30am<br/>
		Instructor: Jack D.
		</div>
		<div class="row1">
		Available: <?php $ans0 = 10 - $ptDayTime[0];
		  echo "$ans0";?> <br/>
		Max size: 10
		</div>
		<div class="row2">
		Available: <?php $ans1 = 10 - $ptDayTime[1];
		  echo "$ans1";?><br/>
		Max size: 10
		</div>
		<div class="row3">
		Available: <?php $ans2 = 10 - $ptDayTime[2];
		  echo "$ans2";?><br/>
		Max size: 10
		</div>
		<div class="row4">
		Available: <?php $ans3 = 10 - $ptDayTime[3];
		  echo "$ans3";?><br/>
		Max size: 10
		</div>
		<div class="row5">
		Available: <?php $ans4 = 10 - $ptDayTime[4];
		  echo "$ans4";?><br/>
		Max size: 10
		</div>
		<div class="row6">
		Available: <?php $ans5 = 10 - $ptDayTime[5];
		  echo "$ans5";?><br/>
		Max size: 10
		</div>
		<div class="row7">
		Available: <?php $ans6 = 10 - $ptDayTime[6];
		  echo "$ans6";?><br/>
		Max size: 10
		</div>
	</div><!-- #session1 ends -->

	<div id="session2">
		<div class="rowT2">
		Session Time<br/>
		12.30pm - 2.30pm<br/>
		Instructor: Jim B.
		</div>
		<div class="row1">
		Available: <?php $ans7 = 10 - $ptDayTime[7];
		  echo "$ans7";?><br/>
		Max size: 10
		</div>
		<div class="row2">
		Available: <?php $ans8 = 10 - $ptDayTime[8];
		  echo "$ans8";?><br/>
		Max size: 10
		</div>
		<div class="row3">
		Available: <?php $ans9 = 10 - $ptDayTime[9];
		  echo "$ans9";?><br/>
		Max size: 10
		</div>
		<div class="row4">
		Available: <?php $ans10 = 10 - $ptDayTime[10];
		  echo "$ans10";?><br/>
		Max size: 10
		</div>
		<div class="row5">
		Available: <?php $ans11 = 10 - $ptDayTime[11];
		  echo "$ans11";?><br/>
		Max size: 10
		</div>
		<div class="row6">
		Available: <?php $ans12 = 10 - $ptDayTime[12];
		  echo "$ans12";?><br/>
		Max size: 10
		</div>
		<div class="row7">
		Available: <?php $ans13 = 10 - $ptDayTime[13];
		  echo "$ans13";?><br/>
		Max size: 10
		</div>
	</div><!-- #session2 ends -->

	<div id="session3">
		<div class="rowT3">
		Session Time<br/>
		6.30 - 7.30<br/>
		Instructor: Bundy B.
		</div>
		<div class="row1">
		Available: <?php $ans14 = 10 - $ptDayTime[14];
		  echo "$ans14";?><br/>
		Max size: 10
		</div>
		<div class="row2">
		Available: <?php $ans15 = 10 - $ptDayTime[15];
		  echo "$ans15";?><br/>
		Max size: 10
		</div>
		<div class="row3">
		Available: <?php $ans16 = 10 - $ptDayTime[16];
		  echo "$ans16";?><br/>
		Max size: 10
		</div>
		<div class="row4">
		Available: <?php $ans17 = 10 - $ptDayTime[17];
		  echo "$ans17";?><br/>
		Max size: 10
		</div>
		<div class="row5">
		Available: <?php $ans18 = 10 - $ptDayTime[18];
		  echo "$ans18";?><br/>
		Max size: 10
		</div>
		<div class="row6">
		Available: <?php $ans19 = 10 - $ptDayTime[19];
		  echo "$ans19";?><br/>
		Max size: 10
		</div>
		<div class="row7">
		Available: <?php $ans20 = 10 - $ptDayTime[20];
		  echo "$ans20";?><br/>
		Max size: 10
		</div>
	</div><!-- #session3 ends -->
</div><!-- #timetable-pt ends -->
</form><!-- Registration Form Ends -->

<h3>Aerobics</h3>
<div id="timetable-ab">
	<div id="days">
    <div class="rowD">
	  Day
	  </div>
	  <div class="row1">
	  Monday
	  </div>
	  <div class="row2">
	  Tuesday
	  </div>
	  <div class="row3">
	  Wednesday
	  </div>
    <div class="row4">
	  Thursday
	  </div>
	  <div class="row5">
	  Friday
	  </div>
	  <div class="row6">
	  Saturday
	  </div>
	  <div class="row7">
	  Sunday
	  </div>
	</div><!-- #days ends -->
	<div id="session1">
		<div class="rowT1">
		Session Time<br/>
		6.30am - 7.30am<br/>
		Instructor: Bundy B.
		</div>
		<div class="row1">
		Available: <?php $abAns0 = 10 - $abDayTime[0];
		  echo "$abAns0";?><br/>
		Max. size: 10
		</div>
		<div class="row2">
		Available: <?php $abAns1 = 10 - $abDayTime[1];
		  echo "$abAns1";?><br/>
		Max. size: 10
		</div>
		<div class="row3">
		Available: <?php $abAns2 = 10 - $abDayTime[2];
		  echo "$abAns2";?><br/>
		Max. size: 10
		</div>
		<div class="row4">
		Available: <?php $abAns3 = 10 - $abDayTime[3];
		  echo "$abAns3";?><br/>
		Max. size: 10
		</div>
		<div class="row5">
		Available: <?php $abAns4 = 10 - $abDayTime[4];
		  echo "$abAns4";?><br/>
		Max. size: 10
		</div>
		<div class="row6">
		Available: <?php $abAns5 = 10 - $abDayTime[5];
		  echo "$abAns5";?><br/>
		Max. size: 10
		</div>
		<div class="row7">
		Available: <?php $abAns6 = 10 - $abDayTime[6];
		  echo "$abAns6";?><br/>
		Max. size: 10
		</div>
	</div><!-- #session1 ends -->

	<div id="session2">
		<div class="rowT2">
		Session Time<br/>
		12.30pm - 2.30pm<br/>
		Instructor: Jack D.
		</div>
		<div class="row1">
		Available: <?php $abAns7 = 10 - $abDayTime[7];
		  echo "$abAns7";?><br/>
		Max. size: 10
		</div>
		<div class="row2">
		Available: <?php $abAns8 = 10 - $abDayTime[8];
		  echo "$abAns8";?><br/>
		Max. size: 10
		</div>
		<div class="row3">
		Available: <?php $abAns9 = 10 - $abDayTime[9];
		  echo "$abAns9";?><br/>
		Max. size: 10
		</div>
		<div class="row4">
		Available: <?php $abAns10 = 10 - $abDayTime[10];
		  echo "$abAns10";?><br/>
		Max. size: 10
		</div>
		<div class="row5">
		Available: <?php $abAns11 = 10 - $abDayTime[11];
		  echo "$abAns11";?><br/>
		Max. size: 10
		</div>
		<div class="row6">
		Available: <?php $abAns12 = 10 - $abDayTime[12];
		  echo "$abAns12";?><br/>
		Max. size: 10
		</div>
		<div class="row7">
		Available: <?php $abAns13 = 10 - $abDayTime[13];
		  echo "$abAns13";?><br/>
		Max. size: 10
		</div>
	</div><!-- #session2 ends -->

	<div id="session3">
		<div class="rowT3">
		Session Time<br/>
		6.30 - 7.30<br/>
		Instructor: Jim B.
		</div>
		<div class="row1">
		Available: <?php $abAns15 = 10 - $abDayTime[15];
		  echo "$ans15";?><br/>
		Max. size: 10
		</div>
		<div class="row2">
		Available: <?php $abAns16 = 10 - $abDayTime[16];
		  echo "$ans16";?><br/>
		Max. size: 10
		</div>
		<div class="row3">
		Available: <?php $abAns17 = 10 - $abDayTime[17];
		  echo "$ans17";?><br/>
		Max. size: 10
		</div>
		<div class="row4">
		Available: <?php $abAns18 = 10 - $abDayTime[18];
		  echo "$ans18";?><br/>
		Max. size: 10
		</div>
		<div class="row5">
		Available: <?php $abAns19 = 10 - $abDayTime[19];
		  echo "$ans19";?><br/>
		Max. size: 10
		</div>
		<div class="row6">
		Available: <?php $abAns20 = 10 - $abDayTime[20];
		  echo "$ans20";?><br/>
		Max. size: 10
		</div>
		<div class="row7">
		Available: <?php $abAns21 = 10 - $abDayTime[21];
		  echo "$abAns21";?><br/>
		Max. size: 10
		</div>
	</div><!-- #session3 ends -->
</div><!-- #timetable-ab ends -->
</div><!-- #content ends -->

The Sidebar Booking Form

<form id="pt-booking-form" action="bookings.php" method="post">
<!-- Name -->
<div class="pt_form_name">
	<span class="pt_form_label">Name:*</span><br/>
	<input name="ptFormName" type="text" value="<?php $ptFormName;?>" id="pt-form-input" /><br />
</div><!-- Name Ends -->
<!-- Activity -->
<div class="form_activity">
  <span class="pt_form_label">Activity:*</span><br/>
	<select name="formActivity" id="activity">
	  <option value="null" selected="selected">Choose Activity</option>
		<option value="pt">Personal Training</option>
		<option value="ab">Aerobics</option>
	</select>
</div><!-- .Form activity ends -->

<!-- Day & Time -->
<div class="form_day_time">
  <span class="pt_form_label">Session Day &amp; Time*</span><br/>
	<select name="formDayTime" id="dayTime">
	  <option value="null" selected="selected">Select Session Day &amp; Time</option>
		<option value="0">Monday 6.30am - 7.30am</option>
		<option value="7">Monday 12.30pm - 2.30pm</option>
		<option value="14">Monday 6.30pm - 7.30pm </option>

		<option value="1">Tuesday 6.30am - 7.30am</option>
		<option value="8">Tuesday 12.30pm - 2.30pm</option>
		<option value="15">Tuesday 6.30pm - 7.30pm </option>

		<option value="2">Wednesday 6.30am - 7.30am</option>
		<option value="9">Wednesday 12.30pm - 2.30pm</option>
		<option value="16">Wednesday 6.30pm - 7.30pm </option>

		<option value="3">Thursday 6.30am - 7.30am</option>
		<option value="10">Thursday 12.30pm - 2.30pm</option>
		<option value="17">Thursday 6.30pm - 7.30pm </option>

		<option value="4">Friday 6.30am - 7.30am</option>
		<option value="11">Friday 12.30pm - 2.30pm</option>
		<option value="18">Friday 6.30pm - 7.30pm </option>

		<option value="5">Saturday 6.30am - 7.30am</option>
		<option value="12">Saturday 12.30pm - 2.30pm</option>
		<option value="19">Saturday 6.30pm - 7.30pm </option>

		<option value="6">Sunday 6.30am - 7.30am</option>
		<option value="13">Sunday 12.30pm - 2.30pm</option>
		<option value="20">Sunday 6.30pm - 7.30pm </option>
	</select>
</div>
<!-- Submit -->
<div>
  <input type="submit" name="submit" value="Make A Booking" />
</div>
<!-- Submit Ends -->
<p>* Required field</p>
</form>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>