<? require_once("../ihs/config.php"); ?>
<? require_once("top_inc.php"); ?>
<?
	// get date ranges
	$link = mysql_connect($db_server, $db_user, $db_password) or die("Could not connect");
	mysql_select_db($db_name) or die("Could not select database");
	$query = "SELECT substring(timestamp, 1, 8) as ts from imagehits order by timestamp desc";
    $result = mysql_query($query) or die("Query failed");
    while ($line = mysql_fetch_array($result)) {
		$dates[$line[ts]] = "";
    }
    mysql_free_result($result);

	$date = $_GET['date'];

	// get bandwidth
	if ($date == "") {
		$date = date("Ymd");
	}
	if ($date == "all") {
		$report = "All Dates";
		$query1 = "select sum(kb) as ttl from imagehits";
		$query2 = "select count(*) as ct from imagehits";
	} else {
		$report = substr($date, 4, 2) . "/" . substr($date, 6, 2) . "/" . substr($date, 0, 4);
		$start = $date . "000000";
		$end = $date . "235959";
		$query1 = "select sum(kb) as ttl from imagehits where timestamp >= $start and timestamp <= $end";
		$query2 = "select count(*) as ct from imagehits where timestamp >= $start and timestamp <= $end";
	}
    $result = mysql_query($query1) or die("Query failed.");
	$bandwidth = 0;
    while ($line = mysql_fetch_array($result)) {
		$bandwidth = $line[ttl];
    }
    mysql_free_result($result);

    $result = mysql_query($query2) or die("Query failed.");
	$images = 0;
    while ($line = mysql_fetch_array($result)) {
		$images = $line[ct];
    }

	mysql_close($link);
?>
&nbsp;&nbsp;<span class="ctracks">STATISTICS</span><br>
<br>
<hr>
<br>
<form name="stats" action="<?= $_SERVER['PHP_SELF'] ?>" method="post" style="margin-bottom: 0px;">
<div align="center"><a href="<?= $_SERVER['PHP_SELF'] ?>?date=<?= date("Ymd") ?>" class="submenu">Today</a> - <a href="<?= $_SERVER['PHP_SELF'] ?>?date=<?= date("Ymd", mktime(0, 0, 0, date("m"), date("d")-1,  date("Y"))) ?>" class="submenu">Yesterday</a> - Date: <select name="date" size="1"><option value="" selected></option><? foreach ($dates as $dt1 => $dt2) { ?><option value="<?= substr($dt1, 0, 4) . substr($dt1, 4, 2) . substr($dt1, 6, 2) ?>"><?= substr($dt1, 4, 2) . "/" . substr($dt1, 6, 2) .  "/" . substr($dt1, 0, 4) ?></option><? } ?></select>&nbsp;<input type="submit" name="go" value="go" style="font-size: smaller;"> - <a href="<?= $_SERVER['PHP_SELF'] ?>?date=all" class="submenu">Forever</a></div>
</form>
<br>
<br>
<div align="center">Report for <?= $report ?></div><br>
<table border="0" align="Center" cellpadding="4" cellspacing="0">
	<tr>
		<td align="right"><strong>Image Views:</strong></td>
		<td align="left"><?= number_format($images) ?></td>
	</tr>
	<tr>
		<td align="right"><strong>Bandwidth Used:</strong></td>
		<td align="left"><?= number_format($bandwidth) ?> Bytes</td>
	</tr>
</table>
<? require_once("bottom_inc.php"); ?>