2010-07-12 190 views
2

我前一段時間發佈了一個問題,我得到了我的答案,但現在我有點想要跟進這個問題,所以我有一個像圖片中的表,但我想增加總數對每一行或實際上一個小組我該怎麼做。 這是創建此表格的php代碼,如何編輯它以添加總數。創建一個這樣的HTML表格

$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>"; 

「;

Screenshot http://www.freeimagehosting.net/uploads/d47281658d.jpg

+0

你缺少圖像:( – Anuraj 2010-07-12 08:19:00

+3

太多面條代碼通過 – Gordon 2010-07-12 08:19:42

+0

其中涉水是圖片????? – Avinash 2010-07-12 08:20:08

回答

1

您必須實現象個計數器:

// 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>"; 
}