4

I have a problem formatting a table. I got, from a query, a set of rows similar to this one:

5   23   d
5   23   d
5   23   t
5   24   d
5   24   t
4   23   d
4   23   t

Now, I want to display them in a table, but grouping similar data, something like this:

         d
5   23   d
         t
    24   d
         t
4   23   d
         t

So the <td> for 5 would be <td rowspan='5'> or the one for the first 23 would be <td rowspan='3'>. How can I achieve this?

I thought about making a multidimensional array storing a number for each rowspan, but the original set of rows is longer, so I got lost somewhere, and I guess there's a much easier solution.

1
  • Just the multidimentional array, and I had some ideas, but didn't try them because they wouldn't work (like checking if the last row was repeated, if so, add 1 to the rowspan... but that would be done after echoing the rowspan, so...) Commented Dec 13, 2012 at 22:20

1 Answer 1

4

You can group the query result by column values. For example:

$grouped=array();
foreach($rows as $r)
  $grouped[ $r[0] ][ $r[1] ][]=$r[2];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.