TapeGun007 Posted June 5, 2010 Share Posted June 5, 2010 Ok, I give up, I'm having a brain fart, and I know this has to be more simple that my brain is making it out to be. In the Database, I have a "group" field that contains this: Choir Chorale Directors Mass Choir Musicians Sign Choir Sound Crew Spanish Music Worship Leaders Worship Teams Youth Chorale Youth Worship Team When a person joins a group, their ID number is added to the group ID. So, when an admin loads up a particular user to see what groups they belong to, the webpage will spit out this information: John Doe belongs to: Choir [remove] Chorale [remove] Directors [remove] The [remove] is simply where an admin can click and it just removes this user from the group. I store the groups that John Doe is a part of into an Array called $GroupArray for later use. Just below this, it shows all the groups they can become a part of: Choir [Add] Chorale [Add] Directors [Add] Mass Choir [Add] Musicians [Add] Sign Choir [Add] Sound Crew [Add] Spanish Music [Add] Worship Leaders [Add] Worship Teams [Add] Youth Chorale [Add] Youth Worship Team [Add] By clicking the [Add], the page will refresh and add this person to the group. However, what I really want to do, is if a person like John Doe belongs to the group "Choir", I do not want the "Choir" group to show up in the Add section. As it stands right now, you can click on the [Add] 6 times, and it would the person to the Choir group 6 times. Here is the code that adds the groups that John Doe already belongs to: <?php // Create an array, store the groups they are a part of, then do NOT display those same groups in the ADD section // this way you cannot add a person to a group more than once... and it looks great $grouparray=array(); $x=0; $result = mysql_query("SELECT * FROM GroupMember WHERE MemberID='$ID' ORDER BY GroupName"); while($row = mysql_fetch_array($result)){ $x++; $GroupArray[$x]=$row['GroupName']; echo "<tr><td>" . $row['GroupName'] . "</td><td><a href='members.php?list=edit&MemberID=$ID'>[ Remove ]</a></td><tr>"; } ?> Here is the part of the code that just simply displays all the groups, and it incorrect. <?php $result = mysql_query("SELECT * FROM Groups ORDER BY GroupName"); while($row = mysql_fetch_array($result)){ foreach ($GroupArray as $value){ If ($row['GroupName'] <> $value){ echo "<tr><td>".$row['GroupName']."</td><td><a href='members.php?list=edit&MemberID=$ID&GroupAdd=".$row['GroupName']."'>[ Add ]</a></td></tr>"; break; } } } ?> This last snippet just simply spits out all the groups. I only want it to spit out the groups that John Doe is NOT a part of. Please be gentle, I'm still a bit new to php. Hope this all makes sense. I have a picture so you can see it. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/203933-checking-db-against-array/ Share on other sites More sharing options...
dabaR Posted June 5, 2010 Share Posted June 5, 2010 This is what you need: <?php $result = mysql_query("SELECT * FROM Groups ORDER BY GroupName"); while($row = mysql_fetch_array($result)){ If (!in_array($value, $GroupArray)){ echo "<tr><td>".$row['GroupName']."</td><td><a href='members.php?list=edit&MemberID=$ID&GroupAdd=".$row['GroupName']."'>[ Add ]</a></td></tr>"; break; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/203933-checking-db-against-array/#findComment-1068062 Share on other sites More sharing options...
TapeGun007 Posted June 5, 2010 Author Share Posted June 5, 2010 Hey, thank you for the quick reply. I didn't know about the in_array command. However, I did put in your code. This is the result I got (see image): [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/203933-checking-db-against-array/#findComment-1068069 Share on other sites More sharing options...
dabaR Posted June 5, 2010 Share Posted June 5, 2010 Make sure you have !in_array(), and remove the break from there, I missed that. Quote Link to comment https://forums.phpfreaks.com/topic/203933-checking-db-against-array/#findComment-1068076 Share on other sites More sharing options...
TapeGun007 Posted June 5, 2010 Author Share Posted June 5, 2010 Ah.... also had to change If (!in_array($row['GroupName'], $GroupArray)){ Works perfectly now! Thank you so much for pointing me in the right direction! Quote Link to comment https://forums.phpfreaks.com/topic/203933-checking-db-against-array/#findComment-1068079 Share on other sites More sharing options...
dabaR Posted June 5, 2010 Share Posted June 5, 2010 Man, I was crazy off point there :-) TTYL! Quote Link to comment https://forums.phpfreaks.com/topic/203933-checking-db-against-array/#findComment-1068084 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.