DasHaas Posted January 21, 2008 Share Posted January 21, 2008 I have a form that emails all the subscribers in my MYSQL database. I would like to be able to filter the people I contact i.e. all males in FL, etc. That part I have down but when I start looking at all the possible combinations I get a little lost. Here is my code as you can see my sql statement is looking for a values in the where clauses, what should the value be if I select the all states or all genders options, or a combination of both (all females in CO)? Im thinking that I have to write a sql query for every possible combination but there has to be an easier way especially if I add more filters like city, country, etc. <?php // includes include('includes/PXS_Conf.php'); // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect to MySql server.'); // select database mysql_select_db($db) or die ('Unable to select database.'); $sql_state = mysql_query("SELECT state FROM PXS_Newsletter WHERE verified = 1 GROUP BY state") or die (mysql_error()); $sql_gender = mysql_query("SELECT gender FROM PXS_Newsletter WHERE verified = 1 GROUP BY gender") or die (mysql_error()); // form not yet submitted // display initial form if (!$_POST['submit']) { ?> <table cellpadding="5" cellspacing="5" class="table1"> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <tr> <td valign="top" class="text5">State</td> <td> <?php echo "<select name='ssl_state'>"; echo "<option value='0' selected>Please select a state</option>"; echo "<option value='1'>Select all states</option>"; if(mysql_num_rows($sql_state)) { while($row = mysql_fetch_assoc($sql_state)) { echo "<option value = '$row[state]'>$row[state]</option>"; } } else { echo "<option>No states present</option>"; } ?> </td> </tr> <tr> <td valign="top" class="text5">Gender</td> <td> <?php echo "<select name='ssl_gender'>"; echo "<option value='0' selected>Please select a gender</option>"; echo "<option value='1'>Select all genders</option>"; if(mysql_num_rows($sql_gender)) { while($row = mysql_fetch_assoc($sql_gender)) { echo "<option value = '$row[gender]'>$row[gender]</option>"; } } else { echo "<option>No genders present</option>"; } ?> </td> <tr> <tr> <td valign="top" class="text5">Subject</td> <td><input name="txtsubject" type="text" class="table2" id="txtsubject" size="50" maxlength="250"></td> </tr> <tr> <td valign="top" class="text5">Email Body</td> <td valign="top" class="text5"><textarea name="txtbody" cols="47" rows="10" class="table2" id="txtbody"></textarea></td> </tr> <tr> <td colspan="2" valign="top" class="text5"><input name="submit" type="submit" id="submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td> </tr> </form> </table> <?php } else { // set up error list array $errorList = array(); $msgsubject = $_POST['txtsubject']; $msgbody = $_POST['txtbody']; $state = trim($_POST['ssl_state']); $gender = trim($_POST['ssl_gender']); // validate text input fields if (trim($_POST['txtsubject']) == '') {$errorList[] = 'Invalid entry: Message Subject';} if (trim($_POST['txtbody']) == '') {$errorList[] = "Invalid entry: Message Body";} if ($_POST['ssl_state'] == '0') {$errorList[] = "Please select a state";} if ($_POST['ssl_gender'] == '0') {$errorList[] = "Please select a gender";} // check for errors // if none found... if (sizeof($errorList) == 0) { // generate and execute query $query = mysql_query("SELECT email FROM PXS_Newsletter WHERE state = $state AND gender = $gender AND verified = 1") or die (mysql_error()); while($row = mysql_fetch_row($query)){ $email = $row[0]; // send email $headers = "Reply-To:".$fromname."<".$fromaddress.">".$eol; mail($email, $msgsubject, $msgbody, $headers);} // print result echo '<center><span class="text6">Message sent!</span></center>'; Thanks in advance, any help would be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/87119-if-statement-for-all-possible-form-select-box-combination/ Share on other sites More sharing options...
cooldude832 Posted January 21, 2008 Share Posted January 21, 2008 just from a glance try this 1) Get a set of fields you want to use as criteria (State, Gender) stuff with a finite number of unique values. 2) Run a query that is like <?php $q = "Select UNIQUE($field1) as $field1_u, UNIQUE($field2) as $field2_i from `table`...."; ?> and then from there you populate your select list from these "unique" values and you can select which ones to send or send all or make them multi selects so you can send to partial complete list Quote Link to comment https://forums.phpfreaks.com/topic/87119-if-statement-for-all-possible-form-select-box-combination/#findComment-445556 Share on other sites More sharing options...
DasHaas Posted January 21, 2008 Author Share Posted January 21, 2008 Im able to populate the list menu with the unique values, its sending the emails to the selected groups that im having a problem with. Quote Link to comment https://forums.phpfreaks.com/topic/87119-if-statement-for-all-possible-form-select-box-combination/#findComment-445650 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.