2016-09-27 93 views
-1

我有一個看起來像這樣的索引網頁上即可顯示打開數據庫字段放入頭

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

Mark  [email protected]  I.T   8438   9393829239 

有沒有辦法讓他們知道他們的部門歸入一個數據庫?是這樣的:我已經嘗試它,它要麼不顯示任何內容或犯規類的話

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

Mark  [email protected]  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>  
+0

您想要給定部門的所有聯繫人嗎? –

+0

我希望聯繫人顯示在他們所在的部門中作爲標題。 –

+0

與h1,h2,h3元素一樣嗎? –

回答

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 } ?> 
+0

讓我試試快速 –

+1

這只是給每個部門一個記錄與其他列的不可預知的內容。你應該使用「ORDER BY」。 – PaulF

+0

我複製了您的代碼字的先生,但是當我添加一個新的聯繫人。它給了我一個錯誤,說數據庫表找到0而不是1. –