
<?php 
session_start();
 if(!isset($_SESSION['email']))
  { $_SESSION['email'] = $_POST['email']; }
if(!isset($_SESSION['password'])) 
{ $_SESSION['password'] = $_POST['password']; } ?>
<html>
<head>
<title>The Science Network</title>

<link rel="stylesheet" type="text/css" href="style.css" />
</head>
	<body style="background-color:#E8E8E8">

    	<center>
    	<table width="768" border="0" style="background-color:#666666;">
  <tr>
    <td colspan="2"><img src="./logo2.gif" width="763" height="108" alt="logo"></td>
    </tr>
   <tr>
    <td colspan="2" valign="middle" style="background-color:#666;">
    	&nbsp;&nbsp;&nbsp;<a href="./profile.php">Home</a>&nbsp;&nbsp;&nbsp;&nbsp;
	    <a href="./myfriends.php">Friends</a>&nbsp;&nbsp;&nbsp;&nbsp;
	    <a href="./mygroups.php">Groups</a>&nbsp;&nbsp;&nbsp;&nbsp;
	    <a href="./mail.php">Mail</a>&nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="./logout.php">Log Out</a>
    </td>
    </tr>
   <tr>
    <td width="585" style="background-color:#FFF; vertical-align:top; text-align:center">		        <br />
   	<?php
		

$db = mysql_connect("localhost","root","mysql");           /* start connection with database */

echo "<br>";
if (!$db)                                                       /* conditional statement */
{
die('Connection to Database down: '. mysql_error());            /* print if connection is not established */
}

mysql_select_db("ScientistNet", $db);                            /*select database */

if (!(isset($pagenum)))
{
$pagenum = 1;
}                         //checking the number of page

$email =$_SESSION['email']; 
//$email = 'mm@hot.com';// change to session  $_SESSION['email']

$sql =mysql_query("SELECT timestamp, subject ,name ,m.id as mailid , opened
FROM Mail m , Scientist s
WHERE s.id = m.recipient 
AND s.email='$email'
ORDER BY timestamp") 
                     or die(mysql_error());


$page_rows=4;

$rows = mysql_num_rows($sql);

$last = ceil($rows/4);
                                 

if ($pagenum < 1)


{
$pagenum = 1;
}


elseif ($pagenum > $last)
{
$pagenum = $last;
}


$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

// get inbox info
$sql_p = $sql =mysql_query("SELECT timestamp, subject , name , m.id as mailid , opened
FROM Mail m , Scientist s
WHERE s.id = m.recipient 
AND s.email='$email'
ORDER BY timestamp") 
                     or die(mysql_error());
					  
                  
?>




<?php

// show header
echo "<br>";
	echo "<table border='1'>                          
	<tr>
        <th>From</th>
        <th>Subject</th>
        <th>Date</th>
	    <th>Opened</th>	
		<th>Read</th>

		
	
	</tr>";



while($info = mysql_fetch_array( $sql))
                                               // display mail result in table 

{


echo "<tr><form action='message.php' method='POST'>";

echo "<td>";

Print $info['name'];

echo "</td>";
 
echo "<td>";

Print $info['subject'];

echo "</td>";

echo "<td>";

Print $info['timestamp'];

echo "</td>";

echo "<td>";

if( $info['opened'] == 'T')  // change the staus when the message is read
$opened = 'yes';
else
$opened = 'no';
Print $opened;

echo "</td>";

$mail = $info['mailid'];
echo "<input type='hidden' id='mail' name='mail' value='$mail' />";

echo "<td>";
echo "<button type='submit' />";
echo "</td>";

echo "</form></tr>";



}


echo "</tbody>";

echo "</table>";



echo " --Page $pagenum of $last-- <p>";
                                                 /* -nivagation option */

if ($pagenum == 1) {
}
 else 
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";


echo " ";


$previous = $pagenum-1;


echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}           /* display previous page */

 if ($pagenum == $last) {
} 

else {


$next = $pagenum+1;


echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";                    /* display next page */
echo " ";

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";                   /* display last page */



} 
mysql_close($db);
?>
    </td>
    <td width="173" align="center" valign="middle" class="sidebar" style="background-color:#CCC"><br />
	
	<a href="./newemail.php">Compose</a><br /><br />
	

    </td>
  </tr>
  <tr>
    <td colspan="2" align="center" valign="middle" style="background-color:#666; color:#FFF;">The Science Network, 2012&copy;</td>
    </tr>
</table>

	</body>
</html>
