<?php
include("connect.php");
include("search.php");

$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>Book Search Results</h3>";

echo "<p>Total books found: ".$total_records."</p>";

$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 rowspan='7' width='20%' align='center'>";
	echo "<img src=/bookstore/bookstaff/".$row["itemImage"].">";
	echo "</td>";	
	echo "<td colspan='2'><h3>".$row["itemName"]."</h3></td>";
	echo "<td rowspan='7' valign='bottom'><a href='bookviewpage.php?ISBN=".$row["ISBN"]."'>View Details</a><br/><a href='cart.php'>Add To Cart</a></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td width='20%'>ISBN:</td>";
	echo "<td width='40%'>".$row["ISBN"]."</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Author:</td>";
	echo "<td>".$row["itemAuthor"]."</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Publisher:</td>";
	echo "<td>".$row["itemPublisher"]."</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Issue Date:</td>";
	echo "<td>".$row["itemDate"]."</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Category:</td>";
	echo "<td>".$row["itemCategory"]."</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Price:</td>";
	echo "<td>RM";
	printf("%10.2f",$row["itemPrice"]);
	echo "</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='bookmenu.php?Pages=".($pages-1)."'>Previous</a>|";
}

for($k=1; $k<=$total_pages; $k++){
	if($k!=$pages){
	echo "<a href=\"bookmenu.php?Pages=".$k."\">".$k."</a>";
	}else
	{
	echo $k."";
	}
}


if($pages<$total_pages){
echo "|<a href='bookmenu.php?Pages=".($pages+1)."'>Next</a>";
}
echo "</center>";
$db->close();

?>