<?php
include "header.php";

$page = $_GET["page"];
if (!($page > 1))
{
	$page = 1;
}

$step = 15;
$start = ($page - 1) * $step;
$catid = $_GET["catid"];

$query = "select eventid, title, startdate, stopdate from events order by startdate desc limit $start, $step";

$result = mysql_query($query) or die (mysql_error());

echo "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\">";
if (mysql_num_rows($result) < 1)
{
	echo "<tr><td colspan=5>Currently no Event, please add one <a href=\"addevent.php\">here</a></td></tr>";
}
else
{
	echo "<tr bgcolor=\"#a0a0a0\"><th>Event</th><th>Start Date</th><th>Stop Date</th><th colspan=2>Options</th></tr>";
	
	$tri = true;
	
	while ($row = mysql_fetch_array($result))
	{
		$eventid = $row[0];
		$title = $row[1];
		$startdate = formatdate($row[2]);
		$stopdate = formatdate($row[3]);
		
		$tri = ! $tri;
		if ($tri)
		{
			echo "<tr bgcolor=\"#c9c9c9\">";
		}
		else
		{
			echo "<tr bgcolor=\"#e9e9e9\">";
		}
		
		echo "<td>$title</td>";
		echo "<td>$startdate</td>";
		echo "<td>$stopdate</td>";
		echo "<td align=\"right\"><form action=\"modevent.php\" method=\"post\"><input type=\"hidden\" name=\"id\" value=\"$eventid\"><input type=\"submit\" value=\"Modify\"></td></form>";
		echo "<td><form action=\"delevent.php\" method=\"post\"><input type=\"hidden\" name=\"id\" value=\"$eventid\"><input type=\"submit\" value=\"Delete\"></td></form>";
	}
}
pages($catid, $step);
?>

<?php
include "footer.php";

function pages($catid, $step)
{
	if ($catid > 0)
	{
		$query = "select count(title) from events, event_cat where event_cat.catid='$catid' and events.eventid=event_cat.eventid";
	}
	else
	{
		$query = "select count(*) from events";
	}
	$res = mysql_query($query);
	while ($ro = mysql_fetch_array($res))
	{
		$total = $ro[0];
	}
	
	$pages = ceil($total / $step);
	
	if ($pages > 1)
	{
		echo "<tr><td colspan=5><table><tr><td>Page:</td>";
	
		for ($i = 1; $i <= 7 && $i <= $pages; $i++)
		{
			echo "<td><a href=\"" . $_SERVER["PHP_SELF"] . "?catid=$catid&page=$i\">$i</a></td>";
		}
	
		if ($i <= $pages)
		{
			echo "<td>...</td><td><a href=\"" . $_SERVER["PHP_SELF"] . "?catid=$catid&page=$pages\">$pages</a></td>";
			echo "<td><form action=\"" . $_SERVER["PHP_SELF"] . "\" method=get><input type=text name=catid value=$catid><input type=text name=page size=3><input type=submit value=Go></td></form>";
		}
	
		echo "</tr></table></td></tr>";
	}
}

function formatdate($date)
{
	if ($date == "0000-00-00")
	{
		return '';
	}
	else
	{
		$split = explode("-", $date);
		$date = date("jS M, Y", mktime(0, 0, 0, $split[1], $split[2], $split[0]));
		return $date;
	}
}
?>