Jump to content

[SOLVED] Group query not list them seperate. And choose how to group.


SEVIZ

Recommended Posts

Ok guys heres what I got!  My current code is this:

<?php 
if ($_POST["email"]<>'') { 
$ToEmail = $_POST["recip"]; 
$EmailSubject = $_POST["subject"]."\r\n";
$mailheader = "From: ".$_POST["email"]."\r\n"; 
$MESSAGE_BODY = nl2br($_POST["message"]);
// $MESSAGE_BODY .= ($_POST["auth"]); 
mail($ToEmail,$EmailSubject, $MESSAGE_BODY,$mailheader) or die ("Failure"); 
?> 
---
<?php 
} else { 
?> 

<form action="text_tool.php" method="post">
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
	<td valign="top">
	Send To:<br />
<?php 

mysql_connect("localhost", "XXXXX", "XXXXX") or die(mysql_error());
mysql_select_db("TEXT_TOOL") or die(mysql_error());

$q = "select * from sprint";
$ret = mysql_query($q);

print '<select name="recip">';
while ($row = mysql_fetch_assoc($ret)) {
        $email_addr = $row['num'];
$email_sec = $row['dept'];
$domain = '@messaging.sprintpcs.com';
        $email_addr .= $domain;
    print "<option value=\"$email_addr\">$email_sec</option>";
}
?>
		</select>
	</td>
</tr>


<tr>
<td>Message:<br /><textarea name=message cols="20" rows="6" id="comment" maxlength="143"></textarea><br />
</td>
</tr>
<tr>
<td>
<input name="email" type="hidden" id="email" size="20" value="[email protected]"></td>
</tr>
<tr>

<td valign="top"><font color="red" size="2"><b>Do not hit enter for a new line. This will give you less characters to use due to the text limits.</b></font><br /><input type="submit" name="Submit" value="Send"></td>
</tr>

</table>
</form> 
</body>
</html>
<?php 
}; 
?>


 

Right now it queries the DB and gives me select options of all the choices in row "dept". 

Shows up like this:

<option value="EMAIL">Tech</option>
<option value="EMAIL2">Tech</option>
<option value="EMAIL3">Tech</option>
<option value="EMAIL4">Sup</option>
<option value="EMAIL5">Sup</option>

 

This all works great.  With the above I get all 270 of my entries listed separate.  But what I need to do is have it query the same info but only display a main title (based on how its listed in the db under row:"dept") and have it email all of them. 

 

So what it SHOULD look like is:

<option value="EMAIL, EMAIL2, EMAIL3">Tech</option>
<option value="EMAIL4, EMAIL5">Sup</option>

 

I hope this makes sense.  There are over 10 options for the main titles.  Such as Tech, Sup, Mgr, HR, etc.  On top of the above I need some choices to overlap.  Such as this:

<option value="EMAIL, EMAIL2, EMAIL3">Tech</option>
<option value="EMAIL4, EMAIL5">Sup</option>
<option value="EMAIL, EMAIL2, EMAIL3, EMAIL4, EMAIL5">Techs and Sups</option>

 

I am at a loss on how to do this.  Any help is appreciated as always!  You guys rock!

Well your query would likely change to something SIMILAR to:

$q = "SELECT GROUP_CONCAT(DISTINCT email SEPARATOR ', ') 
FROM sprint 
GROUP BY dept";

As far as I can guess.

 

But there are length limitations to this method. http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

 

Perhaps someone can post a non-limited version, maybe using subqueries or something

Am, sorry, I didn't read fully.

 

However, you might be able to use PHP to combine the now grouped categories, right?  The dataset would come out as:

[dept][email,email]

 

And so when you build the list in php, you could modify the select <options> how you wanted.

<?php 

mysql_connect("localhost", "XXXXX", "XXXXX") or die(mysql_error());
mysql_select_db("TEXT_TOOL") or die(mysql_error());

