Forum Moderators: open

Message Too Old, No Replies

MySQL List Groups Under Headings

         

firefox15

10:25 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



Hi Everybody,

I'm having a issue that's probably easily answered, but I can't seem to resolve it. For simplicity, let's say I have one table, "Employees." In this table I have two columns: "Name" and "Title." In this company, the only titles are "Support 1," "Support 2," and "Support 3." There are approx. two employees in each support group.

What I would like to do is display the "Title" field with an HTML heading and list all the employees under that heading that match the title. I'd like the same for all the titles in the database, so I'd like it to look something like this:

<h1>Support 1</h1>
Employee 1
Employee 2
<h1>Support 2</h1>
Employee 3
Employee 4
Employee 5
<h1>Support 3</h1>
Employee 6

I can't seem to figure out how to do this. I can display the support levels on a separate page and list the applicable employees on the next page, but I'd like to list them all on one page. Short of writing separate queries with some crazy PHP include stuff, I'm at a loss. I'd be very thankful for any assistance.

physics

1:46 am on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know about crazy PHP stuff. Seems to me you could just do
SELECT Title, Name FROM Employees ORDER BY Title

$title = "";
Then loop over the results.

If the current title is equal to $title then do not print a title header.

If it is not then print <h1>(TITLE INFO)</h1> and set $title equal to the title info.

Print the name of the employee.

Repeat.

firefox15

3:22 pm on Jun 15, 2007 (gmt 0)

10+ Year Member



That makes sense. I'll give it a shot. Thanks for your help.

firefox15

7:52 pm on Jun 15, 2007 (gmt 0)

10+ Year Member



It worked great. Thanks a lot!