2017-02-13 50 views
3

我的php表格顯示在我的頁面上,從一個mysql數據庫。它們顯示在什麼部門他們是爲了我的字段標題未顯示在我的php表中

目前,它看起來像這樣:

      Vehicle Department 
       Bob  3234234  [email protected] 
       Hanna  3434323  [email protected] 
         Workshop Department 
       Andrew  45454523 [email protected] 

但正如你可以看到的名字,電話和電子郵件字段標題沒有顯示。我曾嘗試將它們與數據庫結果一起回顯,但它只是在表格中創建了一個巨大的混亂。

最後的結果我想實現:

     Vehicle Department 
       Name  Phone  Email 
       Bob  3234234  [email protected] 
       Hanna  3434323  [email protected] 
         Workshop Department 
       Name  Phone  Email 
       Andrew  45454523 [email protected] 

所以每個部門將顯示的字段標題的姓名,電話,從MySQL數據庫電子郵件數據。我的代碼可以發現如下:

<?php 
    $db_host = 'localhost'; 
    $db_user = 'root'; 
    $db_pwd = '*****'; 

    $database = 'list'; 
    $table = 'users'; 

    $conn = mysqli_connect($db_host, $db_user, $db_pwd) or die("Connecting to database failed"); 

    mysqli_select_db($conn, $database) or die("Can't select database"); 

    // sending query 
    $result = mysqli_query($conn, "SELECT name, email, extension, phone, department FROM {$table} ORDER BY department, name ASC"); 
    if (!$result) { 
     die("Query to show fields from table failed"); 
    } 

    echo "<table border='1'><tr>"; 

    // printing table rows 
    $temp = ""; 

    while($row = mysqli_fetch_array($result)) { 
     echo "<tr>"; 

     if ($row['department'] != $temp){ 
      echo "<td colspan='4' style='text-align: center; font-weight: bold'>{$row['department']}</td></tr>\n<tr>"; 
      $temp = $row['department']; 
     } 
    echo "<td>" . $row['name'] . "</td><td>" . $row['email'] . "</td><td>" . $row['extension'] . "</td><td>" . $row['phone'] . "</td>"; 

    echo "</tr>\n"; 
    } 
    mysqli_free_result($result); 
    echo "</table>" 
?> 

回答

1

希望這有助於

表頭將顯示下面的部門名稱

<?php 
     $db_host = 'localhost'; 
     $db_user = 'root'; 
     $db_pwd = '*****'; 

     $database = 'list'; 
     $table = 'users'; 

     $conn = mysqli_connect($db_host, $db_user, $db_pwd) or die("Connecting to database failed"); 

     mysqli_select_db($conn, $database) or die("Can't select database"); 

     // sending query 
     $result = mysqli_query($conn, "SELECT name, email, extension, phone, department FROM {$table} ORDER BY department, name ASC"); 
     if (!$result) { 
      die("Query to show fields from table failed"); 
     } 

     echo "<table border='1'><tr>"; 

     // printing table rows 
     $temp = ""; 

     while($row = mysqli_fetch_array($result)) { 
      echo "<tr>"; 

      if ($row['department'] != $temp){ 
       echo "<td colspan='4' style='text-align: center; font-weight: bold'>{$row['department']}</td></tr>\n"; 
echo "<tr><th>Name</th> 
<th>Phone</th> 
<th>Email</th> 
</tr>\n"; 
echo "<tr>"; 

       $temp = $row['department']; 
      } 
     echo "<td>" . $row['name'] . "</td><td>" . $row['email'] . "</td><td>" . $row['extension'] . "</td><td>" . $row['phone'] . "</td>"; 

     echo "</tr>\n"; 
     } 
     mysqli_free_result($result); 
     echo "</table>" 
    ?> 
+0

你是主人長!非常感謝你(我實際上已經嘗試過,但把它放在$ temp = row行之後)哈哈做得很好^^ – RedZ

+0

@ J.Hof歡迎......如果你把它放在'$ temp = $ row ['部門']'也......你可能錯過了將''替換成它......好運:) – affaz

0

您可以使用MySQL是mysql_field_name使用此,你將能夠獲得字段名稱的內置功能。 雖然它不贊成在PHP 5.4.0或以上,您可以檢查 http://php.net/manual/en/mysqli-result.fetch-field-direct.php

+0

它'mysqli_field'取代'mysql'請更改it..or他們可能會出問題 – affaz

+0

是的,這是我發送了新功能的鏈接。因爲mysql現在已經被棄用了。 – Abizz

+0

我知道..只是告訴改變你的內容太多.. – affaz

0

當你從數據庫中提取數據,只有實際的數據是從服務器獲取。

因此,如果您需要輸入表格標題,您可以在while循環前插入HTML代碼以插入標題。

<?php 
$db_host = 'localhost'; 
$db_user = 'root'; 
$db_pwd = '*****'; 

$database = 'list'; 
$table = 'users'; 

$conn = mysqli_connect($db_host, $db_user, $db_pwd) or die("Connecting to database failed"); 

mysqli_select_db($conn, $database) or die("Can't select database"); 

// sending query 
$result = mysqli_query($conn, "SELECT name, email, extension, phone, department FROM {$table} ORDER BY department, name ASC"); 
if (!$result) { 
    die("Query to show fields from table failed"); 
} 

echo "<table border='1'><tr>"; 

/******** Add this! *********/ 
echo '<tr> 
<th>Name</th> 
<th>Phone</th> 
<th>Email</th> 
</tr>'; 
/****************************/ 

// printing table rows 
$temp = ""; 

while($row = mysqli_fetch_array($result)) 
{ 
    echo "<tr>"; 

    if ($row['department'] != $temp){ 
     echo "<td colspan='4' style='text-align: center; font-weight: bold'>{$row['department']}</td></tr>\n<tr>"; 
     $temp = $row['department']; 
    } 
echo "<td>" . $row['name'] . "</td><td>" . $row['email'] . "</td><td>" . $row['extension'] . "</td><td>" . $row['phone'] . "</td>"; 

    echo "</tr>\n"; 
} 
mysqli_free_result($result); 
echo "</table>" 
?> 
+0

這可行,但我需要他們出現在每個部門。不只是一次先生 – RedZ