将数据库字段转换为标题

时间:2016-09-27 12:32:28

标签: php html mysql

我有一个数据库,当它显示

时会在索引页面上显示如下内容
name----------email-----department-----extension-----cellphone

Mark        ddsad@dsad     I.T           8438          9393829239

有没有办法让他们归入他们所在的部门?像这样:我尝试了它,它既没有显示任何内容,也没有任何组合

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

Mark        ddsad@dsad     8438          9393829239

我的索引代码如下:

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

1 个答案:

答案 0 :(得分:0)

您可以在执行查询时尝试按部门排序,然后只需验证以前的部门是否与当前部门不同:

$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 } ?>