我有以下代码:
//set $message and run database query
$message .= '<body style="margin: 0;">';
$message .= '<div style="padding: 0 20px;">';
$message .= '<span style="display: block; font-size: 22px; font-weight: bold; margin: 25px 0 -15px 0;"> User Activity on ' . $website . ' for ' . $yesterday . '</span>';
//query the database for today's history
$result = mysql_query("SELECT * FROM user_history WHERE date = '$yesterday' ORDER by name, time, title")
or die(mysql_error());
$old_user = '';
while($row = mysql_fetch_array( $result )) {
$new_user = $row['uid'];
if ($new_user != $old_user) {
$message .= '<br /><br /><hr />' . '<span style="font-size: 18px; font-weight: bold;">' . $row['name'] . '</span>' . '<br />' . $row['company'] . '<br />' . $row['email'] . '<br />' . $row['phone'] . '<br /><br />';
$old_user = $new_user;
}
$message .= '<ul><li>' . $row['time'] .
'<ul><li>' . $row['title'] . ' (<a href="' . $row['url'] . '">' . $row['url']. '</a> )' . '</li></ul>' . '<br /><br />' .
'</li></ul>';
}
//if no user activity, simply say so
if($new_user == "") {
$message .= '<br /><br /><hr /><span style="font-size: 18px; font-weight: bold;">No user activity to report</span>';
}
$message .= '</div>';
$message .= '<div style="position: fixed; bottom: 0; height: 40px; width: 100%; margin: 20px 0 0 0; background: #000; color: #FFF; line-height: 40px; padding: 0 20px;">' .
'Report generated ' . $date . ' ' . $time .
'</div>';
$message .= '</body>';
它在邮件功能中运行良好。像这样的输出电子邮件组......
John Doe
John Doe的公司
johndoe@johndoeco.com
800-555-0000
* 1:30pm
*视频1
* 1:47pm
*视频2
* 1:47pm
*视频3
==============================
相反,我想输出如下:
John Doe
John Doe的公司
johndoe@johndoeco.com
800-555-0000
* 1:30pm
*视频1
* 1:47 pm
*视频2
*视频3
=============================
有人可以告诉我如何实现这一目标吗?谢谢!
答案 0 :(得分:1)
使用与不同用户(if ($new_user != $old_user) {
)相同的技术跟踪当前时间值,但是时间值。
像
这样的东西if ($new_user != $old_user) {
//your existing code
$old_time = null;
$end_tag = false;
}
if ($old_time == null || $row['time'] != $old_time) {
$old_time = $row['time'];
$message .= '<ul><li>' . $row['time'];
$end_tag = true;
}
$message .= '<ul><li>' .
$row['title'] . ' (<a href="' .
$row['url'] . '">' . $row['url']. '</a> )' .
'</li></ul>';
if ($end_tag) {
$message .= "</li></ul>";
$end_tag = false;
}
虽然以这种方式编码开始变得很难看,但应该分成函数。
答案 1 :(得分:0)
这有帮助吗?
$old_time = '';
$video_cache = '';
while($row = mysql_fetch_array( $result ))
{
$new_user = $row['uid'];
if ($new_user != $old_user)
{
$message .= '<br /><br /><hr />' . '<span style="font-size: 18px; font-weight: bold;">' . $row['name'] . '</span>' . '<br />' . $row['company'] . '<br />' . $row['email'] . '<br />' . $row['phone'] . '<br /><br />';
$old_user = $new_user;
}
$new_time = $row['time'];
if ($old_time != $new_time)
{
$video_cache = '<ul><li>' . $old_time . '</li>' . $video_cache . '</ul>';
$message .= $video_cache;
$video_cache = '';
}
$video_cache .= '<li>' . $row['title'] . ' (<a href="' . $row['url'] . '">' . $row['url']. '</a> )' . '</li>';
}