<?php
session_start();
$username = $_SESSION['staff'];

include ("connect.php");

$username = $_SESSION['staff'];
if($_GET["sort"]!=""){
$sort = $_GET["sort"];
}else{
$sort = num;
}
if($_SESSION['staff']!= null)
{
?>
	
<html>
<head>
<title>Daily Bookstore - Management System</title>
<!--include css file-->
<link href="bookstaff.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" type="text/css" href="paginate.css">
</head>

<body>

<?php
include ("stafffunctionbar.php");
?>

<center>
<h2>Daily Bookstore Management - Gift List</h2>
</center>

<div id="mainpage">
<?php
include("bookfunction.php")
?>
</fieldset>
</td>

<td width="75%">
<?php
include ("connect.php");
$result = mysql_query("SELECT * FROM gift");   

$total_records = mysql_num_rows($result);
$records_per_page = 10;
$page   = intval($_GET['page']);

$adjacents  = intval($_GET['adjacents']);
if($adjacents<=0) $adjacents = 4;

if($page<=0)  $page  = 1;
//$tpages = ($_GET['tpages']) ? intval($_GET['tpages']) : 20; // 20 by default

if($total_records){
   if($total_records<$records_per_page){
   $tpages = 1;
   }
   else if($total_records%$records_per_page){
   $tpages = (int)($total_records/$records_per_page) + 1;
   }else{
   $tpages = $total_records/$records_per_page;                     
   }
}
else{
   $tpages = 0;
}

//if(isset($_GET["Pages"])){
//	$page = intval($_GET["Pages"]);
//	}else{
//	$page = 1;
//}

$offset = ($page - 1)*$records_per_page;
@mysql_data_seek($result, $offset);

echo "<div id='mainpage'>";
echo "<div>";

echo "<table border='1' cellspacing='0' cellpadding='5' bordercolor='white' align='center' width='90%'>";

echo "<tr>";
echo "<td colspan='6'><a href='addgift.php'><img src='Picture/add.gif' width='30' height='30' alt='add gift'>Add Gift</a></td>";
echo "</tr>";

echo "<tr bgcolor='gray' style='color:lightblue'>";
echo "<td width='3'>No.</td>";
echo "<td width='400'>Gift</td>";
echo "<td width='100'>Quantity</td>";
echo "<td width='100'>Redeem Point</td>";
echo "<td>Edit</td>";
echo "<td>View</td>";
echo "</tr>";

$num_results = mysql_num_rows($result);
if($num_results!=0){
for($i=0;$i<$num_results;$i++){
	$row = mysql_fetch_array($result);
	echo "<tr>";
	echo "<td>".($i + 1)."</td>";
	echo "<td>".$row["giftname"]."</td>";
	echo "<td>".$row["giftquantity"]."</td>";
	echo "<td>".$row["redeempoint"]."</td>";
	echo "<td><a href='editgift.php?giftname=".$row["giftname"]."'>Edit</a></td>";
	echo "<td><a href='viewgift.php?giftname=".$row["giftname"]."'>View</a></td>";
	echo "</tr>";
}
}else{
echo "<td colspan='8' align='center'>No Gift Data Here.</td>";
}

echo "</table>";
echo "</div>";

echo "<hr/>";
if(($keyword=="")||($searchBy=="")){
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
}else{
$reload = $_SERVER['PHP_SELF'] . "?keyword=" . $keyword . "&searchBy=" . $searchBy . "&search=Search&tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
}

function paginate_three($reload, $page, $tpages, $adjacents) {
	
	$prevlabel = "&lsaquo; Prev";
	$nextlabel = "Next &rsaquo;";
	
	$out = "<div class=\"pagin\">\n";
	
	// previous
	if($page==1) {
		$out.= "<span>" . $prevlabel . "</span>\n";
	}
	elseif($page==2) {
		$out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
	}
	else {
		$out.= "<a href=\"" . $reload . "&amp;page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
	}
	
	// first
	if($page>($adjacents+1)) {
		$out.= "<a href=\"" . $reload . "\">1</a>\n";
	}
	
	// interval
	if($page>($adjacents+2)) {
		$out.= "...\n";
	}
	
	// pages
	$pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
	$pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
	for($i=$pmin; $i<=$pmax; $i++) {
		if($i==$page) {
			$out.= "<span class=\"current\">" . $i . "</span>\n";
		}
		elseif($i==1) {
			$out.= "<a href=\"" . $reload . "\">" . $i . "</a>\n";
		}
		else {
			$out.= "<a href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>\n";
		}
	}
	
	// interval
	if($page<($tpages-$adjacents-1)) {
		$out.= "...\n";
	}
	
	// last
	if($page<($tpages-$adjacents)) {
		$out.= "<a href=\"" . $reload . "&amp;page=" . $tpages . "\">" . $tpages . "</a>\n";
	}
	
	// next
	if($page<$tpages) {
		$out.= "<a href=\"" . $reload . "&amp;page=" . ($page+1) . "\">" . $nextlabel . "</a>\n";
	}
	else {
		$out.= "<span>" . $nextlabel . "</span>\n";
	}
	
	$out.= "</div>";
	
	return $out;
}
echo paginate_three($reload, $page, $tpages, $adjacents);

echo "</center>";

$db = mysql_close();
?>

</td>
</tr>
</table>
<center>
<div id="bottom">
<br/>
Copyright &#169; with Tian Tian Bookstore 2011. All Rights Reserved.
<br/>
Website:<a href="../bookstore" target="_blank">www.dailybook.com.my
</div>
</center>
</body>
</html>
<?php
}else{
	echo "<script language='javascript'>alert ('Please login.')</script>";
	echo "<script language='javascript'>window.location='index.php'</script>";
}
?>