<?php
include ("connect.php")
?>

<html>
<head>
<title>test</title>
</head>
<body>
<form name="searchform" method="GET">
Search: <input type="text" size="100" name="keyword" value="Type your keyword here.">

<select name="searchBy">
	<option>Select One
	<option value="itemAuthor">Author
	<option value="itemCategory">Category
	</select>

<input type="submit" name="search" value="Search"><input type="submit" name="reset" value="Reset Result">
</form>
</center>

<?php
if($_GET["search"] == "Search")
{	
	$searchBy = $_GET["searchBy"];
	$keyword = $_GET["keyword"];
	$query = "SELECT * FROM item WHERE ".$searchBy." LIKE '%".$keyword."%' ORDER BY num";
	$result = $db->query($query);
}
else if($_GET["reset"] == "Reset Result")
{
	$query = "SELECT * FROM item ORDER BY num";
	$result = $db->query($query);
}
else
{
	$query = "SELECT * FROM item ORDER BY num";
	$result = $db->query($query);
}
$records_per_page = 2;
if(isset($_GET["Pages"])){
	$pages = $_GET["Pages"];
	}else{
	$pages = 1;
}

$total_records = mysqli_num_rows($result);
$total_pages = ceil($total_records/$records_per_page);
$offset = ($pages - 1)*$records_per_page;
mysqli_data_seek($result, $offset);

echo "<center>";
echo "<h3>Results</h3>";

$j = 1;
do{
for($i=0; $i<$records_per_page; $i++)
{
	$row = $result->fetch_array();
	if($row["itemName"]!=""){
	echo "<table border='1' cellspacing='2' cellpadding='1' bordercolor='black' width='70%' align='center'>";
	echo "<tr>";
	echo "<td colspan='2'><h3>".$row["itemName"]."</h3></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Author:</td>";
	echo "<td>".$row["itemAuthor"]."</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Category:</td>";
	echo "<td>".$row["itemCategory"]."</td>";
	echo "</tr>";
	echo "</table>";}
	}
$j++;
}while($rows = mysqli_fetch_array($result, MYSQLI_NUM)and($j<$records_per_page));

echo "<hr/>";

if($pages>1){
echo "<a href='test.php?Pages=".($pages-1)."'>Previous</a>|";
}

for($k=1; $k<=$total_pages; $k++){
	if($k!=$pages){
	echo "<a href=\"test.php?Pages=".$k."\">".$k."</a>";
	}else
	{
	echo $k."";
	}
}
if($pages<$total_pages){
echo "|<a href='test.php?Pages=".($pages+1)."'>Next</a>";
}
echo "</center>";
$db->close();
?>
</body>
</html>