<?php
//session
session_start();
include ("connect.php");
$username = $_SESSION['staff'];

$searchBy = $_GET["searchBy"];
$keyword = $_GET["keyword"];

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

<script language="Javascript">
	function showPreview(ele)
	{
		document.addbook.imgAvatar.src=ele.value;
	}
</script>
</head>

<body>

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

<center>
<h2>Member Management System - Member List</h2>
</center>

<div id="mainpage">
<table width="90%">
<tr>
<td width="15%" valign="top">
<fieldset style="margin:0 5 0 0">
<legend><img src="Picture/member.gif" width="30" height="30">Member</legend>
<table>
<tr>
<td valign="top">
<a href="memlist.php"><img src="Picture/list.gif" width="30" height="30" alt="member list"><a href="memlist.php">Member List</a><br/>
<br/>
<a href="newmem.php"><img src="Picture/add.gif" width="30" height="30" alt="approve member"><a href="newmem.php">Approve Member</a><br/>
<br/>
</td>
</tr>
</table>
</fieldset>
</td>

<td width="75%">

<?php

include ("connect.php");
$result = mysql_query("SELECT * FROM membership");

$num_results = mysql_num_rows($result);

$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 "<p>Number of Member(s) : ".$num_results."</p>";

echo "<center>";
echo "<fieldset style='width:90%'>";
echo "<div id='bill'>";
echo "<table border='1' cellspacing='0' cellpadding='5' align='center'>";
echo "<tr>";
echo "<th>No.</th>";
echo "<th width='20%'>Login Name</th>";
echo "<th width='25%'>Member Name</th>";
echo "<th>Gender</th>";
echo "<th>Race</th>";
echo "<th width='25%'>Email</th>";
echo "<th>Daily Point</th>";
echo "<th colspan='2' align='center'>Function</th></tr>";

for ($i=0; $i <$num_results; $i++)
{
	$row = mysql_fetch_array($result);
	echo "<tr>";
	echo "<td>".($i+1)."</td>";
	echo "<td>".$row["memberLname"]."</td>";
	echo "<td>".$row["memberName"]."</td>";
	echo "<td>".$row["memberSex"]."</td>";
	echo "<td>".$row["memberRace"]."</td>";
	echo "<td>".$row["memberEmail"]."</td>";
	echo "<td>".$row["memberpoint"]."</td>";

	echo "<td><a href='memview.php?memberLname=".$row["memberLname"]."'>View</td>";
	echo "<td><a href='memberdeleted.php?memLname=".$row["memberLname"]."'>Delete</a></td>";
	echo "</tr>";
	}
	echo "</table>";
	echo "</div>";
	echo "</fieldset>";
	echo "</center>";
	echo "</form>";
	
	echo "<hr/>";
//create page previous button
echo"<center>";
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();

}else{
	echo "<script language='javascript'>alert ('Please login.')</script>";
	echo "<script language='javascript'>window.location='index.php'</script>";
}
?>
</td>
</tr>
</table>
<hr/>

<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>