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

session_start();

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

if (!isset($_SESSION['loggedin'])) 
{
  $_SESSION['loggedin'] = '0'; 
}
?>


<html>

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

<div id="aside">
<?php include 'filter.php'; ?>

	<br><fieldset style="width:85%"><legend>HELP</legend>
	<p>All of our products are listed here.</p>
	<p>Scroll through the pages at the bottom of the previous and next links, 
	or you can use the filters at the top.</p>
	</fieldset><br>
	
</div>

<div id="content">
<br>
<fieldset style="width:95%"><legend>LIST OF ITEMS</legend>

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

//pagination function control
$pagination_switch = 0;

//items per page
$rec_limit = 12;


//if any variable arrived from filter module
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	$OrderBy = test_input($_POST["OrderBy"]);
	$Category = test_input($_POST["Category"]);
	$Brand = test_input($_POST["Brand"]);
	$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"]);  
    
	$pagination_switch = 1;
}

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

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

mysql_select_db("university", $con);

/* Get total number of items */
$sql = "SELECT count(ItemID) FROM natty ";
$retval = mysql_query( $sql, $con );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

//how many pages have
$last_page = ceil($rec_count / $rec_limit);

if( isset($_GET{'page'} ) )
{
   $page = $_GET{'page'} + 1;
   $offset = $rec_limit * $page ;
}
else
{
   $page = 0;
   $offset = 0;
}

//count left items
$left_rec = $rec_count - ($page * $rec_limit);


//query string
$sql = "SELECT * FROM natty 
	WHERE ItemAvailable=1 AND 
	ItemCategory LIKE '%$Category%' 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";

if ($pagination_switch == 0)
	{
		$sql .= " LIMIT $offset, $rec_limit";
	}


$retval = mysql_query( $sql, $con );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}


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

$i=1;

//loop for sql result
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{


//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>";

//variable to item numbers in a row
if ($i % 4 == 0)
{
echo "</tr><tr bgcolor='#e1e0e0'>";
}

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

//Previous and next page links
if 	($pagination_switch == 0)
{

	if( $page > 0 )
	{
		$last = $page - 2;
		echo "<a href=\"product-list.php?page=$last\">Previous page </a>";
		if ($page < $last_page-1)
		{
			echo "<a style='float: right;' href=\"product-list.php?page=$page\">Next page </a>";
		}
	}
	else if( $page == 0 )
	{
		echo "<a style='float: right;' href=\"product-list.php?page=$page\">Next page </a>";
	}
	else if( $left_rec < $rec_limit )
	{
		$last = $page - 2;
		echo "<a href=\"product-list.php?page=$last\">Previous page </a>";
	}
}

//close sql connection
mysql_close($con);

?>
</fieldset>
</div>

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

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