0

So here is the table I get from a query:

----------+----+-----------+----------------------
45678-sm-w| 18 |T-Shirts   | Mens clothing
----------+----+-----------+----------------------
45678-sm-b|  5 |T-Shirts   | Mens clothing
----------+----+-----------+----------------------
2189-L-Wh |  4 | Socks     | Juniors Clothing
----------+----+-----------+----------------------
2189-L-Bl |  3 | Socks     | Juniors Clothing
----------+----+-----+-----+----------------------

I want to put into a report the looks like:

T-Shirts
----------+----+-----------+----------------------
45678-sm-w| 18 |T-Shirts   | Mens clothing
----------+----+-----------+----------------------
45678-sm-b|  5 |T-Shirts   | Mens clothing
----------+----+-----------+----------------------

Socks
 ----------+----+-----------+----------------------
2189-L-Wh |  4 | Socks     | Juniors Clothing
----------+----+-----------+----------------------
2189-L-Bl |  3 | Socks     | Juniors Clothing
----------+----+-----+-----+----------------------

I know it is done through a loop but cannot figure it out. Thanks!

3
  • 2
    Show some code (PHP/SQL.. what you have tried). Commented Oct 12, 2012 at 19:51
  • I dont know where to begin. You dont need to vote down my question. Commented Oct 12, 2012 at 19:57
  • 1
    Once the question shows effort and is edited, I can remove my down vote. Commented Oct 12, 2012 at 20:04

1 Answer 1

4

Try ordering your query by item type and force a header when the current header changes.

$res = mysql_query("select * from clothing order by item_type, item_size");

$item_type=null;
while ($row = mysql_fetch_assoc($res)) {
    if ($item_type != $row['item_type']) {
        $item_type = $row['item_type'];
        echo $item_type . "\r\n";
    }
    echo $row["item_name"];
    echo $row["item_size"];
    echo $row["item_desc"];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much works great. A little format tweaking and it is just what I needed.

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.