<?php  
  $full_path = $_SERVER['REQUEST_URI'];
  $script = $_SERVER['SCRIPT_NAME'];
  $fixed_path = ereg_replace("^$script","",$full_path);// remove script name from path
  $path_arr = explode("/", $fixed_path);
 
/*
FOR CATEGORIES - "categories" then category name
anyrib.com/articles/categories/php

FOR ARTICLES - article name, then page #
anyrib.com/articles/basicxhtml/2
*/

  $first = $path_arr[1]; // "categories" or article name
  $second = $path_arr[2]; // category name or page number
  $articles_intro = "<p class=\"intro\">Here, you can find articles on web authoring and programming, featuring content for web designers of all levels of experience.</p>";
  include "connect.php";

  if ($first == "")  //     *****      MAIN INDEX     *****
	{
	$title = "WebFu Articles";
	$tagline = "Web Development Articles";
	include "begin.php"; 
	
	echo "\n\n<div id=\"main\" class=\"articles\">";
	
	//Main Index - List all categories
	$result = mysql_query("select distinct article_category from articles") or die(mysql_error());

	echo "\n  <div id=\"side\">\n";
	echo "<div id=\"side_h\"></div>";
	echo "  <h4>Categories</h4>\n";
	echo "  <ul>\n";
	while($categ = mysql_fetch_array($result,MYSQL_NUM)) 
		{
		echo "  <li><a href=\"$script/categories/$categ[0]\">$categ[0]</a></li>\n";
		}
	echo "  </ul>\n";
	echo "  </div> <!-- end side -->\n\n";
	echo "  <div id=\"body_main\">\n";
	//Intro
	echo $articles_intro;
	
	//List articles in paged format
	$result = mysql_query("select distinct article_title, article_url, article_synopsis from articles") or die(mysql_error());
	echo "\n<dl>\n";
	while($article = mysql_fetch_array($result,MYSQL_NUM)) 
		{
		echo "  <dt><a href=\"$script/$article[1]\">$article[0]</a></dt>\n";
		echo "  <dd>$article[2]</dd>\n";
		}
	echo "\n</dl>\n";
	echo "  </div> <!-- end body_main -->\n\n";
	}
  elseif ($first == "categories") // *****     SPECIFIC CATEGORY     *****
	{
	$title = "WebFu Articles - $second";
	$tagline = "Web Development Articles";
	include "begin.php"; 
	echo "\n\n<div id=\"main\" class=\"article_cat\">";
	
	//search db for all articles under this category
	$sql = "select distinct article_title, article_url from articles where article_category = '$second'";
	$result = mysql_query($sql) or die(mysql_error());
	if (mysql_num_rows($result) == 0)
		{echo ("<h4>No articles found under category &quot;$second&quot;</h4>");}
	else
		{
		echo "<h4>$second</h4>";
		//List all articles under this category
		echo "<ul>\n";
		while($article = mysql_fetch_array($result,MYSQL_NUM)) 
			{
			echo "  <li><a href=\"$script/$article[1]\">$article[0]</a></li>\n";
			}
		echo "</ul>\n";
		}
	}
  else //     *****     DISPLAY ARTICLE     *****
	{
	//search for article named $first
	$page = 1;
	if (isset($second)) {$page = $second;} //check page number
	// select title and text from one page
	$sql = "select 
		a.article_title,
		p.page_text,
		p.page_title,
		a.article_id,
		a.article_url
		from articles a, article_pages p
		where a.article_url = '$first' 
		and p.page_num = '$page'
		and p.page_article_id = a.article_id
		";
	$result = mysql_query($sql) or die(mysql_error());
	$row = mysql_fetch_row($result);
	$art_title = $row[0];
	$art_txt = $row[1];
	$pg_title = $row[2];
	$foreign_key = $row[3];
	$art_url = $row[4];
	if (mysql_num_rows($result) == 0)
		{
		$title = "WebFu";
		$tagline = "Error";
		include "begin.php"; 
		echo "\n\n<div id=\"main\">\n";
		echo ("<h4>Error: Page $page of $first not found</h4>");
		}
	else
		{
		/* *******                                              DISPLAY ARTICLE                                              ******* */
		//select all page titles

		$sql = "select page_title, page_num from article_pages where page_article_id = '$foreign_key'";
		$result = mysql_query($sql) or die(mysql_error());
		
		$title = "$art_title - WebFu";
		$tagline = "Web Development Articles";
		include "begin.php"; 
				
		echo "\n\n<div id=\"main\" class=\"single_art\">";
		
		echo "\n<div id=\"side\">\n";
		echo "<div id=\"side_h\"></div>";
		echo "<h5>$art_title</h5>\n";
		echo "<ol>\n";
		while($pg = mysql_fetch_array($result,MYSQL_NUM)) 
			{
			if ($pg[1] == $page) 
				{
				echo "<li>$pg[0]</li>\n";
				$last_page = $pg[1];
				}
			else
				{
				echo "<li><a href=\"$script/$art_url/$pg[1]\">$pg[0]</a></li>\n";
				$last_page = $pg[1];
				}
			}
		echo "</ol>\n";
		echo "</div> <!-- end side --> \n\n";

		//spit out article
		echo "<div id=\"body_main\">\n";
		echo "<h3>$art_title</h3>\n";
		echo "<h4 class=\"page_name\">Page $page</h4>\n";
		echo "<h4>$pg_title</h4>\n";
		echo $art_txt;
		echo "\n</div>\n";
		
		//page navigation
		echo "\n<div id=\"art_pagenav\">\n";
		$prev = $page - 1;
		$next = $page + 1;
		if ($page != "1")
			{
			//Show *prev* nav
			echo "<a rel=\"prev\" href=\"$script/$art_url/$prev\">&laquo; PREV</a>";
			}
		if ($page != $last_page)
			{
			//Show *next* nav
			echo "<a rel=\"next\" href=\"$script/$art_url/$next\">NEXT &raquo;</a>";
			}

		echo "</div> <!-- end art_pagenav -->\n\n";
		}
	}

  echo "\n  </div> <!-- end main -->\n";
  
  include "end.php"; 
  ?>