From the looks of it, you can tailor your SQL statement such that you dont need to check $departments for duplicate entries. After that, the actual printing of the department just needs a check to print a space if there is a blank (but not NULL in the database) department (e.g. an empty string).
$departments = array();
$query = "SELECT DISTINCT(department) FROM tablename";
if ($result = mysql_query($query)) {
while ($row = mysql_fetch_assoc($result)) {
$departments[] = $row['department'];
}
}
foreach ($departments as $department) {
echo '<tr><td width="140">';
if ($department !== '') {
echo '<a href="directory_main.php?department=' . rawurlencode($department)
. '" target="mainFrame" class="menulink">' . $department . '</a>';
} else {
echo ' ';
}
echo '</td>';
}
Of course, if you dont actually need the $departments array, you might even do the printing in the same loop that you extract from the result set.