There must be an easier way

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

There must be an easier way

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Post Reply