在foreach循环中存储变量

时间:2013-03-05 21:04:57

标签: php mysql foreach

您好我目前有一些PHP代码,它会将之前选定的时间段从前一页的复选框放入我的数据库,但我不知道如何在执行此操作之前将每一个存储在变量中,以便我可以使用他们列出了我的电子邮件功能

中的约会时段

当我尝试将'$ key'插入我的电子邮件功能时,它只保留最后选择的值...

我在第一页的代码是:

<?php
for ($i=0;$i<=31;$i++){ while ($myrow = mysql_fetch_row($result)) {
printf("<tr><td>%s</td><td>%s</td></tr>\n",
$myrow[0]);
}

echo "<td><input type='checkbox' name='$displayTime2' onclick='KeepCount()'></td>";
echo "<td>$pagetime</td>"; ?>

目前我的代码如下:

<?php
foreach ($_POST as $key=>$value)
{
    echo $key; // show times selected on previous page
    mysql_query("INSERT INTO appointment(Patient_ID, Appointment_Date, Appointment_Time
                , Practice_ID, Appointment_ID)
                VALUES('$patid','$insertdate','$key','$pracid','$apptype')");   
}
?>

我的邮件功能如下:

   $to = "$pemail";
   $subject = "Talking Teeth Check Up Booking Confrmation";
   $message = "Hello! This e-mail is to confirm your ".$appname." appointment has been
   booked for ".$insertdate. " at ".$key." at the ".$pracname." practice";
   $from = "talkingteethdentists@talkingteeth.co.uk";
   $headers = "From:" . $from;
   mail($to,$subject,$message,$headers);
   echo "Mail Sent.";

1 个答案:

答案 0 :(得分:0)

虽然上面的评论是对的,但请允许我帮助你。

首先,您需要确定选择了哪些复选框,因此请执行一些IF语句,以便在foreach循环中的每个内容中找出答案。

尝试这样的事情:

<?php
$savedData = array();
foreach ($_POST as $key=>$value){
    $key = mysql_real_escape_string($key);
    echo($key. "<br>"); // show times selected on previous page
    mysql_query("INSERT INTO appointment(Patient_ID, Appointment_Date, Appointment_Time
                , Practice_ID, Appointment_ID)
                VALUES('$patid','$insertdate','$key','$pracid','$apptype')");   

    //To save the variables for later:
    $savedData[] = $key;
}
?>

现在你可以使用$ savedData [0] .... $ savedData [1] ... $ savedData [2] ....等来访问我们所有的KEY ....

因此,您的电子邮件功能可能如下所示:

foreach($savedData as $time){
   $to = "$pemail";
   $subject = "Talking Teeth Check Up Booking Confirmation"; //mis-spelled confirmation =)
   $message = "Hello! This e-mail is to confirm your ".$appname." appointment has been
   booked for ".$insertdate. " at ".$time." at the ".$pracname." practice";
   $from = "talkingteethdentists@talkingteeth.co.uk";
   $headers = "From:" . $from;
   mail($to,$subject,$message,$headers);
   echo "Mail Sent.";
}

因此,这将为$savedData内的每件作品发送一封电子邮件 - 如果真的是意图。