简体   繁体   English

PHP While循环在While循环中

[英]PHP While Loop In a While Loop

Trying to run a script to email users their individual schedules from my database. 尝试运行脚本以通过电子邮件从我的数据库向用户发送其个人日程表。

The code should go like this...select all users from Table 1 where users want their weekly email. 代码应该像这样...从表1中选择所有用户,这些用户需要他们的每周电子邮件。

Then take those user_ids and run a query to obtain the weekly schedules for each user from Table 2. 然后使用这些user_ids并运行查询以从表2中获取每个用户的每周计划。

If the date in the Table 2 is less than or equal to 1 week from when the code is ran, and it does not equal today's date, then set the variable $email_content. 如果表2中的日期距离运行代码的时间少于或等于1周,并且不等于今天的日期,则设置变量$ email_content。

If the date in the Table 2 exceeds 1 week, then the code should know that it is done, and here is would like to go ahead and mail() the info gathered for that user and send only that info, to only that user, then continue on to the next user. 如果表2中的日期超过1周,那么代码应该知道它已经完成了,在这里想继续进行并将该用户收集的信息mail()仅发送给该用户,然后继续寻找下一个用户。

Where it says echo "break", it is essentially marking the separation between each users' schedule, but it is echoing about 20 times. 回声“中断”的地方实质上是标记每个用户的计划之间的间隔,但回声约为20次。 This part will ideally become where I put the mail() function, so I can't have it execute the mail() function 20 times too. 理想情况下,这部分将成为我放置mail()函数的位置,因此我不能让它也执行20次mail()函数。

I've tried a ton of different variations of this code, but can't seem to get it right. 我已经尝试了此代码的许多不同变体,但似乎无法正确完成。 To summarize, I need to get info1 for user1, then mail info1 to user1, then move on to get info2 for user2, then mail info2 to user2, etc... 总而言之,我需要获取user1的info1,然后将info1发送给user1,然后继续获取user2的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;
    }
}

Just saying that you should pay attention to potential SQL injection, escape any user-controllable strings before they get in the SQL statement. 只是说您应该注意潜在的SQL注入,请在用户可控制的字符串进入SQL语句之前对其进行转义。 For example if $row['user_id'] is a string then you must perform mysqli_escape_string 例如,如果$row['user_id']是一个字符串,则必须执行mysqli_escape_string

Seems like that you forgot something in your code: 似乎您忘记了代码中的某些内容:

    $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")) { // I think that you have a $x here?
            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/>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM