Location: PHPKode > projects > ArticleSetup > ArticleSetup-v1-1/upload/category.php
<?php 
	include('config.php');
	
	//Create site settings variables			
	$sitequery = 'select * from settings;';
	$siteresult = mysql_query($sitequery,$connection) or die(mysql_error());
	$siteinfo = mysql_fetch_array($siteresult);
	$siteurl = $siteinfo['url'];


	$category = $_GET['id'];

	
    if (!is_numeric($category)) {
              header('Location: '.$siteurl); 
         }
        
        else

        {        

	$catquery = "select * from categories where id =".$category;
	$catresult = mysql_query($catquery,$connection) or die(mysql_error());
	$catinfo = mysql_fetch_array($catresult);
	$cattitle = $catinfo['name'];

	$cathead = 1;
	$catheadtitle = $cattitle." Portal";
	$metatitle = $cattitle." Portal - ";
	
	// if the category doesn't exist, redirect to homepage
	if (!$catinfo) {
		header('Location: index.php');
	}	
	
	include('header.php'); 
	include('sidebar.php'); 


// Setup the category template
	$categorytemp = new Template("templates/".$template."/category.tpl");
	$categorytemp->set("siteurl", $siteurl);
	$categorytemp->set("cattitle", $cattitle);
	$categorytemp->set("catid", $category);
	
		
	if ($seourls == 1) {
		include ('paginator-seo.php');
	} else {
		include ('paginator.php');
	}

// Setup pagination controls
	$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);


	
// Create the category box with all subcategories
	$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 it's a subcategory, display the parent's subcats (complicated!)
	if (!$sub_num_results) {
		
		$categorytemp->set("catboxtop", "");
		$categorytemp->set("catboxlist", "");
		$categorytemp->set("catboxbottom", "");
		
		$subcatquery = "select * from categories where id =".$category.";"; 
		$subcatresult = mysql_query($subcatquery,$connection) or die(mysql_error());
		$subcatinfo = mysql_fetch_array($subcatresult);
		$parentid = $subcatinfo['parentid'];
		
		if ($parentid) {
		$categorytemp->set("catboxtop", '<div id="catbox"> <center><h3>Explore '.$cattitle.'</h3></center> <ul>');
		$getquery = "select * from categories where parentid =".$parentid.";"; 
		$getresult = mysql_query($getquery,$connection) or die(mysql_error());
		$get_num_results = mysql_num_rows($getresult);

		for ($x=0; $x <$get_num_results; $x++) { 
			$subrow = mysql_fetch_assoc($getresult); 
			if ($seourls == 1) { $scrubsubname = generate_seo_link($subrow['name']); }
			
			if ($seourls == 1) { // With SEO URLS
				$catboxlist = "<li><a href=\"".$siteurl."/category/".$subrow['id']."/"
							.$scrubsubname."/\">".$subrow['name']."</a></li>";
				$catboxoutput .= $catboxlist;
			} else {
				$catboxlist = "<li><a href=\"".$siteurl."/category.php?id=".$subrow['id']."\">"
							.$subrow['name']."</a></li>";
				$catboxoutput .= $catboxlist;			
			}
		$categorytemp->set("catboxbottom",  '</ul><br style="clear:both"/></div>');
		$categorytemp->set("catboxlist", $catboxoutput);
		
		}
		}	
		
	} else {
		for ($x=0; $x <$sub_num_results; $x++) { 
			
			$categorytemp->set("catboxtop", '<div id="catbox"> <center><h3>Explore '.$cattitle.'</h3></center> <ul>');

			$subrow = mysql_fetch_assoc($sub_result); 
			if ($seourls == 1) { $scrubsubname = generate_seo_link($subrow['name']); }
			
			if ($seourls == 1) { // With SEO URLS
				$catboxlist = "<li><a href=\"".$siteurl."/category/".$subrow['id']."/"
							.$scrubsubname."/\">".$subrow['name']."</a></li>";
				$catboxoutput .= $catboxlist;
			} else {
				$catboxlist = "<li><a href=\"".$siteurl."/category.php?id=".$subrow['id']."\">"
							.$subrow['name']."</a></li>";
				$catboxoutput .= $catboxlist;			
			}
		$categorytemp->set("catboxbottom",  '</ul><br style="clear:both"/></div>');
		$categorytemp->set("catboxlist", $catboxoutput);
		}	
	}
	

// Checks if category is empty
	if ($num_results == 0) { 
	//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>";
				}
			}

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

			} else {
				$thisrow = "<h2><a href=\"".$siteurl."/article.php?id=".$id."\">".$title."</a></h2>"; 
				$thisrow .= $body;
				$thisrow .= "<div class=\"article-meta\">".$displaycat." | 
							By: <a href=\"".$siteurl."/profile.php?a=".$authorid."\"><b>"
							.$authorname."</b></a> (".$artdate.")</div><br/>";
			}
		
		// add this row to the list of current rows
		$allrows .= $thisrow;

		} // closing the article loop
			
	} 


	$categorytemp->set("articlerows", $allrows);


//display pagination
		$categorytemp->set("paginationright", '<span style="float:right; margin: 10px 10px 2px 0;">
					<b>Page Navigation: '.$pages->display_pages().'</b></span>');


// Outputs the homepage template!
	 
	echo $categorytemp->output();

	include('rightsidebar.php');
	include('obinclude.php'); 
	
}

?>
Return current item: ArticleSetup