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
https://forums.phpfreaks.com/topic/203933-checking-db-against-array/
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;
                  }
            }
?>

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.