将数据库字段转换为标题

I have a database which looks like this on the index page when it displays

name----------email-----department-----extension-----cellphone

Mark        ddsad@dsad     I.T           8438          9393829239

Is there a way to make them grouped under the departments they are in? like this: I have attempted it and it either shows nothing or doesnt group anything

              **I.T**
name----------email-------extension-----cellphone

Mark        ddsad@dsad     8438          9393829239

my index code is as following:

<?php 
require_once"connection.php";

$contacts = array();

$all_contacts = "select * from contacts where contact_status = '1'";


$sql_all_contacts = $conn->query($all_contacts);

$total_contacts = $sql_all_contacts->num_rows;

while ($row = mysqli_fetch_assoc($sql_all_contacts)) {
            $contacts[] = $row;
    }
?>
<!DOCTYPE html>
<html>
<head>
<?php include"includes/head.inc"; ?>

</head>
<body>
<div class="wrapper">

    <!-- header section -->
    <?php include"includes/header.inc"; ?>
    <!-- content section -->
    <div class="content">
        <div class="floatl"><h1><?php echo $total_contacts ?> Contact(s) Total</h1></div>
                    <a class="floatr" href="insert_contact.php"><input class="cancel_contact_button" type="button" value="New Contact"></a>
        <div class="clear"></div>
            <hr class="pageTitle">
            <table border ="1" style="width:100%" id="contactsTable" class="display">
                <thead>
                    <tr align="left">
                        <th>Name:</th>
                        <th>Email:</th>
                        <th>Department:</th>
                        <th>Extension:</th>
                        <th>Cellphone:</th>
                        <th>Actions</th>
                    </tr> 
                </thead>
                <tbody>
                 <?php foreach ($contacts as $contact) {?>
        <tr>
            <td><?php echo $contact["name"];?></td>
            <td><?php echo $contact["email"]; ?></td>
            <td><?php echo $contact["department"]; ?></td>
            <td><?php echo $contact["extension"]; ?></td>
            <td><?php echo $contact["cellphone"]; ?></td>
            <td><a href="update_contact.php?id=<?php echo $contact["contact_id"]; ?>"><i class="fa fa-pencil"></i></a> | <a href="delete_contact.php?id=<?php echo $contact["contact_id"] ?>"><i class="fa fa-trash-o"></i></a></td>
        </tr>
        <?php } ?>
                </tbody>
            </table>
    </div>
</div>  
</body>

</html>     

You can try ordering by department when doing the query and then simply verify if the previous department is different than the current one:

$all_contacts = "select * from contacts where contact_status = '1' order by department";

<thead>
    <tr align="left">
        <th>Name:</th>
        <th>Email:</th>
        <th>Extension:</th>
        <th>Cellphone:</th>
        <th>Actions</th>
    </tr> 
</thead>
<tbody>
    <?php
        $previousDepartment = null; 
        foreach ($contacts as $contact) {
            if ($previousDepartment !== $contact['department']) {
                $previousDepartment = $contact['department'];
    ?>
    <tr>
        <td rowspan="5"><?php echo $contact['department']; ?></td>
    </tr>
    <?php
            }
    ?>
    <tr>
        <td><?php echo $contact["name"];?></td>
        <td><?php echo $contact["email"]; ?></td>
        <td><?php echo $contact["extension"]; ?></td>
        <td><?php echo $contact["cellphone"]; ?></td>
        <td><a href="update_contact.php?id=<?php echo $contact["contact_id"]; ?>"><i class="fa fa-pencil"></i></a> | <a href="delete_contact.php?id=<?php echo $contact["contact_id"] ?>"><i class="fa fa-trash-o"></i></a></td>
    </tr>
    <?php } ?>