gpt4 book ai didi

php - 如何通过电子邮件向所有用户发送用户特定信息。

转载 作者:行者123 更新时间:2023-11-29 14:03:03 25 4
gpt4 key购买 nike

我为一个网站构建了一个竞赛系统,它的工作原理是用户登录,根据现实生活中的事件(销售特定元素)提交选票,然后在月底,随机选票被选择,该选票的所有者就是获胜者。

我被要求创建一个脚本,该脚本将通过电子邮件向数据库中的所有用户发送他们在系统中当前拥有的选票数量。

我当前的登录/注册系统是经过大量编辑的 HTML-Form-Guies Simple PHP Registration System 版本.

我知道我想做的事情的伪代码。

一步一步,所需的方法是这样的。

调用EmailUsersTotalEntries,用数据库中的所有用户填充一个数组,选择数组中的第一个条目,用户1,找到itemsold列中用户id为1的所有行的总和。然后发送用户1一封电子邮件,其中包含从选票中选择 sum(itemsold) 的结果,其中 userid = 1;到用户 1。然后循环转到用户 2 并执行相同的操作,直到它向数据库中的每个用户发送了一封电子邮件。

以下是我编写的或来自登录系统的一些方法,将用于完成此任务。我唯一的问题是我不知道如何创建一个循环,以便它从用户 1 开始,然后一直到用户 2,而且我不知道如何在数据库中查询数据库/循环中任何用户的 user_id目前已开启。

方法如下:这是主要方法,它将调用子方法来收集用户,然后发送实际的电子邮件。 我不确定 TotalEntries 是否应该是一个数组

 function EmailTotalEntries()
{
if(empty($_POST['email']))
{
$this->HandleError("Email is empty!");
return false;
}
$user_rec = array();
if(false === $this->GetUsers($user_rec))
{
return false;
}
**/* $TotalEntries = array(); */**
if(false === $this->GetTotalEntriesForEmail($user_rec, $TotalEntries)
{
return false;
}

//At this point, I have an array, user_rec, populated with all the data from my users table, and an array $TotalEntries that will have nothing since its trying to pull from user_rec, which usually is one user but right now is all of the users.
//This is where I know I should have already started the loop, so chosen the first element in user_rec, and applied the GetTotalEntriesForEmail method, then the SendUserEmail method, then gone to the top of the loop and gone to the second user_rec element and repeat.

if(false === $this->SendUsersEmail($user_rec, $TotalEntries))
{
return false;
}
return true;
}

这是收集用户的方法

function GetUsers(&$user_rec)
{
if(!$this->DBLogin())
{
$this->HandleError("Database login failed!");
return false;
}

$result = mysql_query("Select * from $this->tablename",$this->connection);

$user_rec = mysql_fetch_assoc($result);

return true;
}

这是我编写的用于获取已登录用户的 TotalEntries 的方法(检查他的控制面板以查看他有多少条目)

function GetTotalEntries()
{
if(!$this->CheckLogin())
{
$this->HandleError("Not logged in!");
return false;
}

$user_rec = array();
if(!$this->GetUserFromEmail($this->UserEmail(),$user_rec))
{
return false;
}
$qry = "SELECT SUM(itemsold) AS TotalEntries FROM entries WHERE user_id = '".$user_rec['id_user']."'";
$result = mysql_query($qry,$this->connection);
while($row = mysql_fetch_array($result))
{
echo $row['TotalEntries'];
}



}

以下是我认为需要对其进行调整以使其在电子邮件中发挥作用的方式。

function GetTotalEntriesForEmail($user_rec, &$TotalEntries)
{
if(!$this->DBLogin())
{
$this->HandleError("Database login failed!");
return false;
}


$qry = "SELECT SUM(itemsold) FROM entries WHERE user_id = '".$user_rec['id_user']."'"; //$user_rec['id_user'] should the be id of the user the loop is currently on.
$result = mysql_query($qry,$this->connection);
$TotalEntries = mysql_fetch_assoc($result);
return true;

}

这是实际的电子邮件

function SendUsers($user_rec, $TotalBallots)
{
$email = $user_rec['email']; //should be the for the user the loop is currently on.

$mailer = new PHPMailer();

$mailer->CharSet = 'utf-8';

$mailer->AddAddress($email,$user_rec['name']); //user the loop is currently on.

$mailer->Subject = "Total Ballots to Date";

$mailer->From = $this->GetFromAddress();

$mailer->Body ="Hello ".$user_rec['name']."\r\n\r\n". //Same thing
"To date you have: "/* .$TotalBallots. */" ballots.\r\n" //Same thing

if(!$mailer->Send())
{
return false;
}
return true;
}

我不太擅长 PHP,这整件事对我来说是一次学习经历,因此非常感谢您的帮助。

如果我还不清楚,也许用另一种语言给出一个例子会更清楚,所以这就是我想做的,但是用java

for(x = 0; x <= user_rec.length; x++)
{
int ballots = getTotalEntriesForUser(x);
sendEmailToUser(ballots)
}

如果我不够清楚,请告诉我,我会尽力澄清。

如何将上述代码与一个循环结合起来,向所有用户一封一封地发送一封电子邮件,每封电子邮件对于发送到的用户来说都是唯一的?

最佳答案

你的函数是类的一部分吗?您不一定需要他们这样做。这是我的建议,如果需要,您可以将其转换为函数或类。另外,您可能需要考虑研究和使用 MySQLi,并利用它使用的类。再说一次,所有这些都只是我的建议。

在不知道你的表结构的情况下,我只是猜测一下。

$sql = mysql_query("SELECT u.*,
u.user_id AS user,
COALESCE(SUM(e.itemssold), 0) AS total_items
FROM users u
LEFT JOIN entries e ON e.user_id = u.user_id
GROUP BY u.user_id");

while($row = mysql_fetch_array($sql))
{
$user = $row['user'];
$email = $row['user_email'];
$items = $row['total_items'];

yourEmailFunction($email, $items);
}

这会根据匹配的用户 ID 从您的用户表和条目表中提取信息。它将用户表中的用户 ID 设置为用户,这样您就不必稍后尝试区分这两者。要了解 COALESCE 函数,read here 。 while() 函数将循环遍历从该 SQL 语句中提取的每个用户。

这尚未经过任何方式的测试,但这基本上就是您所需要的。只需传递用户的电子邮件和总项目,然后编写电子邮件函数将该信息发送到该电子邮件地址。

但是,如果您知道您的函数可以正常工作,并且想要使用 for 循环(例如您在 Java 中提供的循环),那么您可以按照以下方式在 PHP 中编写它。

for($x = 0; $x <= count($user_rec); $x++)
{
$ballots = getTotalEntriesForUser($x);
sendEmailToUser($ballots);
}

关于php - 如何通过电子邮件向所有用户发送用户特定信息。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14775917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com