2010-07-12 7 views
2

J'ai posté une question il y a un moment et j'ai eu ma réponse mais maintenant je veux un suivi de ce problème, donc j'ai une table comme celle de l'image, mais je veux ajouter totale à chaque rangée ou en fait un groupe comment puis-je faire cela. Ceci est le code PHP pour créer cette table comment l'éditer pour ajouter le total.Créer un tableau HTML comme celui-ci

$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

Il vous manque l'image :( – Anuraj

+3

trop beaucoup de code spaghetti à patauger dans – Gordon

+0

où est l'image ????? – Avinash

Répondre

1

vous devez implémenter un compteur total comme:

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