Hi guys.
I’m hoping you can help me out please.
We have a private messaging system which allows user->user communication, and also for a “Group” message to be sent to a whole group and all its members.
There’s one annoying thing though, at present, only one recipient can be selected, or one group.
We want to update the attached code to allow for up to 5 recipients (in the “send” case) and up to 3 groups (in the “group case”) to be sent.
I’ve tried doing this myself, but I’m getting really confused on how to do it.
As you will see with the code, the user->user communication (“send” case) checks that the sender hasn’t sent too many messages in a certain time, and that the recipent’s inbox isn’t full.
I was wondering if anyone could help me updating the below code to work with sending to more than one recipient and group but to still apply the same checks? One other thing I cannot figure out is how to check that the same recipient on the “send” case isn’t listed twice – so they don’t receive the same message twice. This is already done with the “group” case with SELECT DISTINCT
Also, for the Group message, it states in the message which group it was sent to – I was hoping it could state all the groups.
I’m hoping this makes sense I’ve tried to scale it down so just the basics are there – hope that helps.
Thanks in advance.
[code=php]
if(count($toAr) > 5){
$write = 0;
$msg .= 'Too many recipients';
}[/code]
-- </H2>
<H2>-- Table structure for table <C><span><code>privmsg</code></span></C>
-- </H2>
CREATE TABLE <C><span><code>privmsg</code></span></C> (<br/>
<C><span><code>id</code></span></C> int(11) NOT NULL auto_increment,<br/>
<C><span><code>to_id</code></span></C> varchar(40) NOT NULL,<br/>
<C><span><code>from_id</code></span></C> varchar(40) NOT NULL,<br/>
<C><span><code>time_sent</code></span></C> datetime NOT NULL,<br/>
<C><span><code>subject</code></span></C> varchar(50) NOT NULL default '',<br/>
<C><span><code>message</code></span></C> text NOT NULL,<br/>
<C><span><code>opened</code></span></C> char(1) NOT NULL default 'n',<br/>
<C><span><code>time_opened</code></span></C> datetime default NULL,<br/>
<C><span><code>deleted_from</code></span></C> int(11) NOT NULL default '0',<br/>
<C><span><code>deleted_to</code></span></C> int(11) NOT NULL default '0',<br/>
PRIMARY KEY (<C><span><code>id</code></span></C>)<br/>
) ENGINE=InnoDB AUTO_INCREMENT=71131 DEFAULT CHARSET=latin1 AUTO_INCREMENT=71131 ;<br/>
[code=php] foreach($toAr as $to){
$sql = "INSERT INTO privmsg (to_id, from_id, time_sent, subject, message, deleted_from, deleted_to) VALUES ('".mysql_real_escape_string($to)."', '".mysql_real_escape_string($from)."', '".mysql_real_escape_string($dateadded)."', '".mysql_real_escape_string($sub)."', '".mysql_real_escape_string($message)."', 1, 0)";
// add these two lines:
if( ! mysql_query2($sql))
echo " <ul>n <li><span>Database error; could not send message to {$to} (".mysql_error().")</span></li>n </ul>nn";
// and that should work.
}[/code]
[code=php]
$groups = is_array($_POST['group'])?$_POST['group']:array($_POST['group']);
//...
if(in_array('ALL', $groups))// send to all members, like you are doing
// ...
}else{// now for the hard part, getting the users who's group in an array is set in an integer, horrible.
foreach($groups as $k=>$group)
$groups[$k] = mysql_real_escape_string($group);
$users_to_notify=mysql_query2("SELECT DISTINCT account FROM user WHERE deleted=0 AND ( " . implode( '=1 OR ', $groups) . "=1)");
///...
[/code]
[code=php]
// Note array_unique() here for unique accounts; no duplicates
$groups = !is_array($_POST['group']) ? $_POST['group'] : array_unique($_POST['group']);
foreach($groups as $i=>$group) {
if(empty($group)) {
unset($groups[$i]);
} else {
$groups[$i] = htmlentities($group);
}
}
$from = $_SESSION['account'];
// blah blah for the rest; the same
// Send to everyone
if (in_array('All', $groups)) {
// Find ALL accounts from * user
$users_to_notify=mysql_query2("SELECT * FROM user WHERE deleted=0");
$recipient_total = mysql_num_rows($users_to_notify);
// Notify all found users
while ($user_to_notify=mysql_fetch_array($users_to_notify))
$groupnotify = mysql_query2("INSERT INTO privmsg (to_id, from_id, time_sent, subject, message, deleted_from, deleted_to) VALUES"
." ('".$user_to_notify['account']."', '".mysql_real_escape_string($from)."', '".mysql_real_escape_string($dateadded)."', '(".implode(', ', $groups).") ".mysql_real_escape_string($sub)."', '".mysql_real_escape_string($edited)."', 1, 0)");
} else {
// Multiple Groups; check for DE
if (in_array('DE', $groups)) {
// Send to DEM and DEAM, too
$users_to_notify=mysql_query2("SELECT DISTINCT account FROM user WHERE deleted=0 AND (DEM=1 OR DEAM=1)");
$recipient_total = mysql_num_rows($users_to_notify);
// Notify all found users
while ($user_to_notify=mysql_fetch_array($users_to_notify))
$groupnotify = mysql_query2("INSERT INTO privmsg (to_id, from_id, time_sent, subject, message, deleted_from, deleted_to) VALUES"
." ('".$user_to_notify['account']."', '".mysql_real_escape_string($from)."', '".mysql_real_escape_string($dateadded)."', '(".$group.") ".mysql_real_escape_string($sub)."', '".mysql_real_escape_string($edited)."', 1, 0)");
}
foreach($groups as $k=>$group) {
$groups[$k] = mysql_real_escape_string($group);
$users_to_notify=mysql_query2("SELECT DISTINCT account FROM user WHERE deleted=0 AND ( " . implode( '=1 OR ', $groups) . "=1)")
$recipient_total = mysql_num_rows($users_to_notify);
// Notify all found users
while ($user_to_notify=mysql_fetch_array($users_to_notify))
$groupnotify = mysql_query2("INSERT INTO privmsg (to_id, from_id, time_sent, subject, message, deleted_from, deleted_to) VALUES"
." ('".$user_to_notify['account']."', '".mysql_real_escape_string($from)."', '".mysql_real_escape_string($dateadded)."', '(".$group.") ".mysql_real_escape_string($sub)."', '".mysql_real_escape_string($edited)."', 1, 0)");
}
}
if (!$groupnotify) {
// database error
} else {
// sent ok
}
[/code]
if(in_array('DE', $groups)){
//.....
}
<i>
</i>if(in_array('DE', $groups))
array_push($groups, 'DEM', 'DEAM');
This would also reduce the chance of it sending a PM to someone twice.[code=php]
// Note array_unique() here for unique accounts; no duplicates
$groups = !is_array($_POST['group']) ? $_POST['group'] : array_unique($_POST['group']);
$from = $_SESSION['account'];
// blah blah for the rest; the same
// Send to everyone
if (in_array('All', $groups)) {
// Find ALL accounts from * user
$users_to_notify=mysql_query2("SELECT * FROM user WHERE deleted=0");
$recipient_total = mysql_num_rows($users_to_notify);
// Notify all found users
while ($user_to_notify=mysql_fetch_array($users_to_notify))
$groupnotify = mysql_query2("INSERT INTO privmsg (to_id, from_id, time_sent, subject, message, deleted_from, deleted_to) VALUES"
." ('".$user_to_notify['account']."', '".mysql_real_escape_string($from)."', '".mysql_real_escape_string($dateadded)."', '(".implode(', ', $groups).") ".mysql_real_escape_string($sub)."', '".mysql_real_escape_string($edited)."', 1, 0)");
} else {
if(in_array('DE', $groups))
array_push($groups, 'DEM', 'DEAM');
foreach($groups as $k=>$group)
$groups[$k] = mysql_real_escape_string($group);
$users_to_notify=mysql_query2("SELECT DISTINCT account FROM user WHERE deleted=0 AND ( " . implode( '=1 OR ', $groups) . "=1)")
$recipient_total = mysql_num_rows($users_to_notify);
// Notify all found users
while ($user_to_notify=mysql_fetch_array($users_to_notify))
$groupnotify = mysql_query2("INSERT INTO privmsg (to_id, from_id, time_sent, subject, message, deleted_from, deleted_to) VALUES"
." ('".$user_to_notify['account']."', '".mysql_real_escape_string($from)."', '".mysql_real_escape_string($dateadded)."', '(".$group.") ".mysql_real_escape_string($sub)."', '".mysql_real_escape_string($edited)."', 1, 0)");
}
if (!$groupnotify) {
// database error
} else {
// sent ok
}
[/code]
implode(', ', $groups)
to return a list of groups, that's about it.Parse error: syntax error, unexpected T_VARIABLE
<i>
</i> $users_to_notify=mysql_query2("SELECT DISTINCT account FROM user WHERE deleted=0 AND ( " . implode( '=1 OR ', $groups) . "=1)")
[b] $recipient_total = mysql_num_rows($users_to_notify);[/b]
// Notify all found users
[code=php]
// Note array_unique() here for unique accounts; no duplicates
$groups = !is_array($_POST['group']) ? $_POST['group'] : array_unique($_POST['group']);
foreach($groups as $i=>$group) {
if(empty($group)) {
unset($groups[$i]);
} else {
$groups[$i] = htmlentities($group);
}
}
$from = $_SESSION['account'];
$sub = htmlentities($_POST['subject']);
$message = htmlentities($_POST['msg']);
[/code]
<span><code>
</CODE>around variable names should be sufficient protection:
<CODE lang="php">[code=php] $users_to_notify=mysql_query2("SELECT DISTINCT account FROM user WHERE deleted=0 AND (
" . implode( '=1 OR
', $groups) . "`=1)");[/code]And quicker, and safer, since nasty little queries can't get submitted.JU
SU
0.1.9 — BETA 5.4