在有人注册我的网站后,他们必须获得加入它的权限。我已经设置了一个组数据库表,如果有人是组1级,他们就无法访问该站点。然后,如果某人是2-5组,他们就可以。每当我更改该组级别时,我都希望向他们发送电子邮件。电子邮件工作正常,但我无法弄清楚如何做到这一点:
我希望将任何组级别发送到我的php文件2-6,给出不同的消息并回显我指定的变量。因此,如果有人被授予第2组,我想要这部分电子邮件......
<p>You have been put into the permission category of </p><br>
说
<p>You have been put into the permission category of Spectator. This level of permission allows you to do basic site tasks</p><br>
我只是不确定如何构建它,因此它根据授予的用户和组级别而有所不同。
我该如何做到这一点?
if($change_group = 2) {
$group2 = "Spectator";
$group2Message = "This level of permission allows you to do basic site taks....."
}
if($change_group = 3) {
$group3 = "Team Member";
$group3Message = "This level of permission allows ....."
}
if($change_group = 4) {
$group4 = "Commissioner";
$group4Message = "This level of permission allows ....."
}
if($change_group = 6) {
$group6 = "Denied";
$group6Message = "This level of permission allows ....."
}
$to = $email;
$subject = 'Permission Status';
$message = '
<html>
<head>
<title>Group Change</title>
</head>
<body>
<p>Hi '.$firstname.',</p><br>
<p>You have been added to the group.. </p><br>
答案 0 :(得分:1)
让if($change_group = 2)
返回true,因为它成功地将变量$change_group
分配给值2.使用==
代替,检查值是否等于2.
这是使用switch
代替一系列if
语句的好地方,但这样做并没有错。
if ($change_group == 2) {
$groupname = "Spectator";
$groupmessage = "do basic site tasks";
} elseif ($change_group == 3) {
$groupname = "Team Member";
$groupmessage = "do other tasks";
} elseif ($change_group == 4) {
$groupname = "Commissioner";
$groupmessage = "act like a boss";
} elseif ($change_group == 6) {
$groupname = "Denied";
$groupmessage = "be denied";
}
然后你的$message
设置为类似
$message = '
<html>
<head>
<title>Group Change</title>
</head>
<body>
<p>Hi '.$firstname.',</p><br>
<p>You have been put into the permission category of '.$groupname.'! This level of permissions allows you to '.$groupmessage.'.</p><br>';
然后你只需按照你想要的方式格式化它!
答案 1 :(得分:0)
不是使用一堆elseif,甚至是一个开关,另一种可能性是创建一个组数组,并根据$change_group
值从数组中提取。
<?php
$groups = array(
2 => array('name' => 'Spectator', 'message' => 'This level of permission allows you...'),
3 => array('name' => 'Team Member', 'message' => 'This level of permission allows ...'),
4 => array('name' => 'Commissioner', 'message' => 'This level of permission allows ...'),
6 => array('name' => 'Denied', 'message' => 'This level of permission allows ...')
);
'<p>You have been added to the group '.$groups[$change_group]['name']
.' '.$groups[$change_group]['message'].'</p><br>';
最终,您可能希望将这些值存储在数据库中,而不是对其进行硬编码。