嘗試運行腳本以通過電子郵件將用戶的個人日程安排從我的數據庫發送給用戶。PHP雖然在While循環中循環
代碼應該像這樣...從表1中選擇用戶希望每週發送電子郵件的所有用戶。
然後取這些user_ids並運行一個查詢以從表2
如果在表2中的日期小於或等於1周從當代碼是RAN獲取每週日程安排爲每個用戶,並且它不等於今天的日期,然後設置變量$ email_content。
如果表2中的日期超過1周,那麼代碼應該知道它已完成,並且這裏是想繼續並且mail()爲該用戶收集的信息並僅發送該信息到只有那個用戶,然後繼續到下一個用戶。
它說回聲「中斷」,它基本上標記每個用戶的時間表之間的分離,但它回聲約20次。這部分理想情況下會成爲我放置mail()函數的地方,所以我不能讓它執行20次mail()函數。
我已經試過了這段代碼的一大堆不同的變體,但似乎無法弄清楚它的正確性。總之,我需要獲得用戶1 INFO1,然後郵寄INFO1爲user1,然後轉移到獲取用戶2 INFO2,然後郵寄INFO2的user2,等...
<?php
// GET USERS WHO WANT THEIR WEEKLY SCHEDULE EMAILED TO THEM (Table 1)
$sql = "SELECT * FROM XXXXXXX WHERE weekly_email = 'Yes'";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_array($result)) {
$id .= "'" . $row['user_id'] . "', ";
}
$id = trim($id, ', ');
// GET THOSE USERS' SCHEDULES (Table 2)
$sql_email = "SELECT * FROM XXXXXXXX WHERE user_id IN ($id) ORDER BY user_id, date, start_time ASC";
$result_email = mysqli_query($connection, $sql_email);
while ($row_email = mysqli_fetch_array($result_email)) {
$truck_name_email = $row_email['truck_name'];
$location_name_email = $row_email['location_name'];
$date_email = $row_email['date'];
$address_email = $row_email['address'];
$x = strtotime("7 days");
$start_email = $row_email['start_time'];
$end_email = $row_email['end_time'];
if ($date_email <= date("m/d/Y l", $x)) {
if ($date_email !== date("m/d/Y l")) {
if (!empty($address_email)) {
$address_email2 = explode(",", $row_email['address'], 2);
$email_content = $date_email . substr($date_email, 11) . " - "
. $location_name_email . ", " . $address_email2[0] . ", "
. $start_email . "-" . $end_email . "<br/>";
echo $email_content;
} elseif (empty($address_email)) {
$email_content .= $date_email . " - " . $location_name_email
. ", " . $start_email . "-" . $end_email . "<br/>";
echo $email_content;
}
}
}
if ($date_email >= date("m/d/Y l", $x)) {
echo "break";
// break;
}
}
無需兩個查詢。只需使用子查詢。如果你有'IN($ id)'而不是'IN'(SELECT user_id FROM XXXXXX WHERE weekly_email ='Yes)' – siride
當你想比較時間戳時,你比較if塊中的兩個字符串。例如「星期四」>「星期天」? – Drakes
謝謝@siride,我擺脫了我的代碼中的第一個查詢,並根據您的建議更新了第二個查詢。 – Shane