0

我想$name在一个名为sendMail. 我是否必须以某种方式将函数合并到循环中?

<?php
while ($row = mysql_fetch_assoc($result))
{
    print_r($row); echo "<br><br>";
    $name = $row['Name'];
    $date = $row['sDate'];
    $time = $row['sTime'];
    $phone = $row['Phone'];

    $email = $row['Email'];
    sendMail($row['Email']);

    $company = $row['Company'];
    $course = $row['Course'];
    $ref = $row['Reference'];
    $optout = $row['optout'];

    echo "<tr bgcolor=#ABB5F6>
    <td>$name</td>
    <td>$date</td>
    <td>$time</td>
    <td>$phone</td>
    <td>$email</td>
    <td>$company</td>
    <td>$course</td>
    <td>$ref</td>
    <td>$optout</td>
    </tr>";
}

// Mail to $to and $emailarray recipients
function sendMail($to)
{
    $subject = 'Test mail';
    $message = 'Hello'.$name; // I want $name from the while loop
    $headers = array();
    $headers[] = "From:" . "myemail@email.com";
    $headerz = implode("\r\n", $headers);

    mail($to, $subject, $message, $headerz);
}

?>
4

4 回答 4

5

要么sendMail()接受一个数组(即$row), or add another parameter tosendMail() which is called$name` 并将其传入。

IE。

function sendMail($to, $name)

称为:

sendMail($row['Email'], $row['Name']);

该参数可能是最简单的,不要走全局路径。我建议也不要使用数组,因为格式是未定义的,并且如果您的表结构可以更改,则可能会更改。如果您要使用mysql_fetch_object强定义的类,那么这将是最可接受的解决方案(当然,也使用 PDO/MySQLi)......特别是如果您计划在$row未来扩展以使用更多数据.

于 2012-07-10T15:33:45.873 回答
5

为什么不直接将 $name 传递给函数 sendMail:

// Mail to $to and $emailarray recipients
function sendMail($to, $name)
{
    $subject = 'Test mail';
    $message = 'Hello'.$name; // I want $name from the while loop
    $headers = array();
    $headers[] = "From:" . "myemail@email.com";
    $headerz = implode("\r\n", $headers);

    mail($to, $subject, $message, $headerz);
}

然后用 name 变量调用 sendMail 函数?

于 2012-07-10T15:33:49.093 回答
5

为什么不直接通过呢?

function sendMail($to, $name);

并打电话

sendMail($row['Email'], $row['name']);
于 2012-07-10T15:34:04.547 回答
0

为了让您的代码正常工作,我已经进行了必要的更改。

<?php
            while ($row = mysql_fetch_assoc($result))
            {
                print_r($row); echo "<br><br>";
                $name = $row['Name'];
                $date = $row['sDate'];
                $time = $row['sTime'];
                $phone = $row['Phone'];

                $email = $row['Email'];

                // i added name as a parameter
                sendMail($row['Email'],$name);

                $company = $row['Company'];
                $course = $row['Course'];
                $ref = $row['Reference'];
                $optout = $row['optout'];

                echo "<tr bgcolor=#ABB5F6>
                <td>$name</td>
                <td>$date</td>
                <td>$time</td>
                <td>$phone</td>
                <td>$email</td>
                <td>$company</td>
                <td>$course</td>
                <td>$ref</td>
                <td>$optout</td>
                </tr>";

            }

            // Mail to $to and $emailarray recipients
            // i added name as a parameter
            function sendMail($to,$name)
            {
                $subject = 'Test mail';
                $message = 'Hello'.$name; // I want $name from the while loop
                $headers = array();
                $headers[] = "From:" . "myemail@email.com";
                $headerz = implode("\r\n", $headers);

                mail($to, $subject, $message, $headerz);
            }

            ?>
于 2012-07-10T15:42:35.917 回答