DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Michael has posted 27 posts at DZone. View Full User Profile

Assign Multiple Members To A Group

01.18.2006
| 1058 views |
  • submit to reddit
        Assign multiple members pulled from a db table to a group.

<?
/*
Example Table Schema


CREATE TABLE `members` (
  `address` varchar(100) NOT NULL default '',
  `group_id` int(3) NOT NULL default '0'
) TYPE=MyISAM;

CREATE TABLE `group` (
  `group_id` int(75) NOT NULL auto_increment,
  `group_name` varchar(75) default NULL,
  `group_desc` longtext,
  PRIMARY KEY  (`group_id`)
) TYPE=MyISAM AUTO_INCREMENT=0 ;


*/
?>




<?php
echo "<form action='?a=add_members2group' method='POST'><br><br>
		<b>Group List:</b> <select name='group_id' onchange='document.myform.submit()'><option value=''>Select Group</option>";
		$query1 = "SELECT * FROM group WHERE 1";
		$result1 = mysql_query($query1);
		while ($row1 = mysql_fetch_assoc($result1))
		{
			if (urldecode($_POST['group_id']) ==	$row1['group_id']) $selected = 'selected';
			else $selected = '';
			echo "<option value='$row1[group_id]' $selected>".stripslashes(htmlentities($row1['group_name']));
		}		 	
		echo "</select>";
 
 
 $query2 = "SELECT address, group_id FROM members";
 $result1 = mysql_query($query2);

 

    echo "<br><br><b>Member List:</b> <select name='address[]' multiple size='10'><option value=''>Select Multiple Members</option>";


    while ($row2 = mysql_fetch_row($result1)) {
        $address = $row2[0];
        $group_id = $row2[1];

        echo "<option value='$address'";

       
        if (!isset($checked_array)) {
            $checked_array = (array) $checked_array;
        }

        if (in_array($address, $checked_array)) {
            echo " selected";
        }

		
		
		
       echo ">$address (";
	   
	   		
			if ($group_id != '0')
			{
				    $query3 = "SELECT group_name FROM group WHERE group_id='$group_id'";
					$result3 = mysql_query($query3);
					while ($row3 = mysql_fetch_assoc($result3))
					{
					
					 
					 echo "$row3[group_name]";
					 
					}
			   }
			   else
			   {
			   echo "Not Assigned";
			   }
	   
	   
	   echo ")</option>";

    }

    echo "</select>";
	echo "<br><br><input type='submit' value='Add Members'></form>";


 
 if ($_GET['a'] == 'add_members2group') 
 {
 
    $group_id = $_POST['group_id']; 
	$address_count = count($_POST['address']);
	$values = $_POST['address'];
	   
	foreach( $_POST['address'] as $address)
	{
   	   	$query = "UPDATE ".MYSQL_TBL_MAILLIST_SUBSCRIBERS." SET group_id='$group_id' WHERE address='$address'";
		$result = mysql_query($query) or die("Error: ".mysql_error());
	}
	
					
	echo "<p><b>$address_count Members have been added.<P>";
	
	
 } 
 
 ?>