View Full Version : Multiple Recipients
Titan85
07-23-2007, 02:14 PM
hello, I am trying to make a PM system that will allow the message to be sent to multiple users. I tried using:
while ($recipients = explode(',', $recipient)) {
$insert = mysql_query("INSERT INTO `pm` (id, recipient, sender, subject, message, status, date) VALUES ('', '$recipients', '$user', '$subject', '$message', 'unread', '$date')") or die ("Error Sending Message! \n<br />\n" .mysql_error());
}
echo('<meta http-equiv="refresh" content="2;URL=ucp.php" /> <div class="message">Your message has been sent</div>');
}
When I send the message, it inserts about 127 lines of duplicate data into my table. Anyone know what is wrong? Thanks
djr33
07-23-2007, 05:25 PM
You're repeatedly exploding. While is always correct there... it won't ever stop being true.
$recipients = explode(',',$recipients);
foreach ($recipients as $recipient) {
$recipient = trim($recipient);
$recipients is originally text with each user separated by commas. Then this uses a foreach loop to go through each part of the array.
Remember to add a close bracket at the end -- } for this new layer.
$recipient will be the value you are adding now. trim() on this makes it take off any extra spaces, like "user1, user2", etc.
Titan85
07-24-2007, 01:13 PM
Thanks, its working great like that. However, I am having some trouble with the error displaying. I am trying to get it to show a line saying, "The user username does not exist" for each user they enter that doesn't exist. I get this error whenever I send the message:
Warning: Invalid argument supplied for foreach() in /home/bntqann/public_html/client_work/cms/ucp/includes/send.inc.php on line 34Here is the code for my foreach statement:
// Send to each user
$recipients = explode(',', $recipient);
foreach ($recipients as $recipient) { $recipient = trim($recipient);
//
// Check user
$qry = mysql_query("SELECT * FROM `users` WHERE user = '$recipient'") or die ("Error Checking User! \n<br />\n" .mysql_error());
$chk = mysql_num_rows($qry);
if ($chk < 1) { $error = trim($recipient); }
//
else {
$insert = mysql_query("INSERT INTO `pm` (id, recipient, sender, subject, message, status, date) VALUES ('', '$recipient', '$user', '$subject', '$message', 'unread', '$date')") or die ("Error Sending Message! \n<br />\n" .mysql_error());
echo('<meta http-equiv="refresh" content="3;URL=messages.php" /> <div class="message">Your message has been sent to '.$recipient.'<br /></div>');
}
}
// If Error Sending
if ($error) {
echo('<div class="error">');
foreach ($error as $r) {
echo('The recipient <b>'.$r.'</b> does not exist. <br />');
}
echo('</div> <a href="javascript:history.back();"><<Back</a>');
}
//
else { echo('<meta http-equiv="refresh" content="3;URL=messages.php" /> <div class="message">Your message has been sent.</div>'); }I know its an error in the second foreach area, but I am not too familiar with using this type of thing, so I can't tell what it is. Anyone know? Thanks
djr33
07-25-2007, 11:56 PM
I see two problems with the script.
1. As the error says, the foreach is incorrect. This means, in almost all cases, that the array supplied is not an array.
Use print_r($recipients); before that to see what is really generated.
I'm guessing that $recipient may be wrong, somewhere before the code supplied.
2. $error is set if it finds a nonexistent user, once. If all of the other users are there, it still wouldn't work because it would have $error set.
I hope this points you in the right direction.
The error here is tracking variables, so just go through the script, line by line if you must, and track what their values are at each step.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.