<?php
require_once("constants.php");
require_once("student.php");
?>

<html>
<head>
<title>My Students</title>
</head>
<body>
<h1>Students</h1>
<p>Click a column to sort the table accordingly.</p>

<?php

//connect to the DB server
if(($sql = mysql_connect('localhost', DB_USER)) === FALSE)
{
	echo "Error connecting to database.";
	exit;
}

//select the database
if(mysql_select_db(DB_NAME, $sql) === FALSE)
{
	echo "Error selecting database.";
	exit;
}

//how do we want to sort it
if(isset($_GET['sort']))
	$sort = "ORDER BY `".$_GET['sort']."` ASC";
else
	$sort = "";

//execute the query
$result = mysql_query("SELECT * FROM students ".$sort);

echo '<table border="1" cellpadding="10px">	
		<tr>
			<th><a href="list.php?sort=Name">Name</a></th>
			<th><a href="list.php?sort=ID">ID Number</a></th>
			<th><a href="list.php?sort=Phone">Phone</a></th>
			<th><a href="list.php?sort=GPA">GPA</a></th>
			<th>View/Edit</th>
			<th>Remove</th>
		</tr>';	
		
while($row = mysql_fetch_array($result))
{ ?>
	<tr>
		<td><?echo $row[0];?></td>
		<td><?echo $row[1];?></td>
		<td><?echo $row[2];?></td>
		<td><? if($row[4] == -1)
					echo "N/A";
				else
					echo $row[4];?></td>
		<td><a href="edit.php?id=<?echo $row[1];?>">Edit</a></td>
		<td><a href="remove.php?id=<?echo $row[1];?>">Remove</a></td>
	</tr>	

<?php } ?>
</table>
<br />
<hr />
<p><a href="index.php">Add a Student</a></p>
</body>
</html>
