Jump to content

while loops inside a while loop??


premora

Recommended Posts

Hi there,

 

Having an issue here.  I have a site where you can join a group.  When the user goes to the groups page it will list their groups that they have joined.  This is fine.  I also want to be able to show how many other members are in that group and am getting stuck.  $row group_name works ok but not sure how to start by adding another while loop in for the members.

 

The table consists of group id, group name, member id (L_ID) and member name

 

$result = mysql_query("SELECT * FROM groups2 WHERE L_ID = ". $_SESSION['member_ID'] .";");

 

while ( $row = mysql_fetch_array($result) ) {   

echo("<tr>

<td><font face='Arial, Helvetica, sans-serif' size='3'><strong>" . $row["group_name"] . "</strong></font></td>

</tr><tr>

<td><font face='Arial, Helvetica, sans-serif' size='1' color='#0000FF'><strong>Members ("???")</strong></font></td>

</tr><tr>

<td><hr width=95%><br></td>

</tr>");

}

Link to comment
Share on other sites

You need to have the group table separate from the users.

 

For instance,.

The GROUPS table

group_id

group_name

 

The USERS table

member_id

username

password

group  // This field would be INT and you would put a group id in it.

 

Then when you run an SQL query on the users you can JOIN the groups to it.

SELECT * FROM users
JOIN groups ON groups.group_id = users.group

Link to comment
Share on other sites

Prior to this post I did put 2 while loops in but it gave me 4 results when there are only 2 items in the DB currently.  Is there a way to use that instead?

 

I am a basic user when it comes to heavy coding so need some help on this i.e. some example code maybe?

 

Thanks

Link to comment
Share on other sites

Why exactly do you need double while loops in the first place, you have exactly specified this.  You mentioned that you wanted a member count, but if you follow my instructions from the begging, you can get that in one query... i.e  ONE LOOP.

Link to comment
Share on other sites

I fully understand what you are saying but the query was fine and all I have done is add your JOIN ON and now it has an error, so I don't understand why there is a query error.

 

Also what count information would be needed in the Members () part as we haven't added a COUNT(*) yet??

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.