Jump to content

Checking db against Array


TapeGun007

Recommended Posts

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]

Link to comment
Share on other sites

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;
                  }
            }
?>

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.