<?Php
/*
* @author Istvan Franko
* @copyright 2014
*/

session_start();

if (!isset($_SESSION['cart'])) 
{
  $_SESSION['cart']=array(); 
}

//get the value from the address bar
if(isset($_GET['cat']))
{
$cat = $_GET['cat'];
$catUpper = strtoupper($cat);
} 


//define variables 
$OrderBy = "ItemID";
$Brand = "%";
$Software = "%";
$Cpu = "%";
$Mem = "%";
$Hdd = "%";
$Screen = "%";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  $OrderBy = test_input($_POST["OrderBy"]);
  $Brand = test_input($_POST["Brand"]);
  $cat = test_input($_POST["cat"]);
  $Software = test_input($_POST["Software"]);
  $Cpu = test_input($_POST["Cpu"]);
  $Mem = test_input($_POST["Mem"]);
  $Hdd = test_input($_POST["Hdd"]);
  $Screen = test_input($_POST["Screen"]); 

  $catUpper = strtoupper($cat);  
}

function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

?>


<html>

<?php include 'head.php'; ?>

<div id="aside">
	<?php include 'filter-category.php'; 
	
	//loops for show helps modules on the matching categories
	if ($cat == 'Laptop')
	{?>
		<br><fieldset style="width:85%"><legend>HELP</legend>
		<p>The items found in this category, their sizes and performance allows 
		to use programs that would normally run on desktops.</p> 
		<p>The filter above allows to specify these parameters.</p>
		</fieldset><br>
	<?php 
	} 
	
	if ($cat == 'Netbook')
	{?>
		<br><fieldset style="width:85%"><legend>HELP</legend>
		<p>All the small, portable computers are listed here 
		up to the screen size of 12 inch. </p>
		<p>The filter is still present to help you refine your needs.</p>
		</fieldset><br>
	<?php 
	} 
	
	if ($cat == 'Tablet')
	{?>
		<br><fieldset style="width:85%"><legend>HELP</legend>
		<p>The very popular tablets are also available, that are perfectly capable of using internet 
		apps, while some that have more powerful graphics can also play games without any problems.</p>
		</fieldset><br>
	<?php 
	} 
	
	if ($cat == 'E-reader')
	{?>
		<br><fieldset style="width:85%"><legend>HELP</legend>
		<p>The lovers of reading can find here today's requirement fulfilling electronic book readers.</p> 
		<p>They specialise in amazingly high battery life due to the e-ink screens that are used, 
		they can even run for a month on stand-by.</p>
		</fieldset><br>
	<?php } ?>	
	
</div>

<div id="content">
<br>
<fieldset style="width:95%"><legend><?php echo $catUpper . "S"; ?></legend>

<?php

//sql connection
$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("university", $con);

//sql query
$result = mysql_query("
SELECT * FROM natty 
WHERE ItemAvailable=1 AND 
ItemCategory LIKE '%$cat%' AND
ItemBrand LIKE '%$Brand%' AND
ItemOpSys LIKE '%$Software%' AND
ItemCpu LIKE '%$Cpu%' AND
ItemMem LIKE '%$Mem%' AND
ItemHdd LIKE '%$Hdd%' AND
ItemScreenSize LIKE '%$Screen%' 
ORDER BY $OrderBy");

echo "<table align='center'  cellspacing='10' cellpadding='5'>";
echo "<tr bgcolor='#e1e0e0'>";

$i=1;
while ($row = mysql_fetch_array($result))
{


//item thumbnail and special offer stamp
if ($row['ItemSpecialOffer'] != '0')
{ 
echo "<td width='150' class=border background='images/special_offer.png' ";
echo "style='background-repeat:no-repeat; background-position:left top;' align='center'><br>";
}
else
{
echo "<td width='150' class=border align='center'>";
}
echo "<a href=\"images/";
echo $row['ItemPicture1'];
echo "\">";
echo "<img width='100' src=\"images/";
echo $row['ItemPicture1'];
echo "\" >";
echo "</a>";
echo "</p>";

//Item price
echo "<b><span class='red'>&#163;";
echo $row['ItemPrice'];
echo "</span></b>";

//item title
echo "<br>";
echo "<a href=\"details.php?id=";
echo $row['ItemID'];
echo "\">";
echo $row['ItemName'];
echo "</a><br>";


//Item CPU and MEM
echo "<span class='blue'><br>";
echo $row['ItemCpu'];
echo "<br>";
echo $row['ItemMem'];
echo " RAM";
echo "</span>";

//Details button
echo "<br><a href=\"details.php?id=";
echo $row['ItemID'];
echo "\">";
echo "<img width='66' src=\"images/Details-button.PNG";
echo "\" >";
echo "</a>";

if ($i % 4 == 0)
{
echo "</tr><tr bgcolor='#e1e0e0'>";
}

$i++;
}
echo "</table>";

//sql connection close
mysql_close($con);

?>
</fieldset>
</div>

<div id="footer">
<p align="center">Copyright &#169; Istvan Franko, 2014</p>
</div>
	

</div>
</body>
</html>