Jump to content

Return an Array from Function - Help Please.


chito

Recommended Posts

Hi.. My script is processing up to 10 inputs from a form (name, email) and mailing out invite to each email. I need to echo after the emails are processed a list of who was emailed. I seem to be stuck on Return Array from a Function. I've tried also using return array ($array) to no luck as well. I've been able to build the array correctly in the function for each email processed now I need to build the array outside to create a running list of all the emails that were processed. Thanks for any help or advice.

 

<?php

function mail_message ($name,$email,$template_file) {
    if (empty ($name)) {
        echo "The fields are empty<br/>";
        return ;
    }

    else {


        $sent['name'] = $name;
        $sent['email'] = $email;

        #echo "inside $name, $email<br/>";
        $email_message = file_get_contents($template_file);
        $email_message = str_replace("#NAME#", $name, $email_message);

        #construct the email headers
        $to = $email;
        $from = "webmaster@viaeterna.com";
        $email_subject = "Your invited to a party!";

        #now mail
        mail($to, $email_subject, $email_message, "From: Party List Central ".$from);

       #for me to see if its building the array inside the function
        echo '<pre>';
        print_r($sent);
        echo '</pre>';

        return $sent;

    }
}


$list = array();
foreach ($_POST as $key => $value) {
    // is this a name?
    if (substr($key, 0, 4) == 'name') {
        $index = (int)substr($key, 4);
        $list[$index]['name'] = $value;
        $name = $value;
        # echo "$name<br/>";
    }

    // is this an email
    if (substr($key, 0, 5) == 'email') {
        $index = (int)substr($key, 5);
        $list[$index]['email'] = $value;
        $email = $value;
        # echo "$email<br/>";
        //DOCUMENT_ROOT is the file path leading up to the template name.
        mail_message($name,$email,'./invite_text.txt');
    }




}

$sent=mail_message($sent);

echo '<pre>';
print_r($sent);
echo '</pre>';

?>

 

Link to comment
Share on other sites

try this but i am not sure that it will work>>>>

 

$list = array();

$sentemail = "";

foreach ($_POST as $key => $value) {

    // is this a name?

    if (substr($key, 0, 4) == 'name') {

        $index = (int)substr($key, 4);

        $list[$index]['name'] = $value;

        $name = $value;

        # echo "$name<br/>";

    }

 

    // is this an email

    if (substr($key, 0, 5) == 'email') {

        $index = (int)substr($key, 5);

        $list[$index]['email'] = $value;

        $email = $value;

        # echo "$email<br/>";

        //DOCUMENT_ROOT is the file path leading up to the template name.

      $sentemail .= mail_message($name,$email,'./invite_text.txt');

    }

 

 

 

 

}

 

 

echo '<pre>';

print_r($sentemail);

echo '</pre>';

 

Link to comment
Share on other sites

can we see the script that's calling this function? i presume you're iterating through the array of names/emails (as per the other thread) and running this function each time? if so, it's likely that you're replacing the $sent array every time mail_message() is called. the tell-tale sign of this being the issue is that it only ever contains the information of the last person mailed at the end.

Link to comment
Share on other sites

Here's the code for the input form. It's an assignment for my class. Thanks

 


    <head>
        <title>Party Invitation Central</title>
    </head>
