tags:

views:

175

answers:

3

Hello guys

I have a table in DB that has messages exchanged between groups and I need to create a table like this one (messages exchanged between 2 groups):

Table Matrix with Group names and message count between Groups names

knowing that Gr1,.... are group names from DB and numbers are from DB too.

Edited to add sQL query (given by Sarah in comments -below):

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

A: 

Here, without bells & whistles. Replace "CELL" with your contents:

<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>
Tom
A: 

Put the results of your query in an array like

$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'];

Then iterate over the array, to generate the header of the table like

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

Then iterate again to build fields:

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

Finally finish table of course.

The example quickly put together to give an idea how to proceed and probably contains errors... And can be written a lot better for sure!

Veger
If you allow me i might add the code with corrections.
Sarah
That's not problem for me!
Veger
Hey Veger i just want to ask what can i do if i want to add the total of messages to each row. I mean for each group.
Sarah
Well, you can use a `foreach` loop to iterate through all groups. In the second loop you can sum all `$cell_value` and print that directly after the second iteration. *Note*: If it is still unclear, formulate (and create) a new question, especially since it has been months since you asked this question. So everyone is able to help you again (since I suppose there are currently not much viewers for this question anymore)
Veger
OK i will post a new question thanks for the reply.
Sarah
A: 

If your problem is about HTML, you can should use "colspan" attribute. I did an example here:

        <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>

You can stylize more using CSS, but this is part two. I hope that I helped your. Good luck

stefanz