 <pre><code>$_SESSION['userName'] = 'oneUser'; $activities = array(); $activityKeys = array('ID','name','spaces','date'); function getActivities() { global $activities,$activityKeys; $activities = array(); $contents = file('activity.txt'); foreach($contents as $line) { $activity = explode(':',$line); $data = array_combine($activityKeys,explode(',',$activity[0])); $users = isset($activity[1]) ? array_filter(explode(',',trim($activity[1]))) : array(); $activities[$data['ID']] = compact('data','users'); } } getActivities(); function userInActivity($activityID) { global $activities; return in_array($_SESSION['userName'],$activities[$activityID]['users']); } function saveActivities() { global $activities; $lines = array(); foreach($activities as $id =&gt; $activity) { $lines[$id] = implode(',',$activity['data']).':'.implode(',',array_filter($activity['users'])); } return file_put_contents('activity.txt',implode("\r\n",$lines)) !== false; } function bookActivity() { global $activities; if(isset($_POST['book'])) { $unbook = false; $bookID = intval($_POST['book']); if($bookID &lt; 0) { //If a negative ID has been submitted, that means we are "unbooking" $unbook = true; $bookID = abs($bookID); //Set the ID back to the normal, positive value } if($unbook) { $users = $activities[$bookID]['users']; $userKey = array_search($_SESSION['userName'],$users); array_splice($activities[$bookID]['users'],$userKey,1); } else { $activities[$bookID]['users'][] = $_SESSION['userName']; } saveActivities(); } } bookActivity(); function activityList($form=true) { global $activities; if($form === true) echo '&lt;form action="" method="post"&gt;'; ?&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;?php $firstKey = @array_shift(array_keys($activities)); foreach(array_keys($activities[$firstKey]['data']) as $index =&gt; $column) { echo '&lt;th&gt;'.$column.'&lt;/th&gt;'; } ?&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;?php foreach($activities as $activity) { echo '&lt;tr&gt;'; $id = $activity['data']['ID']; $activity['data']['spaces'] = $activity['data']['spaces']-count($activity['users']); foreach($activity['data'] as $item) { echo '&lt;td&gt;'.$item.'&lt;/td&gt;'; } $booked = userInActivity($id,$_SESSION['userName']); $buttonText = $booked ? 'Unbook' : 'Book'; $val = $booked ? -$id : $id; echo '&lt;td&gt;&lt;button type="submit" name="book" value="'.$val.'"&gt;'.$buttonText.'&lt;/button&gt;&lt;/td&gt;'; echo '&lt;/tr&gt;'; } ?&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;?php if($form === true) echo '&lt;/form&gt;'; } ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;title&gt;Activities&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;?php activityList(); ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>