<body>
    <h3><b>Invite up to 10 people to your party.</b></h3>

    <form method="POST" action="invite_process.php">

        <table>
            <tbody>
                <tr>
                    <td align="right">Name:</td>
                    <td align="left"><input type="text" name="name1" value="" size="25" /></td>
                    <td align="right">Email:</td>
                    <td align="left"><input type="text" name="email1" value="" size="25" /></td>
                </tr>
                <tr>
                    <td align="right">Name:</td>
                    <td align="left"><input type="text" name="name2" value="" size="25" /></td>
                    <td align="right">Email:</td>
                    <td align="left"><input type="text" name="email2" value="" size="25" /></td>
                </tr><tr>
                    <td align="right">Name:</td>
                    <td align="left"><input type="text" name="name3" value="" size="25" /></td>
                    <td align="right">Email:</td>
                    <td align="left"><input type="text" name="email3" value="" size="25" /></td>
                </tr><tr>
                    <td align="right">Name:</td>
                    <td align="left"><input type="text" name="name4" value="" size="25" /></td>
                    <td align="right">Email:</td>
                    <td align="left"><input type="text" name="email4" value="" size="25" /></td>
                </tr><tr>
                    <td align="right">Name:</td>
                    <td align="left"><input type="text" name="name5" value="" size="25" /></td>
                    <td align="right">Email:</td>
                    <td align="left"><input type="text" name="email5" value="" size="25" /></td>
                </tr><tr>
                    <td align="right">Name:</td>
                    <td align="left"><input type="text" name="name6" value="" size="25" /></td>
                    <td align="right">Email:</td>
                    <td align="left"><input type="text" name="email6" value="" size="25" /></td>
                </tr><tr>
                    <td align="right">Name:</td>
                    <td align="left"><input type="text" name="name7" value="" size="25" /></td>
                    <td align="right">Email:</td>
                    <td align="left"><input type="text" name="email7" value="" size="25" /></td>
                </tr><tr>
                    <td align="right">Name:</td>
                    <td align="left"><input type="text" name="name8" value="" size="25" /></td>
                    <td align="right">Email:</td>
                    <td align="left"><input type="text" name="email8" value="" size="25" /></td>
                </tr><tr>
                    <td align="right">Name:</td>
                    <td align="left"><input type="text" name="name9" value="" size="25" /></td>
                    <td align="right">Email:</td>
                    <td align="left"><input type="text" name="email9" value="" size="25" /></td>
                </tr><tr>
                    <td align="right">Name:</td>
                    <td align="left"><input type="text" name="name10" value="" size="25" /></td>
                    <td align="right">Email:</td>
                    <td align="left"><input type="text" name="email10" value="" size="25" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" value="SUBMIT" />
                    </td>
                </tr>

            </tbody>
        </table>



    </form>
</body>


Link to comment
Share on other sites

can we see the script that's calling this function? i presume you're iterating through the array of names/emails (as per the other thread) and running this function each time? if so, it's likely that you're replacing the $sent array every time mail_message() is called. the tell-tale sign of this being the issue is that it only ever contains the information of the last person mailed at the end.

 

Thats correct thats the whole script tho. I'm iterating and sending to the function to do the mailing then I'm trying to keep an array populated of each email that is processed and not blank, then after mailing, echo a list that shows who was emailed. I may be trying to do to much in that function, maybe not the right place to do it. Like I mentioned in the other post, Im just learning and trying to wrap my head around the best practices part of PHP.. any advice on how to structure this better? Am I way off?  :-[

Link to comment
Share on other sites

haha oops - forgot to scroll down in the first post to see the rest of the code..

 

anyhow, you're not far off the mark, you just have the mail_message() call in the wrong place. once the code you've gotten from the other thread runs, you're left with all your name/email pairs in $list. what you need to do is iterate through that list, and use mail_message() for every pair. furthermore, we can edit your mail_message() function to return a message indicating the result, rather than the name/email pair - you don't need to return the pair, since we already know what it is (we have to pass it to the function after all):

 

<?php

function mail_message ($name,$email,$template_file) {
    if (empty ($name) || empty($email) || empty($template_file)) {
        return "One or more fields are missing";
    }

    else {

        #echo "inside $name, $email<br/>";
        $email_message = file_get_contents($template_file);
        $email_message = str_replace("#NAME#", $name, $email_message);

        #construct the email headers
        $to = $email;
        $from = "webmaster@viaeterna.com";
        $email_subject = "Your invited to a party!";

        #now mail
        $mailing_result = mail($to, $email_subject, $email_message, "From: Party List Central ".$from);

        if ($mailing_result == TRUE)
          return 'Mailing successful!';
        else
          return 'Mailing failed.';

    }
}


$list = array();
foreach ($_POST as $key => $value) {
    // is this a name?
    if (substr($key, 0, 4) == 'name') {
        $index = (int)substr($key, 4);
        $list[$index]['name'] = $value;
        $name = $value;
        # echo "$name<br/>";
    }

    // is this an email
    if (substr($key, 0, 5) == 'email') {
        $index = (int)substr($key, 5);
        $list[$index]['email'] = $value;
        $email = $value;
        # echo "$email<br/>";
    }




}

foreach ($list AS $key => $pair_array)
{
        //DOCUMENT_ROOT is the file path leading up to the template name.
        $this_result = mail_message($pair_array['name'],$pair_array['email'],'./invite_text.txt');
        $list[$key]['result'] = $this_result;
}


echo '<pre>';
print_r($list);
echo '</pre>';

?>

 

give that a whirl and see what you get. i'm not sure if this deviates from your assignment, but hopefully it helps you see what order to process the data in. if you're ever wondering what to do with the data you've got, echo all your variables and see what you've got to work with.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.