循环访问数据库以获取详细信息,然后发送包含所述值的电子邮件

发布于 2024-12-17 05:33:24 字数 1394 浏览 0 评论 0原文

我带来了参观者选择的小册子,他们可以选择多个小册子。三天后,他们会收到一封电子邮件,提醒他们选择的小册子。

这是我到目前为止所得到的:

$time_query = "SELECT * FROM users WHERE time < (now() - INTERVAL 1 minute)"; //" //GROUP BY time does group them into an array... well.. it doesnt display duplicate timestamps, so assume it saves it to an array'";

    $time_query_result = mysql_query($time_query, $db) or 

    die("Could not execute sql: $time_query"); 

        $users = array();

        while($row = mysql_fetch_array($time_query_result)) {

            if (!array_key_exists($users[$row["id"]], $users)) {

                $users[$row["id"]] = array('email' => $row["email"], 'brochures' => array());
                $users[$row["id"]]["brochures"] = array('b' => $row["brochures"], 't' => $row["time"]);

            }

        }

        foreach ($users as $user) {

            $text = '<html><body><p>Brochure reminder</p>';
              $i = 0;

            foreach ($user["brochures"] as $brochure) {

                $text .= 'Brochures:<br />'.$i++ . $row["b"];
            }

            $text .= '</body></html>';
            mail($user["email"], $subject, $text, $headers);
        }

我通过电子邮件获取数字而不是小册子名称,我认为这与 array_key_exists 函数有关。

每次用户选择一个小册子时,它都会在数据库中创建自己的行,其想法是拉入用户一次选择的多个小册子(按时间列),因为许多用户可以在一段时间内选择小册子。

任何帮助将不胜感激:)

I am bringing in brochures selected by visitors, and they can select multiple brochures. After three days they are meant to get an email reminding them of the brochures they have chosen.

Here is what I have so far:

$time_query = "SELECT * FROM users WHERE time < (now() - INTERVAL 1 minute)"; //" //GROUP BY time does group them into an array... well.. it doesnt display duplicate timestamps, so assume it saves it to an array'";

    $time_query_result = mysql_query($time_query, $db) or 

    die("Could not execute sql: $time_query"); 

        $users = array();

        while($row = mysql_fetch_array($time_query_result)) {

            if (!array_key_exists($users[$row["id"]], $users)) {

                $users[$row["id"]] = array('email' => $row["email"], 'brochures' => array());
                $users[$row["id"]]["brochures"] = array('b' => $row["brochures"], 't' => $row["time"]);

            }

        }

        foreach ($users as $user) {

            $text = '<html><body><p>Brochure reminder</p>';
              $i = 0;

            foreach ($user["brochures"] as $brochure) {

                $text .= 'Brochures:<br />'.$i++ . $row["b"];
            }

            $text .= '</body></html>';
            mail($user["email"], $subject, $text, $headers);
        }

I am getting numbers through the emails instead of brochure names, and I think its something to do with the array_key_exists fuinction.

Each time a user selects a brochure, it creates its own row in the DB, and the idea was to pull in the multiple brochures a user selected at a time (by the time column), as many users can select brochures over a time period.

Any help would be appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天煞孤星 2024-12-24 05:33:24

在“while”循环中,当我认为您想要附加到“users”数组中时,您正在“users”数组中创建一个新的“brochures”元素。

if (!array_key_exists($row["id"], $users)) {
    $users[$row["id"]] = array('email' => $row["email"], 'brochures' => array());
}
$users[$row["id"]]["brochures"][] = array('b' => $row["brochures"], 't' => $row["time"]);

然后在您的“foreach”中,您将需要使用小册子变量:

foreach ($user["brochures"] as $brochure) {
    $text .= 'Brochures:<br />'.$i++ . $brochure["b"];
}

In your 'while' loop, you're creating a new 'brochures' element in your 'users' array, when I think you're wanting to append to it.

if (!array_key_exists($row["id"], $users)) {
    $users[$row["id"]] = array('email' => $row["email"], 'brochures' => array());
}
$users[$row["id"]]["brochures"][] = array('b' => $row["brochures"], 't' => $row["time"]);

then in your 'foreach', you will want to use the brochure variable:

foreach ($user["brochures"] as $brochure) {
    $text .= 'Brochures:<br />'.$i++ . $brochure["b"];
}
江湖正好 2024-12-24 05:33:24

您当前的代码构建了一个用户数组,其中包含另一个索引为“brochures”的数组。该数组将始终包含两个值。

{
    'b' => $row["brochures"]
    't' => $row["time"])
}

关于这一事实,以下陈述没有意义:

foreach ($user["brochures"] as $brochure) {

}

您所做的只是迭代索引为“b”和“t”的两个值。如果您想迭代一系列小册子,您需要调整您的代码。


另一方面,您有一些重要的错误:

foreach ($user["brochures"] as $brochure) {
    $text .= 'Brochures:<br />'.$i++ . $row["b"];
}

如果您甚至不使用 $brochure 变量,为什么要使用 foreach?

$text .= 'Brochures:<br />'.$i++ . $row["b"];

$row 包含最后一行,这绝对不是您想要的。事实上,$row 超出了范围,在严肃的编程语言中你会看到这一点。

$row["id"]

您大约使用此功能三次。那么为什么不将其存储在变量 $id 中呢?访问带有索引的数组是比简单访问变量更昂贵的操作。

一般来说,我强烈鼓励您切换到面向对象的方法,这样您就可以摆脱这些丑陋的数组中的数组解决方案......

Your current code builds an array of users containing another array with the index 'brochures'. This array will always contain tow values.

{
    'b' => $row["brochures"]
    't' => $row["time"])
}

Regarding this fact the following statements doesn't make sense:

foreach ($user["brochures"] as $brochure) {

}

All you do is iterate over the two values with the index 'b' and 't'. If you want to iterate over a collection of brochures you need to adapt your code.


On the other hand you have a few important mistakes:

foreach ($user["brochures"] as $brochure) {
    $text .= 'Brochures:<br />'.$i++ . $row["b"];
}

Why use a foreach if you don't even use the $brochure variable?

$text .= 'Brochures:<br />'.$i++ . $row["b"];

$row contains the last row, which is definitively not what you wanted. In fact, $row is out of scope, in a serious programming language you would see this.

$row["id"]

You use this about three times. So why not store it in a variable $id? Accessing arrays with indexes is a more expensive operation than simply accessing a variable.

In general I strongly encourage you to switch to an object oriented approach so you get rid of these ugly array in array in array solution...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文