<?php 

	$category = $_GET['id'];
	include('config.php');
	

	$catquery = "select * from categories where id =".$category;
	$catresult = mysql_query($catquery,$connection) or die(mysql_error());
	$catinfo = mysql_fetch_array($catresult);
	$cattitle = $catinfo['name'];
	
	if (!$catinfo) {
		header('Location: index.php');
	}


	$cathead = 1;
	$catheadtitle = $cattitle." Portal";
	$metatitle = $cattitle." Portal - ";

	include('header.php'); 
	include('sidebar.php'); 
	
	if ($seourls == 1) {
	include ('paginator-seo.php');
} else {
	include ('paginator.php');
}

	//for pagination
	$rowsquery = "select * from articles where status=0 and categoryid = ".$category." OR parentid =  ".$category;
	$rowsresults = mysql_query($rowsquery,$connection) or die(mysql_error());
	$rows_results = mysql_num_rows($rowsresults);
	
	$pages = new Paginator;
	if ($seourls == 1) {
		$pages->nav = $siteurl."/category/".$category."/".$cattitle;
	} else {
		$pages->urlparam = "?id=".$category;
	}
	$pages->items_total = $rows_results;
	$pages->mid_range = 9;
	$pages->paginate();

	if ($pages->items_total) {	
	$query = "select * from articles where status=0 and categoryid = ".$category." OR parentid =  ".$category." 
				order by date desc ".$pages->limit;
	} else {
	$query = "select * from articles where status=0 and categoryid = ".$category." OR parentid =  ".$category." 
				order by date desc";
	}
	$articleresults = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($articleresults);

	?>
	
	<!-- CONTENT AREA -->
	<div id="content">	

	
	<?php
	
	// Populates the catbox list with all categories and subcats
	$query = "select * from categories where parentid =".$category.";"; 
	$sub_result = mysql_query($query,$connection) or die(mysql_error());
	$sub_num_results = mysql_num_rows($sub_result);
	
	if (!$sub_num_results) {
	} else {
		echo "<div id=\"catbox\">
		<center><h3>Explore ".$cattitle."</h3></center>
		<ul>";
		for ($x=0; $x <$sub_num_results; $x++) { 
			$subrow = mysql_fetch_assoc($sub_result); 
			if ($seourls == 1) { $scrubsubname = generate_seo_link($subrow['name']); }
			
			if ($seourls == 1) { // With SEO URLS
				echo "<li><a href=\"".$siteurl."/category/".$subrow['id']."/".$scrubsubname."/\">".$subrow['name']."</a></li>";
			} else {
				echo "<li><a href=\"".$siteurl."/category.php?id=".$subrow['id']."\">".$subrow['name']."</a></li>";
			}
		}	
		echo "</ul> 
		<br style=\"clear:both\"/>
		</div>";
	}
	

	echo '<span style="float:right; font-weight: bold; padding: 8px;"><img src="'.$siteurl.'/images/rss.png" /> <a style="margin-top: 5px;" href="'.$siteurl.'/feed.php?cat='.$category.'">'.$cattitle.'</a></span>';
	echo "<h1 id=\"title\">Latest in ".$cattitle."</h1>";
if ($num_results == 0) { // Checks if category is empty
	echo "<br/><p><b>No articles found!</b></p>";
	
} else {

for ($i=0; $i <$num_results; $i++) { 
	$row = mysql_fetch_assoc($articleresults); 
	
//Get article info	
	$id = $row['id'];
	$authorid = $row['authorid'];
	$date = strtotime($row['date']);
	$artdate = date('m/d/y', $date);
	$categoryid = $row['categoryid'];
	$title = $row['title'];
	if ($seourls == 1) { $scrubtitle = generate_seo_link($title); }
	$removedivs = array("<div>", "</div>");
	$body = strip_tags(substr($row['body'], 0, 300));

//get author info
	$authorquery = "select * from authors where id=".$authorid;
	$authorresult = mysql_query($authorquery,$connection) or die(mysql_error());
	$authorinfo = mysql_fetch_array($authorresult);
	$authorname = $authorinfo['displayname'];
	if ($seourls == 1) { $scrubauthor = generate_seo_link($authorname); }
	
//get category info
	$catquery = "select * from categories where id=".$categoryid;
	$catresult = mysql_query($catquery,$connection) or die(mysql_error());
	$catinfo = mysql_fetch_array($catresult);
	$categoryname = $catinfo['name'];
	$catparent = $catinfo['parentid'];
	if ($seourls == 1) { $scrubcatname = generate_seo_link($categoryname); }

	if ($catparent == NULL) {
			if ($seourls == 1) { // With SEO URLS
				$displaycat = "<a href=\"".$siteurl."/category/".$categoryid."/".$scrubcatname."/\"><b>".$categoryname."</b></a>";
			} else {
				$displaycat = "<a href=\"".$siteurl."/category.php?id=".$categoryid."\"><b>".$categoryname."</b></a>";
			}
	
	} else {
//get category info
	$query = "select * from categories where id=".$catparent;
	$result = mysql_query($query,$connection) or die(mysql_error());
	$info = mysql_fetch_array($result);
	$parentname = $info['name'];
	if ($seourls == 1) { $scrubparent = generate_seo_link($parentname); }

	if ($seourls == 1) { // With SEO URLS
		$displaycat = "<a href=\"".$siteurl."/category/".$catparent."/".$scrubparent."/\"><b>".$parentname."</b></a> > 
				   <a href=\"".$siteurl."/category/".$categoryid."/".$scrubcatname."/\"><b>".$categoryname."</b></a>";
	} else {
		$displaycat = "<a href=\"".$siteurl."/category.php?id=".$catparent."\"><b>".$parentname."</b></a> > 
				   <a href=\"".$siteurl."/category.php?id=".$categoryid."\"><b>".$categoryname."</b></a>";
	}
	}

	if ($seourls == 1) { // With SEO URLS
		echo "<h2><a href=\"".$siteurl."/".$id."/".$scrubtitle."/\">".$title."</a></h2>"; 
		echo $body."...";
	echo "<div class=\"article-meta\">".$displaycat." | By: <a href=\"".$siteurl."/profile/".$authorid."/".$scrubauthor."/\"><b>".$authorname."</b></a> (".$artdate.")</div>";

	} else {
		echo "<h2><a href=\"".$siteurl."/article.php?id=".$id."\">".$title."</a></h2>"; 
		echo $body."...";
	echo "<div class=\"article-meta\">".$displaycat." | By: <a href=\"".$siteurl."/profile.php?a=".$authorid."\"><b>".$authorname."</b></a> (".$artdate.")</div>";

	}

}

}


//display pagination
	echo '<span style="float:left; margin: 10px 1px 2px 0;"> '. $pages->display_items_per_page() . '</span>';  
	echo '<span style="float:right; margin: 10px 10px 2px 0;"><b>Page Navigation: '.$pages->display_pages().'</b></span><div style="clear:both"></div>';

?>

</div>


<?php include('rightsidebar.php');

 include('footer.php'); ?>
