Results 1 to 4 of 4

Thread: Multiple Recipients

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple Recipients

    hello, I am trying to make a PM system that will allow the message to be sent to multiple users. I tried using:
    PHP Code:
    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
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:
    Code:
    Warning: Invalid argument supplied for foreach() in /home/bntqann/public_html/client_work/cms/ucp/includes/send.inc.php on line 34
    Here is the code for my foreach statement:
    PHP Code:
    // 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();">&lt;&lt;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
    Last edited by Titan85; 07-24-2007 at 01:57 PM.
    Thanks DD, you saved me countless times

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •