<?php
$title = "APP Products Listing";
include "conn.php";
include "header.php";
include "leftnav.php";
?>
<td valign="top" class="border">
<br>
  <p class="heading"><a href="index.php"><font color="#669900">Home</font></a> 
  <?php
  $catid = $_GET["catid"];
  whereami($catid);
  ?>
    <br>
    <br>
    <img src="img/productsbutt.jpg" width="367" height="21"> <br>
    </p>

<!-- Dated: 13 February 2007 - Added based on request by Lay Wah due to issues with database -->
<!-- Dated: 11 April 2007 - Removed based on request by Lay Wah as database been resolved -->
<!-- <p>Construction in progress.  Sorry for any inconvenience caused.
</p> -->

<!-- remove from here to top of comment once database is resolved -->

<?php
$q = $_GET["q"];
$parentid = $catid;

$empty = 1;

$res = mysql_query("select catname, catdesc from cats where catid='$catid'");
if ($row = mysql_fetch_array($res))
{
	$parent = $row[0];
	$parentdesc = $row[1];
}

if ($q)
{
	$res = mysql_query("select catid, catname, catdesc, catpic from cats where catname like '%$q%' order by catname asc");
	//$res = mysql_query("select catid, catname, catdesc, catpic from cats where catname like '%$q%'");
}
else
{
	if ($parent)
	{
		echo "<table width=95% cellspacing=1 cellpadding=0 border=0>
		<tr><td><b>$parent</b></td></tr>
		<tr><td style=\"line-height: 150%;padding-right: 55px\">$parentdesc</td></tr>
		<tr><td><b>&nbsp;</b></td></tr>
		</table>";
	}
	
	$res = mysql_query("select catid, catname, catdesc, catpic from cats where parentid='$catid' order by catname asc");
	//$res = mysql_query("select catid, catname, catdesc, catpic from cats where parentid='$catid'");
}

if (mysql_num_rows($res))
{
	echo "<table width=95% cellspacing=1 cellpadding=3 border=0>";

	//echo "<tr bgcolor=a0a0a0><th>Category</th><th>Description</th></tr>";
		
	while ($row = mysql_fetch_array($res))
	{
		$catid = $row[0];
		$catname = $row[1];
		$catdesc = $row[2];
		$catpic = $row[3];
		
		if ($q)
		{
			$catname = eregi_replace("$q", "<font color=red>$q</font>", $catname);
			$catdesc = eregi_replace("$q", "<font color=red>$q</font>", $catdesc);
		}
		
		echo "<tr>";
			
		echo "<td width=100% colspan=2>
		<table width=100%>
		";
		if (strlen($catpic)>0 && file_exists("products/$catpic"))
		{
			echo "<tr>
			<td width=100% valign=top nowrap><img src=\"products/$catpic\" border=0></td></tr>";
		}
		echo "<tr><td width=100% valign=top style=\"line-height: 150%; padding-right: 55px\">";
		if (hasub($catid))
		{
			echo "<a href=products.php?catid=$catid title=\"Subcategories and products\"><b>$catname</b></a>";
		}
		else
		{
			echo "<b>$catname</b>";
		}
		if ($catdesc)
		{
			echo "<br>$catdesc";
		}
		
		echo "</td></tr></table></td></tr>";
		echo "<tr><td colspan=2>&nbsp;</td></tr>";
	}
	
	echo "</table>";
	
	$empty = 0;
}


$catid = $_GET["catid"];

if ($q)
{
	$res = mysql_query("select productid, productname, productdesc, productpic from products where productname like '%$q%' order by productname asc");
}
else
{
	$res = mysql_query("select productid, productname, productdesc, productpic from products where catid='$catid' order by productname asc");
}
if (mysql_num_rows($res))
{
	echo "<table width=95% cellspacing=1 cellpadding=3 border=0>";
	//echo "<tr bgcolor=a0a0a0><th>Name</th><th>Description</th></tr>";
		
	while ($row = mysql_fetch_array($res))
	{
		$productid = $row[0];
		
		$productname = $row[1];
		$productdesc = $row[2];
		$productpic = $row[3];
		
		if ($q)
		{
			$productname = eregi_replace("$q", "<font color=red>$q</font>", $productname);
			$productdesc = eregi_replace("$q", "<font color=red>$q</font>", $productdesc);
		}
			
		echo "<tr>";
		echo "<td width=100% colspan=2>
		<table width=100%>
		";	
		if (strlen($productpic)>0 && file_exists("products/$productpic"))
		{
			echo "<tr><td width=100% valign=top nowrap><img src=\"products/$productpic\" border=0></td></tr>";
		}
		echo "<tr>
		      <td valign=top style=\"line-height: 150%; padding-right: 55px\"><b>$productname</b><br>$productdesc</td>
		      </tr></table></td>
		      </tr>";
		      
		echo "<tr><td colspan=2>&nbsp;</td></tr>";
	}
	echo "</table>";
	
	$empty = 0;
}

if ($empty)
{
	//echo "no products found, <a href=\"products.php\">click here</a> to browse the products";
}

echo "</td>";

include "footer.php";

function hasub($catid)
{
	$res = mysql_query("select * from cats where parentid='$catid'");
	if (mysql_num_rows($res)>0)
	{
		return true;
	}
	
	$res = mysql_query("select * from products where catid='$catid'");
	if (mysql_num_rows($res)>0)
	{
		return true;
	}
	
	return false;
}

function whereami($catid)
{
	$res = mysql_query("select catid from cats where catid='$catid'");
	if (mysql_num_rows($res) > 0)
	{
		echo " <font color=669900>></font> <a href=products.php><font color=669900>Products</font></a>";
		
		whichcat($catid);
	}
	else
	{
		echo "<font color=\"#669900\">> Products</font>";
	}
}

function whichcat($catid)
{
	$parentid = 0;
	
	$res = mysql_query("select catname, parentid from cats where catid='$catid'");
	if ($row = mysql_fetch_array($res))
	{
		$catname = $row[0];
		$parentid = $row[1];
	}
	
	if ($parentid > 0)
	{
		whichcat($parentid);
	}
	else
	{
		echo " <font color=669900>> $catname</font>";
	}
}
?>