在foreach循环中存储变量
问题描述:
您好我目前有一些PHP代码,将先前选中的时隙从前一页上的复选框放到我的数据库中,但我不知道如何在我这样做之前将每个变量存储在变量中,以便我可以使用它们列出我的电子邮件功能中的预约插槽在foreach循环中存储变量
当我尝试向我的电子邮件功能中插入'$ 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 = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
答
虽然上述意见是对的,让我来帮你。
首先,您需要确定选中了哪些复选框,因此请执行一些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] .. ..等等....
所以你的邮件功能可能看起来像:
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 = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
}
因此,这将发送一封电子邮件,每件内$savedData
- 如果这确实是意图。
在这段代码中有这么多级别的错误... – mellamokb 2013-03-05 21:06:48
请发布您的HTML代码 – 2013-03-05 21:07:03
你能否写出更多细节?你正在谈论电子邮件,但我只是在循环中插入mysql语句... – 2013-03-05 21:09:34