使用我的桌面设置(我知道这不是正确的设置),如果subcontent
,在一封电子邮件中将branches.id
数据分组以发送给用户的最佳方式是什么?在users.notify
字段?
我目前的代码如下,但我无法获得匹配的分支名称(if($user_value->branch===$key)
),我觉得有更好的方法来完成我需要做的事情。
MySQL(表格设置不是最好,但它现在就是这样)
# `subcontent` table (left out `date` field)
+-----+--------------+----------+
| id | title | branch |
+-----+--------------+----------+
| 147 | Subcontent 1 | -50-100- |
| 588 | Subcontent 2 | -50- |
| 696 | Subcontent 3 | -50-70- |
+-----+--------------+----------+
# `branches` table
+-----+----------+
| id | branch |
+-----+----------+
| 50 | Branch 1 |
| 70 | Branch 2 |
| 100 | Branch 3 |
+-----+----------+
# `users` table
+----+--------+----------+
| id | name | notify |
+----+--------+----------+
| 1 | User 1 | -50- |
| 2 | User 2 | -70-100- |
| 3 | User 3 | -100- |
+----+--------+----------+
PHP
# Get current date.
$datetime_obj=new DateTime();
# Format the current date.
$today=$datetime_obj->format('Y-m-d');
# Subtract 7 days from the current date.
$datetime_obj->sub(new DateInterval('P7D'));
# Format the past date.
$one_week_ago=$datetime_obj->format('Y-m-d');
$sql="SELECT `sc`.`id`,
`sc`.`title`,
`sc`.`date`,
`b`.`branch`
FROM `subcontent` AS sc
INNER JOIN `branches` AS b ON SUBSTRING_INDEX(TRIM(BOTH '-' FROM `sc`.`branch`), '-', 1)=`b`.`id`
WHERE `sc`.`date` >= $one_week_ago
ORDER BY `sc`.`date` DESC";
$all_subcontent=$db->get_results($sql);
# If there was content posted in the last week...
if(!empty($all_subcontent))
{
$branch_content=array();
# Loop through the subcontent.
foreach($all_subcontent as $subcontent)
{
# Build a new array.
$branch_content[$subcontent->branch][]=array(
'id'=>$subcontent->id,
'title'=>$subcontent->title,
'date'=>$subcontent->date,
'domain'=>$subcontent->domain
);
}
# Results below.
//print_r($branch_content);
$users_sql="SELECT `u`.`email`,
`u`.`notify`,
`b`.`branch`
FROM `users` AS u
INNER JOIN `branches` AS b ON SUBSTRING_INDEX(TRIM(BOTH '-' FROM `u`.`notify`), '-', 1)=`b`.`id`
WHERE `u`.`notify` IS NOT NULL";
$users=$db->get_results($users_sql);
# Results below.
//print_r($users);
$branch_users=array();
$subject=DOMAIN_NAME.' News: '.$one_week_ago.' to '.$today;
# Loop through the users.
foreach($users as $user_key=>$user_value)
{
# Loop through the new array.
foreach($branch_content as $key=>$value)
{
# Branch names never match up!
# Does the users branch match the branch we're currently looping through?
if($user_value->branch===$key)
{
# Build a new array.
$branch_users[$user_key]['email']=$user_value->email;
$branch_users[$user_key]['body']='The following news was posted on '.$key.' at '.DOMAIN_NAME.'<br />'.
'Click the title to read more.<br />'.
'<ul>';
foreach($value as $row)
{
$branch_users[$user_key]['body'].='<li>'.
$row['date'].'<br />'.
'<a href="http://'.DOMAIN_NAME.'/'.$key.'/?post='.$row['id'].'">'.$row['title'].'</a><br />'.
'</li>';
}
$branch_users[$user_key]['body'].='</ul><br />';
}
}
}
# Results below.
//print_r($branch_users);
# Loop through $branch_users and send emails
}
$ branch_content结果
Array
(
[branch_1] => Array
(
[0] => Array
(
[id] => 147
[title] => Subcontent 1
)
[1] => Array
(
[id] => 588
[title] => Subcontent 2
)
[2] => Array
(
[id] => 696
[title] => Subcontent 3
)
)
[branch_2] => Array
(
[0] => Array
(
[id] => 696
[title] => Subcontent 3
)
)
[branch_3] => Array
(
[0] => Array
(
[id] => 147
[title] => Subcontent 1
)
)
)
$ users results(尽管通知有多个分支,但分支只列出一个分支,这很好)
Array
(
[0] => stdClass Object
(
[email] => user_1@example.com
[notify] => -50-
[branch] => branch_1
)
[1] => stdClass Object
(
[email] => user_2@example.com
[notify] => -70-100-
[branch] => branch_2
)
[2] => stdClass Object
(
[email] => user_3@example.com
[notify] => -100-
[branch] => branch_3
)
)
$ branch_users结果
Array
(
[0] => Array
(
[email] => user_1@example.com
[body] => The following news was posted on Branch 1 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_1/?post=147">Subcontent 1</a><br /></li></ul><br />The following news was posted on Branch 1 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_1/?post=588">Subcontent 2</a><br /></li></ul><br />The following news was posted on Branch 1 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_1/?post=696">Subcontent 3</a><br /></li></ul><br />
)
[1] => Array
(
[email] => user_2@example.com
[body] => The following news was posted on Branch 2 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_2/?post=696">Subcontent 3</a><br /></li></ul><br />The following news was posted on Branch 3 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_3/?post=588">Subcontent 2</a><br /></li></ul><br />
)
[2] => Array
(
[email] => user_3@example.com
[body] => The following news was posted on Branch 3 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_3/?post=147">Subcontent 1</a><br /></li></ul><br />
)
)