<?php 
$cathead = 0;
$title = "Blog Andrew Article Directory Homepage";

require_once('paginator.php');

include('config.php');
	
include('header.php'); 

include('sidebar.php'); 
?>


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


<?php
	$search = trim($_GET['s']);

echo "<h1>Search Results for '".$search."'</h1>";	
			
		//for pagination
	$rowsquery = "select * from articles where title like '%".$search."%' or body like '%".$search."%'";
	$rowsresults = mysql_query($rowsquery,$connection) or die(mysql_error());
	$rows_results = mysql_num_rows($rowsresults);
	
	$pages = new Paginator;
	$pages->urlparam = "?search=".$search;
	$pages->items_total = $rows_results;
	$pages->mid_range = 9;
	$pages->paginate();


	if ($pages->items_total) {	
		$query = "select * from articles where title like '%".$search."%' or body like '%".$search."%' order by date desc ".$pages->limit; 
	} else {
		$query = "select * from articles where title like '%".$search."%' or body like '%".$search."%' order by date desc"; 
	}
	
	$articleresults = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($articleresults);

		
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 = stripslashes($row['title']);
	if ($seourls == 1) { $scrubtitle = generate_seo_link($title); }
	$body = strip_tags(substr(stripslashes($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'); ?>
