premora Posted May 12, 2011 Share Posted May 12, 2011 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>"); } Quote Link to comment https://forums.phpfreaks.com/topic/236218-while-loops-inside-a-while-loop/ Share on other sites More sharing options...
Zane Posted May 12, 2011 Share Posted May 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236218-while-loops-inside-a-while-loop/#findComment-1214509 Share on other sites More sharing options...
premora Posted May 12, 2011 Author Share Posted May 12, 2011 Hi, Thanks for the advice. I haven't used a JOIN before. How would I code that in a while loop to get the results? Quote Link to comment https://forums.phpfreaks.com/topic/236218-while-loops-inside-a-while-loop/#findComment-1214513 Share on other sites More sharing options...
Zane Posted May 12, 2011 Share Posted May 12, 2011 no different than the way you already are doing it. ^^^^ up there. Just change your query. Quote Link to comment https://forums.phpfreaks.com/topic/236218-while-loops-inside-a-while-loop/#findComment-1214515 Share on other sites More sharing options...
premora Posted May 12, 2011 Author Share Posted May 12, 2011 I get an error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 108 Quote Link to comment https://forums.phpfreaks.com/topic/236218-while-loops-inside-a-while-loop/#findComment-1214517 Share on other sites More sharing options...
Zane Posted May 12, 2011 Share Posted May 12, 2011 It's a warning, not an error. And it has a meaning... highlighted in bold. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 108 In other words.. the query is bad. Quote Link to comment https://forums.phpfreaks.com/topic/236218-while-loops-inside-a-while-loop/#findComment-1214521 Share on other sites More sharing options...
premora Posted May 12, 2011 Author Share Posted May 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236218-while-loops-inside-a-while-loop/#findComment-1214523 Share on other sites More sharing options...
Zane Posted May 12, 2011 Share Posted May 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236218-while-loops-inside-a-while-loop/#findComment-1214525 Share on other sites More sharing options...
premora Posted May 12, 2011 Author Share Posted May 12, 2011 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?? Quote Link to comment https://forums.phpfreaks.com/topic/236218-while-loops-inside-a-while-loop/#findComment-1214530 Share on other sites More sharing options...
Zane Posted May 12, 2011 Share Posted May 12, 2011 you must have mispelled something.. or put in an inexistant field or table. Quote Link to comment https://forums.phpfreaks.com/topic/236218-while-loops-inside-a-while-loop/#findComment-1214541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.