Location: PHPKode > projects > Run > Run-0.8.0.2/Run-0.8.0.2/administrator.php
<?php
include("system/environment.php");
include("system/functions.php");
?>

<html>
<?php
include("system/head.php");
?>
<body>

<script language="javascript" type="text/javascript">
function addnewrunnerWindow(url)
{
	link = window.open(url, 'addnewrunner', 'location=no, toolbar=no, scrollbars=no, width=340px, height=640px');
}

function editrunnerWindow(url)
{
	link = window.open(url, 'editrunner', 'location=no, toolbar=no, scrollbars=no, width=340px, height=740px');
}

function settingsWindow(url)
{
	link = window.open(url, 'settings', 'location=no, toolbar=no, scrollbars=no, width=340px, height=400px');
}
</script>

<div class="logo"><img src="system/imgs/runsmall.png"></div>

<div class="navigation">
<a href="javascript:addnewrunnerWindow('addnewrunner.php')" title="Add a New Runner: Alt+N" accesskey="n" class="url">Add <u>N</u>ew Runner</a> |
<?php
if ($_GET['inactive'] == 1) {
	echo "<a href=\"administrator.php\" title=\"Show Active Runners: Alt+A\" accesskey=\"a\" class=\"url\">Show <u>A</u>ctive Runners</a> | ";
} else {
	echo "<a href=\"administrator.php?inactive=1\" title=\"Show Inactive Runners: Alt+I\" accesskey=\"i\" class=\"url\">Show <u>I</u>nactive Runners</a> | ";
}
?>
<a href="javascript:settingsWindow('adminsettings.php')" title="Run Settings: Alt+S" accesskey="s" class="url">Run <u>S</u>ettings</a> |
<a href="<?php $_SERVER['PHP_SELF']?>" title="Refresh: Alt+R" accesskey="r" class="url"><u>R</u>efresh</a> |
<a href="administrator.php?logout=1" title="Logout: Alt+Q" accesskey="q" class="url">Logout</a>
</div>


<div class="content">

<table border="0">
<?php
//display the data from the Runners table
//first part of the if statemend deals with inactive runners
if ($_GET['inactive'] == 1) {
	//this part picks only INACTIVE runner's data from the database and displays that data

	//query and get data
	$result = mysql_query(onlyletter('N', $mysqlTableRunners)) or die (mysql_error());

	echo "<tr><td colspan=\"2\" class=\"name\">Inactive runners: " . letters('N', $mysqlTableRunners) . "</td></tr>";
	while ($runners = mysql_fetch_assoc($result)) {
		printf("
			<tr><td class=\"name\"><b>%s</b>
			<ul class=\"runner\">
			<li><a href=\"javascript:editrunnerWindow('editrunner.php?ID=%s')\" class=\"url\">Edit %s's Data</a></li>
			<li><a href=\"%s&showlastten=1&ID=%s&inactive=1\" class=\"url\">Show %s's Last 10 Runs</a></li>
			</ul></td>
			<td class=\"left\"><b>First Name:</b> %s<br>
			<b>Last Name:</b> %s<br>
			<b>Date of Birth:</b> %s<br>
			<b>Gender:</b> %s<br>
			<b>Member Since:</b> %s<br>
			<b>Notes:</b> %s<br>
			<b></b><br>
			</td></tr>",
			$runners['RunnerID'], $runners['ID'], $runners['RunnerID'], viewlastten(), $runners['ID'], $runners['RunnerID'], $runners['FirstName'], $runners['LastName'], datum($runners['DateOfBirth']), gender($runners['Gender']), datum($runners['MemberSince']), nonotes($runners['Notes']));
	}

} else {
	//this part picks only ACTIVE runner's data from the database and displays that data

	//query and get data
	$result = mysql_query(onlyletter('Y', $mysqlTableRunners)) or die(mysql_error());

	echo "<tr><td colspan=\"2\" class=\"name\">Active runners: " . letters('Y', $mysqlTableRunners) . "</td></tr>";
	while ($runners = mysql_fetch_assoc($result)) {
		printf("
			<tr><td class=\"name\"><b>%s</b>
			<ul class=\"runner\">
			<li><a href=\"javascript:editrunnerWindow('editrunner.php?ID=%s')\" class=\"url\">Edit %s's Data</a></li>
			<li><a href=\"%s&showlastten=1&ID=%s\" class=\"url\">Show %s's Last 10 Runs</a></li>
			</ul></td>
			<td class=\"left\"><b>First Name:</b> %s<br>
			<b>Last Name:</b> %s<br>
			<b>Date of Birth:</b> %s<br>
			<b>Gender:</b> %s<br>
			<b>Member Since:</b> %s<br>
			<b>Notes:</b> %s<br>
			<b></b><br>
			</td></tr>",
			$runners['RunnerID'], $runners['ID'], $runners['RunnerID'], viewlastten(), $runners['ID'], $runners['RunnerID'], $runners['FirstName'], $runners['LastName'], datum($runners['DateOfBirth']), gender($runners['Gender']), datum($runners['MemberSince']), nonotes($runners['Notes']));
	}
}
?>
</table>
</div>

