将for循环中的项添加到变量中

时间:2013-12-25 03:31:00

标签: php loops for-loop mailer

我正在使用以下代码(此处稍作修改以删除安全项)在表单处理后发送电子邮件并将数据插入数据库。它工作得很好,但我真正努力的一件事就是最后一个循环。在表单上,​​一个人可以选择他们正在输入的多个类,我需要将其添加到电子邮件中。 $ message变量的其余部分已设置。工作正常,我只是不是100%如何循环通过每个$ class []并将其添加到$ message的尾部。

非常感谢任何帮助。

<?


include "Scripts/db.class.php";
include "Scripts/functions.class.php";

$db = new mysqldb();
$db->select_db();
$f = new functions();

$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$streetaddress = $_POST['streetaddress'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$phone = $_POST['phone'];
$emailaddress = $_POST['emailaddress'];
$racedate = $_POST['racedate'];
$number = $_POST['number'];

$sql1 = "SELECT date FROM schedule WHERE id = " . $racedate . " ";
    $result1 = $db->query($sql1);
    while ($r1 = $db->fetch_array($result1)) {
        $datemail = date_create($r1['date']);
    }


$sql ="INSERT INTO `riders` (`name`, `streetaddress`, `city`, `state`, `zipcode`, `phone`, `emailaddress`, `racedate`, `number`) VALUES ('" . $fname . " " . $lname . "', '" . $streetaddress . "', '" . $city . "', '" . $state . "', '" . $zipcode . "', '" . $phone . "', '" . $emailaddress . "', '" . $racedate . "', '" . $number . "')";
$result = $db->query($sql);
$inserted_user_id = $db->last_insert_id();

if (isset($_POST['class'])) {

    for ( $i=0;$i<count($_POST['class']);$i++) {
        $class = $_POST['class'][$i];
        $sql = "INSERT INTO rider_classes (rider, class) VALUES ('$inserted_user_id', '$class')"; //Insert into rider_classes table
        $result = $db->query($sql);
    }
}

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.server';  // Specify main and backup server
$mail->Port = "26";
$mail->From = 'email.address';
$mail->FromName = 'Mailer';
$mail->addAddress($emailaddress);  // Add a recipient

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = "Preentry for " . date_format($datemail, 'l F jS') . "";
$mail->Body    = $message;
$message =  "Name : " . $fname . " " . $lname . "\n<br />
                Race Date : " . date_format($datemail, 'l F jS') . "\n<br />
                Classes : ";
        for ( $i=0;$i<count($_POST['class']);$i++) {
        $class = $_POST['class'][$i];
        echo $f->getClassName($db, $class) . "<br />";
        }

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}

print "<meta http-equiv='refresh' content='0;URL=thanks.php?id=". $inserted_user_id ."'>";
?>

1 个答案:

答案 0 :(得分:1)

$message =  "Name : " . $fname . " " . $lname . "\n<br />
                Race Date : " . date_format($datemail, 'l F jS') . "\n<br />
                Classes : ";
        for ( $i=0;$i<count($_POST['class']);$i++) {
            $class = $_POST['class'][$i];
            $message .= ", " . $class;  <<<< adds the current $class in loop at the end of $message
            // ...
        }