Create an HTML table like this one
i posted a question a while ago and i got my answer but now i kind of want a followup to this problem so i have a table like the one in the picture but i want to add total to each row or in fact a group how can i do that. This is the php code to create this table how to edit it to add the total.
$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><<&l开发者_JS百科t;/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>";
";
You have to implement a total counter like :
// 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>";
}
精彩评论