<div class="lastten">

<?php
//if Show last ten runs is clicked on the right side of the administrator page, last ten runs for the selected runers are displayed. if there are no runs, a message saying so is displayed.
if ($_GET['showlastten'] == 1) {
	$getRunnerID = mysql_fetch_assoc(mysql_query("select RunnerID, Measurements from $mysqlTableRunners where ID='".$_GET['ID']."'")) or die (mysql_error());
	$allEntriesQuery = mysql_query("select count(*) from $mysqlTableRunningLogs where RunnerID='".$getRunnerID['RunnerID']."'");
	$allEntries = mysql_fetch_row($allEntriesQuery);

	//get the distance unit
	$units = explode("-", $getRunnerID['Measurements']);
	$distUnit = $units['0'];


	if ($allEntries['0'] < 1) {
		echo "<table border=\"0\">";
		echo "<tr><td class=\"left\" colspan=\"8\"><p class=\"header\">{$getRunnerID['RunnerID']} doesn't have any runs logged.</p></th></tr>";
		echo "</table>";
	} else {
		//if there are less then ten entries by selected runner, show them all
		//if there are more than ten, offset so that only last ten are displayed
		if ($allEntries['0'] < 10) {
			$tenLess = 0;
			$lastTen = mysql_query("select * from $mysqlTableRunningLogs where RunnerID='".$getRunnerID['RunnerID']."' order by Date asc");
		} else {
			$tenLess = $allEntries['0'] - 10;
			$lastTen = mysql_query("select * from $mysqlTableRunningLogs where RunnerID='".$getRunnerID['RunnerID']."' order by Date asc limit $tenLess, 10");
		}

		$numRows = mysql_num_rows($lastTen);



	//get the total distance for runs in kilometers, put values in an array and calculate the sum of that array
	$distanceTotalKmQuery = mysql_query("select Distance from $mysqlTableRunningLogs where RunnerID='{$getRunnerID['RunnerID']}' and DistanceUnit='km' limit $tenLess, 10");
	while ($distanceTotalKmArray = mysql_fetch_array($distanceTotalKmQuery)) {
		$distanceStackKm[] = $distanceTotalKmArray['0'];
	}
	$distanceTotalKm = array_sum($distanceStackKm);

	//get the total distance for runs in miles, put values in an array and calculate the sum of that array
	$distanceTotalMiQuery = mysql_query("select Distance from $mysqlTableRunningLogs where RunnerID='{$getRunnerID['RunnerID']}' and DistanceUnit='mi' limit $tenLess, 10");
	while ($distanceTotalMiArray = mysql_fetch_array($distanceTotalMiQuery)) {
		$distanceStackMi[] = $distanceTotalMiArray['0'];
	}
	$distanceTotalMi = array_sum($distanceStackMi);

	//if distance unit in runner's settings is 'km', convert all mile entries to kilometers and add the two values. round the result to two decimal values.
	//if distance unit in runner's settings is 'mi', convert all kilometer entries to miles and add the two values. round the result to two decimal values.
	if ($distUnit == 'km') {
		$distanceMiToKm = $distanceTotalMi * 1.609344;
		$distanceTotal = round(($distanceTotalKm + $distanceMiToKm), 2);
	} elseif ($distUnit == 'mi') {
		$distanceKmToMi = $distanceTotalKm * 0.621371192;
		$distanceTotal = round(($distanceTotalMi + $distanceKmToMi), 2);
	}

	//get the average per run distance
	$distanceAvg =  round(($distanceTotal / $numRows), 2);


	//get the total time in seconds, put the values in an array and add them
	$timeTotalQuery = mysql_query("select time_to_sec(Time) from $mysqlTableRunningLogs where RunnerID='".$getRunnerID['RunnerID']."' limit $tenLess, 10");
	while ($timeTotalArray = mysql_fetch_array($timeTotalQuery)) {
		$timeTotalSecStack[] = $timeTotalArray['0'];
	}


	$timeTotalSecSum = array_sum($timeTotalSecStack);
	//calculate total hours, minutes and seconds from the total seconds value
	$timeTotalHrs = intval($timeTotalSecSum / 3600);
	$timeTotalMins = intval(($timeTotalSecSum % 3600) / 60);
	$timeTotalSecs = intval(($timeTotalSecSum % 3600) % 60);

	if ($timeTotalHrs == '') {
		$timeTotalHrs = '00';
	} elseif (strlen($timeTotalHrs) == 1) {
		$timeTotalHrs = 0 . $timeTotalHrs;
	}
	if ($timeTotalMins == '') {
		$timeTotalMins = '00';
	} elseif (strlen($timeTotalMins) == 1) {
		$timeTotalMins = 0 . $timeTotalMins;
	}
	if ($timeTotalSecs == '') {
		$timeTotalSecs = '00';
	} elseif (strlen($timeTotalSecs) == 1) {
		$timeTotalSecs = 0 . $timeTotalSecs;
	}

	//format the total time in HH:MM:SS value
	$timeTotal = implode(":", array($timeTotalHrs, $timeTotalMins, $timeTotalSecs));


	$timeAvgSecSum = $timeTotalSecSum / $numRows;
	//calculate average hours, minutes and seconds from the average seconds value
	$timeAvgHrs = intval($timeAvgSecSum / 3600);
	$timeAvgMins = intval(($timeAvgSecSum % 3600) / 60);
	$timeAvgSecs = intval(($timeAvgSecSum % 3600) % 60);

	if ($timeAvgHrs == '') {
		$timeAvgHrs = '00';
	} elseif (strlen($timeAvgHrs) == 1) {
		$timeAvgHrs = 0 . $timeAvgHrs;
	}
	if ($timeAvgMins == '') {
		$timeAvgMins = '00';
	} elseif (strlen($timeAvgMins) == 1) {
		$timeAvgMins = 0 . $timeAvgMins;
	}
	if ($timeAvgSecs == '') {
		$timeAvgSecs = '00';
	} elseif (strlen($timeAvgSecs) == 1) {
		$timeAvgSecs = 0 . $timeAvgSecs;
	}

	//format the average time in HH:MM:SS value
	$timeAvg = implode(":", array($timeAvgHrs, $timeAvgMins, $timeAvgSecs));

		echo "<table cellspacing=\"0\" border=\"0\">";
		echo "<tr><td class=\"left\" colspan=\"8\"><p class=\"header\">Last ten runs by {$getRunnerID['RunnerID']}</p></th></tr>";
		echo "<tr><td height=\"7px\"></td></tr>";
		echo "<tr>";
		echo "<th class=\"display\">&nbsp;Day</th>";
		echo "<th class=\"display\">Time of Day</th>";
		echo "<th class=\"display\">Run Type</th>";
		echo "<th class=\"display\">Time</th>";
		echo "<th class=\"display\">Distance</th>";
		echo "<th class=\"display\">AHR</th>";
		echo "<th class=\"display\">Feeling</th></tr>";


		while ($details = mysql_fetch_assoc($lastTen)) {

		//if row is even, use different tr style
		//first pass $numRows to oddEven function which finds out whether the passed number is odd or even, and then subtract 1 from $numRows to get 1 less than $numRows was in previous turn
		$row = oddEven($numRows);
		$numRows--;

		printf("
		<tr class=\"display%s\"><td class=\"lasttenright\">%s</td>
		<td class=\"lastten\">%s</td>
		<td class=\"lastten\">%s</td>
		<td class=\"lastten\">%s</td>
		<td class=\"lasttenright\">%s %s</td>
		<td class=\"lasttenright\">%s bpm</td>
		<td class=\"lastten\">%s</td></tr>",
		$row, datum($details["Date"]), $details["TimeOfDay"], $details["RunType"], $details["Time"], stripslashes($details["Distance"]), $details["DistanceUnit"], stripslashes($details["AHR"]), $details["Feeling"]);
		}
		echo "<tr><td colspan=\"3\" class=\"lasttenright\"><b>Total:</b></td><td class=\"lasttenright\">$timeTotal</td><td class=\"lasttenright\">$distanceTotal $distUnit</td></tr>";
		echo "<tr><td colspan=\"3\" class=\"lasttenright\"><b>Average:</b></td><td class=\"lasttenright\">$timeAvg</td><td class=\"lasttenright\">$distanceAvg $distUnit</td></tr>";
		echo "</table>";
	}

}
?>
</div>

</body>
</html>
Return current item: Run