<?php
/*
This file shorts the posting by 3 categories. 
*/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>IEEE Student Chapter| Short Posts </title>
<link rel="stylesheet" href="includes/style.css" type="text/css" /></head>
<body> 
<?php
include("header.htm");
include("includes/mysql_login.php");
echo "<div class=left>";
include("includes/left.htm");
echo "</div>";
echo "<div class=body>";
$sort = $_GET['sort'];
$query   = "SELECT COUNT(*) AS numrows FROM post where category = '$sort'";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
echo "<br>Number of Posts in this category: <b>" . $numrows = $row['numrows'] . "</b><br>";
/*
Maximum number of posting in one page is two (2). If there are more then 2 posting on one categories display then in more then on one page for
easy loadin. 
*/
$rowPerPage = 2;
$pageNum = 1; 
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}
$offset = ($pageNum - 1) * $rowPerPage;
$sort_post = "SELECT * FROM `post` where category = '$sort' ORDER BY `date_created` DESC LIMIT $offset, $rowPerPage";
$PageMax = ceil($numrows/$rowPerPage);
/*
This is for navigation between pages
*/
$self = $_SERVER['PHP_SELF']; 
$nav = '';
for($page = 1; $page <= $PageMax; $page++)
		{
			if($pageNum == $page)
			{
			$nav = $nav. " $page";
			} else
			{
			$nav = $nav."<a href = \"$self?sort=$sort&page=$page\">$page ||</a>";
			}
		}
			if($pageNum > 1)
			{
				$page = $pageNum - 1;
				$prev = "<a href = \"$self?sort=$sort&page=$page\">[prev]</a>";
				$first = "<a href = \"$self?sort=$sort&page=1\"><< </a>";
			
			} else
			{
				$prev = "";
				$first = "";
			}
			if($pageNum < $PageMax)
			{
				$page = $pageNum + 1;
				$next = "<a href = \"$self?sort=$sort&page=$page\">[next]</a>";
				$last = "<a href = \"$self?sort=$sort&page=$PageMax\"> >> </a>";
			} else 
			{
				$next = "";
				$last = "";
			}
$sort_post_result = mysql_query($sort_post) or die ('Error, query failed');
/*
Query the result and display them. Create the table with different background for odd and even row #999999
*/
while($user_post_result_row = mysql_fetch_array($sort_post_result))
{ 
	echo "<h3>Category: $user_post_result_row[category]</h3>";
		if($result_row['postid']%2 == 0)
		{
		echo "<table width=727 border=0 bgcolor=#999999>";               
		echo "<tr>";
		echo "<td>";
		} else
		{
		echo "<table width=727 border=0 bgcolor=#9999FF>";              
		echo "<tr>";
		echo "<td>";
		}
	echo "<h4>Title: ". $user_post_result_row[title] . "</h4><br>";
	echo " ". $user_post_result_row[post]. "<br>";
	if($user_post_result_row[attachment] != NULL)
	{
	echo "<i><a href=file_upload/$user_post_result_row[attachment]> Attachment</a></i><br>"; 
	} 
	echo "Date submitted : " . $user_post_result_row[date_created] ;
	// Get the corresponding comments for this post
	echo "<dd>";
	include("includes/getcomment.php");
	echo "</dd>";
		echo "</td>";
		echo "</tr>";
		echo "</table>";
} 
/*
Navigation pages. 
*/
echo "<br>Page $first$nav$last" ;
echo "</div>";
include("bottom.htm");
?>