簡體   English   中英

創建一個像這樣的html表

[英]Create an html table like this one

我在DB中有一個表,該表具有在組之間交換的消息,我需要創建一個像這樣的表(在2個組之間交換消息):

具有組名和組名之間的消息計數的表矩陣http://www.freeimagehosting.net/uploads/10b1fb8eeb.jpg

知道Gr1,....是數據庫的組名,數字也是數據庫的組名。

編輯以添加sQL查詢(由Sarah在以下注釋中給出):

(SELECT COUNT( Msg_ID ) AS msgcount, Group_ID, To_Group_ID FROM messages GROUP BY Group_ID, To_Group_ID)

在這里,沒有鍾聲。 用您的內容替換“ CELL”:

<table>
  <tr>
     <td>CELL</td>
     <td>CELL</td>
     <td>CELL</td>
     <td>CELL</td>
  </tr>
  <tr>
     <td>CELL</td>
     <td>CELL</td>
     <td>CELL</td>
     <td>CELL</td>
  </tr>
  <tr>
     <td>CELL</td>
     <td>CELL</td>
     <td>CELL</td>
     <td>CELL</td>
  </tr>
</table>

將查詢結果放入數組

$query = mysql_query('SELECT from_group, to_group, value FROM table')
while($row = mysql_fetch_array($query))
    $results[$row['from_group']][$row['to_group']] = $row['value'];

然后遍歷數組,以生成表的標題,例如

echo "<table><tr>'"
foreach ($results as $group => $group_array)
  echo "<td>$group</td>";
echo "</tr>";

然后再次迭代以構建字段:

foreach ($results as $from_group => $from_group_array)
{
  echo "<tr><td>$from_group</td>";
  foreach ($from_group_array as $cell_value)
     echo "<td>$cell_value</td>";
  echo "</tr>";
}

最后當然要完成表。

該示例迅速匯總在一起,為您提供了一個如何進行操作的想法,其中可能包含錯誤...並且可以肯定地編寫得更好!

如果您的問題與HTML有關,則可以使用“ colspan”屬性。 我在這里做了一個例子:

        <table border="1px" cellpadding="4">
            <tr>
                <th colspan="2">Global status</th>
                <th>Title here</th>
                <th>Title here</th>
                <th>Title here</th>
                <th>Title here</th>
            </tr>
            <tr>
                <td>text</td>
                <td>text</td>
                <td>text</td>
                <td>text</td>
                <td>text</td>
                <td>text</td>
            </tr>
            <tr>
                <td>text</td>
                <td>text</td>
                <td>text</td>
                <td>text</td>
                <td>text</td>
                <td>text</td>
            </tr>
        </table>

您可以使用CSS設置更多樣式,但這是第二部分。 希望對您有幫助。 祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM