Page 1 of 1

There must be an easier way

Posted: Fri Jul 28, 2006 6:14 am
by php3ch0
Hi

I am writing a script to send mail to certain groups of staff. I have check boxes for chosing which group it should be sent to. I am writing a script so that when the checkboxes are selected it will only go to that group of staff. If the staff member is in more than one group he should only receive it once.

All the deatils are stored in a mysql database looking something like this

id
email_address
DDPG (Y/N)
TDPG (Y/N)
SG (Y/N)


At the moment I am using this code

Code: Select all

if($row_email['SG'] == 'Y' && $row_email['DDPG'] == 'Y' && $row_email['TDPG'] == 'Y' ) {
		$query1 = "SG = 'Y' OR DDPG = 'Y' OR TDPG = 'Y'";
		}
		//YNN
		if($row_email['SG'] == 'Y' && $row_email['DDPG'] == 'N' && $row_email['TDPG'] == 'N' ) {
		$query1 = "SG = 'Y'";
		}
		//NYN
		if($row_email['SG'] == 'N' && $row_email['DDPG'] == 'Y' && $row_email['TDPG'] == 'N' ) {
		$query1 = "DDPG = 'Y'";
		}
		//NNY
		if($row_email['SG'] == 'N' && $row_email['DDPG'] == 'N' && $row_email['TDPG'] == 'Y' ) {
		$query1 = "TDPG = 'Y'";
		}
		//YYN
		if($row_email['SG'] == 'Y' && $row_email['DDPG'] == 'Y' && $row_email['TDPG'] == 'N' ) {
		$query1 = "SG = 'Y' OR DDPG = 'Y'";
		}
		//NYY
		if($row_email['SG'] == 'N' && $row_email['DDPG'] == 'Y' && $row_email['TDPG'] == 'Y' ) {
		$query1 = "DDPG = 'Y' OR TDPG = 'Y'";
		}
		//YNY
		if($row_email['SG'] == 'Y' && $row_email['DDPG'] == 'N' && $row_email['TDPG'] == 'Y' ) {
		$query1 = "SG = 'Y' OR TDPG = 'Y'";
		}
		
		mysql_select_db($database_db_connect, $db_connect);
		$query_list = "SELECT * FROM mailing_list WHERE ".$query1;
		$list = mysql_query($query_list, $db_connect) or die(mysql_error());
		$row_list = mysql_fetch_assoc($list);
		$totalRows_list = mysql_num_rows($list);
THe problem is I need to create a new list and there are going to be a lot more options. There must be an easier way. Please help

Posted: Fri Jul 28, 2006 6:47 am
by Jenk
GROUP BY `email_address` will eliminate duplicate email addresses.

and on your post form, use the checkboxes:

Code: Select all

<form action="" method="post">
    <input type="checkbox" name="group[]" value="SD" />
    <input type="checkbox" name="group[]" value="TD" />
    <!-- etc -->
</form>
and for the php:

Code: Select all

$query = 'SELECT * FROM `table` WHERE ';

$switch = false;

foreach ($_POST['group'] as $group) {
    if ($switch) $query .= 'AND ';
    $query .= "`$group` = 'Y' ";
    $switch = true;
}

$query .= "GROUP BY `email_address`";

//etc.
untested.