簡體   English   中英

創建一個像這樣的HTML表

[英]Create an HTML table like this one

我前不久發布了一個問題,我得到了答案,但是現在我有點想要解決這個問題,所以我有一張像圖中的表格,但是我想將總計添加到每一行或實際上是一組我做到了 這是創建該表的php代碼,如何對其進行添加。

$sql = mysql_query(
  "SELECT s.login AS 'from', r.login AS 'to',COUNT(*) as'message_count'
  FROM messages AS m,groups AS s,groups AS r
  WHERE m.Group_ID = s.id
  AND m.To_Group_ID = r.id
  AND m.Simulation_ID = ".$_SESSION['sim_id']."
  AND s.kind_of_user NOT IN (3,1)
  AND r.kind_of_user NOT IN (3,1)
  AND m.Group_ID IN
    (SELECT Group_ID FROM simulationgroups
       WHERE Simulation_ID = ".$_SESSION['sim_id'].")
  AND m.To_Group_ID IN
    (SELECT Group_ID FROM simulationgroups
       WHERE Simulation_ID = ".$_SESSION['sim_id'].")
  GROUP BY m.Group_ID, m.To_Group_ID
  ORDER BY s.id DESC"
  ) or die (mysql_error());

$data = array();
while($row = mysql_fetch_assoc($sql)) 
{ 
   $data[$row['to']][$row['from']] += $row['message_count'];   
}

 // Print headers
$columns = array_keys($data);
echo "<table class='msg_dynamics' cellspacing=0
  cellpadding=0 border=1px><tr><th><<</th>";

foreach($columns as $_column) 
{
  echo "<th width='10%'>{$_column}</th>";
}

echo "<th width='10%'>total</th>";
echo "</tr>";

// Print data
foreach($data as $_row_name => $_row_data) 
{
  // Add the dash (-) for empty cells
  $_row_data[$_row_name] = '-';

  echo "<tr><td width='10%'>{$_row_name}";
  foreach($columns as $_col_name) 
  {
    echo "<td>{$_row_data[$_col_name]}</td>";
  }

  echo "</td></tr>";
}

echo "<tr><td><b>total</b></td>";

“;

截圖

您必須實現一個總計數器,例如:

// Print data
foreach($data as $_row_name => $_row_data) {
  // Add the dash (-) for empty cells
  $_row_data[$_row_name] = '-';

  $total_row = 0;  //initialize 

  echo "<tr><td width='10%'>{$_row_name}</td>";
  foreach($columns as $_col_name) {
    echo "<td>{$_row_data[$_col_name]}</td>";
    if ($_row_data[$_col_name] != '-') {
      $total_row += $_row_data[$_col_name];
      $total_col[$_col_name] += $_row_data[$_col_name];
    }
  }

  echo "<td>{$total_row}</td>";

  echo "</tr>";
}

echo "<tr><td><b>total</b></td>";
foreach ($total_col as $name => $val) {
  echo "<td>$val</td>";
}

暫無
暫無

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

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