if (!empty($_POST["email"])) { 
    $q = "select `num` from sprint WHERE `dept` = '".mysql_real_escape_string($_POST["email"])."'";
    $ret = mysql_query($q);
    $emails = array();
    while ($row = mysql_fetch_assoc($ret)) {
        $emails[] = $row['num'].'@messaging.sprintpcs.com';
    }
    if ($emails) {
        $ToEmail = implode(', ', $emails); 
        $EmailSubject = $_POST["subject"]."\r\n";
        $mailheader = "From: ".$_POST["email"]."\r\n"; 
        $MESSAGE_BODY = nl2br($_POST["message"]);
   // $MESSAGE_BODY .= ($_POST["auth"]); 
        mail($ToEmail,$EmailSubject, $MESSAGE_BODY,$mailheader) or die ("Failure"); 
    }
}
?>
---
<?php 
} else { 
?>

<form action="text_tool.php" method="post">
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
      <td valign="top">
      Send To:<br />
<?php 

$q = "select `dept` from sprint GROUP BY `dept`";
$ret = mysql_query($q);

print '<select name="recip">';
while ($row = mysql_fetch_assoc($ret)) {
    $email_sec = $row['dept'];
    print "<option value=\"$email_sec\">$email_sec</option>";
}
?>
         </select>
      </td>
   </tr>


<tr>
<td>Message:<br /><textarea name=message cols="20" rows="6" id="comment" maxlength="143"></textarea><br />
</td>
</tr>
<tr>
<td>
<input name="email" type="hidden" id="email" size="20" value="[email protected]"></td>
</tr>
<tr>

<td valign="top"><font color="red" size="2"><b>Do not hit enter for a new line. This will give you less characters to use due to the text limits.</b></font><br /><input type="submit" name="Submit" value="Send"></td>
</tr>

</table>
</form>
</body>
</html>
<?php 
}; 
?>

 

EDIT: Fix typo

That's what comes with not using an editor ;-)

 

<?php 

mysql_connect("localhost", "XXXXX", "XXXXX") or die(mysql_error());
mysql_select_db("TEXT_TOOL") or die(mysql_error());

if (!empty($_POST["email"])) {
    $q = "select `num` from sprint WHERE `dept` = '".mysql_real_escape_string($_POST["email"])."'";
    $ret = mysql_query($q);
    $emails = array();
    while ($row = mysql_fetch_assoc($ret)) {
        $emails[] = $row['num'].'@messaging.sprintpcs.com';
    }
    if ($emails) {
        $ToEmail = implode(', ', $emails); 
        $EmailSubject = $_POST["subject"]."\r\n";
        $mailheader = "From: ".$_POST["email"]."\r\n"; 
        $MESSAGE_BODY = nl2br($_POST["message"]);
   // $MESSAGE_BODY .= ($_POST["auth"]); 
        mail($ToEmail,$EmailSubject, $MESSAGE_BODY,$mailheader) or die ("Failure");
        echo 'Message sent';
    }
}
else {
?>

<form action="text_tool.php" method="post">
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
      <td valign="top">
      Send To:<br />
<?php 

$q = "select `dept` from sprint GROUP BY `dept`";
$ret = mysql_query($q);

print '<select name="recip">';
while ($row = mysql_fetch_assoc($ret)) {
    $email_sec = $row['dept'];
    print "<option value=\"$email_sec\">$email_sec</option>";
}
?>
         </select>
      </td>
   </tr>


<tr>
<td>Message:<br /><textarea name=message cols="20" rows="6" id="comment" maxlength="143"></textarea><br />
</td>
</tr>
<tr>
<td>
<input name="email" type="hidden" id="email" size="20" value="[email protected]"></td>
</tr>
<tr>

<td valign="top"><font color="red" size="2"><b>Do not hit enter for a new line. This will give you less characters to use due to the text limits.</b></font><br /><input type="submit" name="Submit" value="Send"></td>
</tr>

</table>
</form>
</body>
</html>
<?php 
}

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.