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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.