well it depends how you want to go about it..

I have done something exactly like this, this morning.. :-p

Firstly.. you can use a normal SQL SELECT statement, but use GROUPing to
seperate the groups in this case and ORDER them alphabetically or
something..

Next.. You can do something like this:

<?
$curgrp="";
$sql=mysql_query("SELECT groups.group_name, users.user_name FROM groups
INNER JOIN users ON groups.group_id=users.group_id GROUP BY
groups.group_name ORDER BY groups.group_name ASC, users.user_name
ASC",$DBH);
while($row=mysql_fetch_array($sql)) {
  if($curgrp!=$row["group_name"]) {
    if(strlen($curgrp)>0) echo "</table>";
    $curgrp=$row["group_name"];
    echo "<table border=1 cellpadding=2 cellspacing=2><tr><td
colspan=2>Group: <b>".$row["group_name"]."</b></td></tr>";
  }
  echo "<tr><td>&nbsp;&nbsp;</td><td>".$row["user_name"]."</td><tr>";
}
?>


This HAS NOT been tested. but I have ran it through the inbuilt PHP
interpretor in my brain.. and passed it through the mySQL server also up in
my head.. and it seems OK..


In any case it was mainly to demonstrate the concept.. As you can see.. It
iterates through the result table which should look a little like this
(using your data supplied):

Blue,Tom
Blue,Nancy
Blue,Jim
Red,Bob
Red,Susan
Red,James

You get it?

HIH.. Please if you notice any bugs in the coding above.. point it out.. I
did it quite quickly and with very little checking as such or testing..


Good luck ;)

I could post what I used but that could get confusing.. :-p

Anyways.. Now I'm rambling.. ha. I'm off..

------oOo---------------oOo------

 Julien Bonastre [The_RadiX]
 The-Spectrum Network CEO
 [EMAIL PROTECTED]
 www.the-spectrum.org

------oOo---------------oOo------

----- Original Message -----
From: "Rankin, Randy" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, September 23, 2002 10:36 PM
Subject: [PHP] Confused


> I have two MySQL tables, groups and users:
>
>         groups: group_id, group_name
>
>         users: user_id, user_name, group_id, etc.
>
> I would like to produce one table for each group which will list all the
>
> members for that particular group. For example:
>
>     Blue (group_id 1)
>
>         Tom (group_id 1)
>
>         Nancy (group_id 1)
>
>         Jim (group_id 1)
>
>     Red (group_id 2)
>
>         Bob (group_id 1)
>
>         Susan (group_id 1)
>
>         James (group_id 1)
>
> ...
>
> My question is, do I need to run 2 queries? The first to select all the
>
> groups and the second to select all the users in each group based on the
>
> group_id? How would I loop through to create a table for each group?
>
> Thanks in advance for any help.
>
> Randy Rankin
>
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to